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.
- What do the
%s%d%s%dsymbols denote? - Why are there so many of them?
- Is the comma used here for concatenation instead of a
+?
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:See this Wikipedia article for a nice picture: printf format string
The
\nat the end of the string is for a newline/carriage-return character.