I don't understand what the %s
and d%
do in this C code:
for (i=0;i<sizeof(code)/sizeof(char*); i++) { printf("%s%d%s%d\n", "Length of String ", i, " is ", strlen(code[i])); str = code[i]; printf("%s%d%s%c\n","The first character in string ", i, " is ", str[0]); }
I'm new to the C language and my background is in Java.
%s%d%s%d
symbols denote?+
?%s and string We can print the string using %s format specifier in printf function. It will print the string from the given starting address to the null '\0' character. String name itself the starting address of the string. So, if we give string name it will print the entire string.
In C programming language, %d and %i are format specifiers as where %d specifies the type of variable as decimal and %i specifies the type as integer. In usage terms, there is no difference in printf() function output while printing a number using %d or %i but using scanf the difference occurs.
The printf() family of functions uses %
character as a placeholder. When a %
is encountered, printf reads the characters following the %
to determine what to do:
%s - Take the next argument and print it as a string %d - Take the next argument and print it as an int
See this Wikipedia article for a nice picture: printf format string
The \n
at the end of the string is for a newline/carriage-return character.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With