473,396 Members | 1,940 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

queries about sprintf

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?q...EASE+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
Nov 15 '05 #1
1 3495
On Tue, 19 Jul 2005 10:41:55 GMT, "jimjim" <ne*****@blueyonder.co.uk>
wrote:
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:
Look in your reference for the default promotions that are
automatically performed on the arguments of a call to a variadic
function.

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?
What is the meaning of the value returned by sprintf? Are you
confusing it with the value returned by sscanf?
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!
Look at your format string in the call to sprintf. What is the first
character you specified to be stored in buffer? In which element of
buffer should this character be stored?

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)
Because the C language is designed for people who know what they are
doing and does not try to second guess the programmer's intent. Cases
where the programmer violates this assumption are relegated to
"undefined behavior".

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?
Buffer does not store digits. It stores characters. How many
characters can fit in a byte?

4. In the above program, I changed one statement that now reads:
t = sprintf(buffer," %d ", "hello" );

What happens in this case?
Undefined behavior. You told sprintf to expect an int and then gave
it a char*. How is sprintf to know you are lying or incompetent (or
maybe just experimenting)?


I have read the manual page regarding stdarg -- variable argument lists @
http://www.freebsd.org/cgi/man.cgi?q...EASE+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.


Do not read any particular implementation or documentation unless you
are only interested in the behavior of that implementation. There are
pdf copies of the draft standard available on the web. That is the
definition of the language and the standard library functions.
<<Remove the del for email>>
Nov 15 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Pete | last post by:
After realising how easy it is for a malicious user to inject an sql query into a paramenter for a query, e.g: $query = "SELECT name FROM employees WHERE ID = ".$HTTP_GET_VARS And the user...
13
by: Yodai | last post by:
Hi all.... I have a little problem that's driving me nuts. I can't seem to make any sense of it. I have this small webserver that substitutes some data from a page when finds a substitution...
3
by: huey_jiang | last post by:
Hi All, I am trying to figure out a right syntax to convert an integer array into hex array. sprintf worked for me on doing single integer: int i, Iarray, n=15; char buf; sprintf(buf,...
6
by: jt | last post by:
I need to produce 1 character array from 3 others. I tried sprintf and it terminates on the first 0, null, 0x00 it sees in tmp data. All 3 args print out nice by themselves. By trying to make...
2
by: aap | last post by:
I have the following code #define MAX 32 struct A { char carr; int iarr; int i; }; void main() {
9
by: Neal Barney | last post by:
I have a C program which runs on a device using a Zilog Z180 microprocessor. While it can address 1MB of RAM, it can only address 64KB at any given time. And of that only 16KB can be used for...
12
by: Henryk | last post by:
Hey there, I have some problems with the following code snippet on a Virtex-4 PowerPC with a GCC based compiler char chData; sprintf(&chData, "%+05.0f", -0.038f); --I get "-000" ???...
15
by: krister | last post by:
Hello, I'm working in a quite large system that has some limitations. One of those is that I can't use printf() to get an output on a screen. I'm forced to use a special function, let's call it...
3
by: google | last post by:
Consider the following code: char str; char str2; strcpy(str, "%alfa% %beta% d%100%d %gamma% %delta%"); printf("printf: "); printf("1%s2", str); printf("\nsprintf: "); sprintf(str2, "1%s2",...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.