J
jimjim
Hello,
I was wondering about the implications of giving as an argument to sprintf a
different data type from the one specified in the format argument. This type
of question along with some others are asked below:
1. #include <stdio.h>
int main(){
char buffer[20];
int j, t;
t = sprintf(buffer," %c ", 97 );
printf("char a in decimal is: %d\n\n", 'a');
printf("sprintf returns: %d \n", t);
for(j=0;j<t;j++)
printf("This is buffer[%d] == %d \n", j, buffer[j]);
return 0;}
Output:
char a in decimal is: 97
sprintf returns: 3
This is buffer[0] == 32
This is buffer[1] == 97
This is buffer[2] == 32
Why does sprintf return 3 and not 1?
On my machine, every ASCII character is essentially an one-byte integer
number. In this case, I would expect that integer 97, which maps to 'a',
would be stored to the byte buffer[0]. Instead, buffer[0] stores 32 which is
the space character!
2. In the above program, I changed the one statement that now reads:
t = sprintf(buffer," %c ", 300 );
buffer[1] is not 300, which I find reasonable as a signed char stores values
from -128 to +127. My question is why is it possible to store a value in
excess of +127 (or +255 for unsigned char), without receiving a warning
like: "overflow in implicit constant conversion ". (compiled with gcc 3. ...
.. ... and -Wall)
3. In the above program, I changed two statements that now read:
t = sprintf(buffer," %d ", 'a' );
printf("This is buffer[%d] == %c \n", j, buffer[j]);
Output:
char a in decimal is: 97
sprintf returns: 4
This is buffer[0] ==
This is buffer[1] == 9
This is buffer[2] == 7
This is buffer[3] ==
I can see that each digit of the integer 97 (character a) is stored to
different bytes in the buffer. Can you explain this behaviour please?
4. In the above program, I changed one statement that now reads:
t = sprintf(buffer," %d ", "hello" );
What happens in this case?
I have read the manual page regarding stdarg -- variable argument lists @
http://www.freebsd.org/cgi/man.cgi?...ropos=0&manpath=FreeBSD+5.4-RELEASE+and+Ports
However, it is still tough for me to read through the C library's
implementation of the sprintf. Answers to my questions with references to
the C library's implementation of the sprintf are more than wellcome.
TIA
I was wondering about the implications of giving as an argument to sprintf a
different data type from the one specified in the format argument. This type
of question along with some others are asked below:
1. #include <stdio.h>
int main(){
char buffer[20];
int j, t;
t = sprintf(buffer," %c ", 97 );
printf("char a in decimal is: %d\n\n", 'a');
printf("sprintf returns: %d \n", t);
for(j=0;j<t;j++)
printf("This is buffer[%d] == %d \n", j, buffer[j]);
return 0;}
Output:
char a in decimal is: 97
sprintf returns: 3
This is buffer[0] == 32
This is buffer[1] == 97
This is buffer[2] == 32
Why does sprintf return 3 and not 1?
On my machine, every ASCII character is essentially an one-byte integer
number. In this case, I would expect that integer 97, which maps to 'a',
would be stored to the byte buffer[0]. Instead, buffer[0] stores 32 which is
the space character!
2. In the above program, I changed the one statement that now reads:
t = sprintf(buffer," %c ", 300 );
buffer[1] is not 300, which I find reasonable as a signed char stores values
from -128 to +127. My question is why is it possible to store a value in
excess of +127 (or +255 for unsigned char), without receiving a warning
like: "overflow in implicit constant conversion ". (compiled with gcc 3. ...
.. ... and -Wall)
3. In the above program, I changed two statements that now read:
t = sprintf(buffer," %d ", 'a' );
printf("This is buffer[%d] == %c \n", j, buffer[j]);
Output:
char a in decimal is: 97
sprintf returns: 4
This is buffer[0] ==
This is buffer[1] == 9
This is buffer[2] == 7
This is buffer[3] ==
I can see that each digit of the integer 97 (character a) is stored to
different bytes in the buffer. Can you explain this behaviour please?
4. In the above program, I changed one statement that now reads:
t = sprintf(buffer," %d ", "hello" );
What happens in this case?
I have read the manual page regarding stdarg -- variable argument lists @
http://www.freebsd.org/cgi/man.cgi?...ropos=0&manpath=FreeBSD+5.4-RELEASE+and+Ports
However, it is still tough for me to read through the C library's
implementation of the sprintf. Answers to my questions with references to
the C library's implementation of the sprintf are more than wellcome.
TIA