273 views
0 votes
0 votes
what is the difference between printf(“\nI am don”) and printf(“%s”, I am don);

1 Answer

1 votes
1 votes
printf("I am don");

"I am don" with a "\0" appended will be stored in some memory address say at 1000, and 1000 will be passed to printf function. printf prints all characters starting from 1000 till a '\0' and hence "I am don" will be printed. 

printf("%s", I am don);

Here also "I am don" with "\0" appended will be stored at some memory address say 1000. "%s" with "\0" appended is another string literal stored at another address say 1020. Now, 1020 is passed to printf function and it prints all characters till "\0". When it encounters the format specifier "%s". it takes in the next argument - which is address 1000, and prints all characters from there till "\0". So, "I am don" will be printed. 

So, the first one is better from performance point of view.

edited by

Related questions

0 votes
0 votes
0 answers
1
ranarajesh495 asked Oct 14, 2018
304 views
Can someone explain how %x works below and how we can write an operation in ths statement.
0 votes
0 votes
1 answer
2
Vaishnavi01 asked Sep 3, 2018
546 views
0 votes
0 votes
2 answers
3
anonymous asked May 14, 2018
511 views
What changes must be done for printing value 5. #include <stdio.h int main() { int var; /*Suppose address of var is 2000 */ void *ptr = &var; *ptr = 5; printf("var=%d and...
0 votes
0 votes
1 answer
4
anonymous asked May 14, 2018
580 views
Please explain solution in brief . #include <stdio.h void f(char ); int main() { char *argv[] = { "ab", "cd", "ef", "gh", "ij", "kl" }; f(argv); return 0; } void f(char ...