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 is for string %d is for decimal (or int) %c is for character
It appears to be chewing through an array of characters, and printing out whatever string exists starting at each subsequent position. The strings will stop at the first null in each case.
The commas are just separating the arguments to a function that takes a variable number of args; this number corresponds to the number of % args in the format descriptor at the front.
% notation is called a format specifier. For example, %d tells printf() to print an integer. %s to print a string (char *) etc. You should really look it up here: http://google.com/search?q=printf+format+specifiers
No, commas are not used for string concatenation. Commas are for separating arguments passed to a function.
The first argument denotes placeholders for the variables / parameters that follow.
For example, %s
indicates that you're expecting a String
to be your first print parameter.
Java also has a printf
, which is very similar.
The first argument to printf
is a string of identifiers.
%s refers to a string %d refers to an integer %c refers to a character. Therefore: %s%d%s%c\n prints the string "The first character in sting ", %d prints i, %s prints " is ", and %c prints str[0].
%s%d%s%d\n is a format string. It is used to specify how the information is formatted on an output. here the format string is supposed to print string followed by a digit followed by a string and then again a digit. The last symbol \n represents carriage return which marks the end of a line. In C, strings cannot be concatenated by + or , although you can combine different outputs on a single line by using the appropriate format strings (the use of format strings is to format output info.).
%d is print as an int %s is print as a string %f is print as floating point
It should be noted that it is incorrect to say that this is different from Java. Printf stands for print format, if you do a formatted print in Java, this is exactly the same usage. This may allow you to solve interesting and new problems in both C and Java!
"%s%d%s%d\n"
is the format string; it tells the printf
function how to format and display the output. Anything in the format string that doesn't have a %
immediately in front of it is displayed as is.
%s
and %d
are conversion specifiers; they tell printf
how to interpret the remaining arguments. %s
tells printf
that the corresponding argument is to be treated as a string (in C terms, a 0-terminated sequence of char
); the type of the corresponding argument must be char *
. %d
tells printf
that the corresponding argument is to be treated as an integer value; the type of the corresponding argument must be int
. Since you're coming from a Java background, it's important to note that printf
(like other variadic functions) is relying on you to tell it what the types of the remaining arguments are. If the format string were "%d%s%d%s\n"
, printf
would attempt to treat "Length of string"
as an integer value and i
as a string, with tragic results.
%(letter) denotes the format type of the replacement text. %s specifies a string, %d an integer, and %c a char.
Source: Stackoverflow.com