Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c

printf

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%d symbols denote?
  • Why are there so many of them?
  • Is the comma used here for concatenation instead of a +?
like image 862
Simon Kiely Avatar asked Jan 26 '12 23:01

Simon Kiely


People also ask

What is %s in printf?

%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.

What does %d mean in c?

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.


1 Answers

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.

like image 171
iccir Avatar answered Oct 09 '22 07:10

iccir