Difference between print and gets

I'm starting to program. I wrote C programming books, but I'm confused.

1.) What is the difference between printf and gets? I find getting is easier and doesn't have any formats?

+3
source share
3 answers

Printf

The printf function writes a formatted string to standard output. A formatted string is the result of replacing placeholders with their values. It sounds a little complicated, but with an example it will become very clear:

   printf("Hello, my name is %s and I am %d years old.", "Andreas", 22);

%s %d , . man ( ) , % d () % s ().

, , . , undefined ( , - : , , ..):

printf("Hello, I'm %s years old.", 22);

, C .

gets : .

:

char name[512];
printf("What your name? ");
gets(name);

, , name.

gets() . , , .

:

gets(). , , gets() , gets() , . . fgets() .

: , , , (name ), , , , gets . undefined, .

, , , gets() . .

, fgets. , gets, size, :

char *fgets(char *s, int size, FILE *stream);

, :

char name[512];
printf("What your name? ");
fgets(name, sizeof(name) /* 512 */, stdin /* The standard input */);
+7

.

printf: .
: .

+2

printf: (.. ), stdout . , '\n' .

puts: stdout, .

scanf: , .

gets: just read the line from stdin without regard to the format, the returned character is replaced with the line terminator '\ 0'.

http://en.wikipedia.org/wiki/Printf

http://en.wikipedia.org/wiki/Gets

+2
source

Source: https://habr.com/ru/post/1759820/


All Articles