If i try to print 1000 as given in C:
printf("%c\n",1000);
printf("%s\n",1000);
What will be the output?Or Will I get error for both lines ?
If i try to print 1000 as given in C:
printf("%c\n",1000);
printf("%s\n",1000);
What will be the output?Or Will I get error for both lines ?
printf("%c\n",1000);
%c
requires an argument of type int
(so far, so good) and prints it as a character. 1000 is (almost certainly) outside the range of char
or unsigned char
, but
If no
l
length modifier is present, theint
argument is converted to anunsigned char
, and the resulting character is written.
Conversion to an unsigned integer type is well defined; it's reduced modulo UCHAR_MAX+1
. Assuming UCHAR_MAX==255
(which it almost certainly is) the result of the conversion is (unsigned char)232
, so it will (attempt to) print character 232. The result is likely to depend on the locale. You're likely to run into problems with 232 (0xe8) being an invalid UTF-8 encoding.
printf("%s\n",1000);
%s
requires an argument of type char*
. so the behavior is undefined. (What's likely to happen in practice is that the value 1000
is treated as if it were a pointer. It's unlikely that that's a valid address, or that there's a valid string starting at that address.)
Using the wrong format specifier for printf invokes undefined behavior bugs, meaning that anything can happen. Pondering about why you get a certain kind of undefined behavior won't give you much in the way of meaningful knowledge, but anyway...
In the case printf("%s\n",1000);
you simply lie to the compiler and tell it that 1000
is a pointer to an allocated string of characters. It's hard to tell exactly what will happen - maybe the compiler will run off to address 1000 and attempt to access it, possibly resulting in a crash.
In the case of printf("%c\n",1000);
this is actually somewhat deterministic behavior, by accident. All arguments to variadic functions like printf get implicitly promoted. Had you passed a char
, it would have been promoted to int
and then converted back to char
.
But you pass the integer constant 1000
which is of type int
. As it happens, printf
expects an int
after the implicit promotion and it will try to convert 1000
"back" to a char
. This means that the compiler will grab the least significant byte of the int
and treat that as char
(or to be picky, as unsigned char
).
1000
== 0x000003E8
. The char
conversion will grab the least significant E8
byte. And then print whatever that character represents in the symbol table, if anything.
Please refer to the printf format specifiers: http://www.cplusplus.com/reference/cstdio/printf/
%c
- used to print a character (char
), which normally has a value between -128 to 127 (it can also be 0 to 255)
%s
- used to print an array of char
s with the last char as NULL
(\0
)
So printf("%c\n",1000);
exhibits implementation defined behaviour as 1000 is greater than 128 (or 255).
and printf("%s\n",1000);
is trying to print an array of chars located at address 1000 (which is most likely wrong). It is also undefined behaviour, so anything could happen.
If you are trying to print an integer you should use %d
, like such:
printf("%d\n",1000);