Reputation: 331
I'm new to C and I'm struggling to understand the difference between the two. Can someone use an example with both? Please correct my logic if I'm wrong but this is the way I understand the following:
int a = 10;
printf("%d\n", a);
The purpose of %d
is to notify the compiler that the variable we want to print is of int type. At least, that's the way I've been thinking of it so far. Thank you.
Upvotes: 0
Views: 5153
Reputation: 53026
The *printf()
family of functions take variable number of arguments, so you need to pass a format string with specifiers that let the function know the type of the currently parsed argument.
A simple example would be like this1
#include <stdio.h>
#include <stdarg.h>
int xprintf(const char *format, ...)
{
char chr;
int count;
va_list va;
count = 0;
va_start(va, format);
while ((chr = *format++) != '\0')
{
if ((chr == '%') && ((chr = *format++) == 'd'))
{
int argument;
argument = va_arg(va, int);
count += printf("%d", argument);
}
else
{
fputc(chr, stdout);
count += 1;
}
}
return count;
}
int
main(void)
{
xprintf("example %d\n", 4);
return 0;
}
The compiler does not need to know anything about it, but somtimes it does and it helps you know when you pass the wrong argument type by mistake, but code with wrong, extra, less arguments can compile and then the behavior of the program can't be specified in those cases.
1Notice that I've used the standard printf()
as an auxiliary function to print the integer.
Upvotes: 1
Reputation: 123578
The conversion specification tells printf
both the type of the argument and how you want to format the output for it. The length modifier is a part of the conversion specification, and it gives printf
additional type information for the corresponding argument.
printf
is a variadic function, which means arguments of certain types are promoted to a more limited set of types; arguments of type char
and short
are promoted to int
, arguments of type float
are promoted to double
, etc. The length modifier helps you communicate the original type to printf
, so it will properly convert the promoted argument back to the original type.
So, examples:
int aRegularInt = 64;
short aShortInt = 64;
char aReallyShortInt = 64;
printf( "aRegularInt = %d\n", aRegularInt );
printf( "aShortInt = %hd\n", aShortInt );
printf( "aReallyShortInt = %hhd\n", aReallyShortInt );
printy( "aReallyShortInt = %c\n", aReallyShortInt );
The conversion specification %d
indicates that the argument has type int
and that the output should be a string of decimal digits with a leading -
for negative values. There is no length modifier.
The conversion specification %hd
indicates that the argument has type short int
. The output is the same as above. h
is the length modifier.
The conversion specification %hhd
indicates that the argument has type char
. The output is the same as above. In this case the length modifier is hh
.
The conversion specification %c
indicates that the argument has type int
, and that the output should be the glyph corresponding to that character code (in ASCII, the character for code 64 is @
).
Upvotes: 0
Reputation: 7437
In a format string like %ld
, the letter l
would be the length modifier, which indicates to the standard library function (not the compiler) that you want the associated argument to be interpreted as a long int
. There's a handy chart showing the standard interpretations made by various length/conversion character combinations here on cplusplus.com.
The compiler knows the types of all your variables at compile time, but the printf
function doesn't have a way to determine the types of arguments at run time because of how variadic functions work. You can experiment for yourself and see how different combinations of length modifiers and conversion specifiers can yield completely different results for the same data passed to printf
.
Upvotes: 2