Simon Kiely Simon Kiely - 1 year ago 80
C Question

What does %s and %d mean in printf in the C language?

I don't understand what the

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
    symbols denote?

  • Why are there so many of them?

  • Is the comma used here for concatenation instead of a

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download