Reputation: 1113
#include <stdio.h>
int main()
{
char string[]="Programming Language";
printf(string);
printf("\n%s",string);
return 0;
}
Output
Programming Language
Programming Language
Why is the output same?
Upvotes: 0
Views: 183
Reputation: 604
First argument of printf could contains plain text besides modifiers. So basically it equals. %s is useful when you want to include some string inside of other e.g.:
printf ("One Two %s Four Five (%d, %d, %d, %d, %d)", "Three", 1, 2, 3, 4, 5);
Using %s modifier standalone quite meaningless. Just one thing you should remember - in the case you are not using %s modifier and pass string as a first parameter you should quote % symbol. E.g.:
printf ("I am 100%% sure that it would works!");
So basically instead just single % sign you need to use double % ( %% ). Even in the case you pass it as a variable:
char s [] = "50%% complete";
printf (s);
Hope it makes sence!
Upvotes: 0
Reputation: 67195
They are not the same. The second one includes a new line that is not included in the first one.
If you remove the newline, they will be the same because:
The first version simply prints the contents of string
.
The second version uses %s
, which is replaced with the contents of string
.
Either way, the result would be the same.
Upvotes: 1
Reputation: 53006
When printf
parses the format string it prints the characters that are not format specifiers as they are.
So when it parses "Programming Language"
it just echoes each character.
Upvotes: 2
Reputation: 59681
The first printf statement is the same as:
printf("Programming Language");
and your second printf statement is just the same: (Because the 'placeholder' gets replaced with the variable, + a new line at the start)
printf("\nProgramming Language");
So that's why it is the same output
Upvotes: 3