473,799 Members | 3,350 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

printf errrow ! same code differernt result between dev c++ and vc6.0

void show( char *s, ...) is a function seemd like prinf
code
--------------

#include <stdio.h>
#include <stdarg.h>

void show( char *s, ...)
{
va_list stage;
char *ps;
char pchar;
int pint;
double pdouble;
float pfloat;

va_start( stage, s);
{
while (*s)
{
switch (*s)
{
case '%':
{
s++;
switch (*s)
{
case 'f':
case 'F':
pdouble = va_arg(stage, float );
printf("%f", pdouble);
break;

case 'd':
case 'D':
pint = va_arg(stage, int );
printf("%d", pint);
break;
case 's':
case 'S':

ps = va_arg(stage, char* );
printf("%s", ps);
break;

case 'c':
case 'C':
pchar = va_arg(stage, char );
printf("%c", pchar);
break;

}
s++;
break;
}
default:
printf("%c",*s) ;
s++;
break;

}
}

}
}

int main()
{
int x =55;
char h ='a';
show("%c",h);
show("%c",h);
return 0;
}

---------------------------------------

under vc6.0 it can run well!

but under linux redhat7.x gcc error! win2k +dev c++ 4.98
errow !

case 'c':
case 'C':
pchar = va_arg(stage, char ); // here!!!
printf("%c", pchar);
break;
how to fix

Nov 14 '05 #1
9 2915
In article <d1**********@n ews.yaako.com>, <ch***********@ hotmail.com> wrote:
:void show( char *s, ...) is a function seemd like prinf
:code

: va_list stage;

: pdouble = va_arg(stage, float );

: pchar = va_arg(stage, char );

gcc warns about two things:

va.c: In function `show':
va.c:26: warning: `float' is promoted to `double' when passed through `...'
va.c:26: warning: (so you should pass `double' not `float' to `va_arg')
va.c:46: warning: `char' is promoted to `int' when passed through `...'
If you fix those two things then gcc will run the code.

I would hazard a guess that va_arg() is using sizeof()
and you happened to get an unaligned access when you
told it the wrong size.
--
'The short version of what Walter said is "You have asked a question
which has no useful answer, please reconsider the nature of the
problem you wish to solve".' -- Tony Mantler
Nov 14 '05 #2
ch***********@h otmail.com wrote:
void show( char *s, ...) is a function seemd like prinf
code
--------------

#include <stdio.h>
#include <stdarg.h>

void show( char *s, ...)
{
va_list stage;
char *ps;
char pchar;
int pint;
double pdouble;
float pfloat;

va_start( stage, s);
{
while (*s)
{
switch (*s)
{
case '%':
{
s++;
switch (*s)
{
case 'f':
case 'F':
pdouble = va_arg(stage, float );
printf("%f", pdouble);
break;

case 'd':
case 'D':
pint = va_arg(stage, int );
printf("%d", pint);
break;
case 's':
case 'S':

ps = va_arg(stage, char* );
printf("%s", ps);
break;

case 'c':
case 'C':
pchar = va_arg(stage, char );
printf("%c", pchar);
break;
You need a default here, as you otherwise run into trouble
for the string "%"; after reading '%', you skip over
'\0' without a default label (or a check against *s!=0)...
}
s++;
.... and then step over the '\0' to point to the next character.
Note that this is still legal -- it is evaluating *s which
can bring you into the realm of undefined behaviour (can because
you may be lucky in that the underlying storage s points to is
greater than strlen(s)+1 bytes and one of these bytes is '\0').

For the issues with arguments of variadic functions and va_arg,
Walter's post cleared that up.
Cheers
Michael
break;
}
default:
printf("%c",*s) ;
s++;
break;

}
}

}
}

int main()
{
int x =55;
char h ='a';
show("%c",h);
show("%c",h);
return 0;
}

---------------------------------------

under vc6.0 it can run well!

but under linux redhat7.x gcc error! win2k +dev c++ 4.98
errow !

case 'c':
case 'C':
pchar = va_arg(stage, char ); // here!!!
printf("%c", pchar);
break;
how to fix

--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Nov 14 '05 #3
ch***********@h otmail.com wrote:
[code snipped, see replacement below]
under vc6.0 it can run well!
It shouldn't.
but under linux redhat7.x gcc error! win2k +dev c++ 4.98
errow !
case 'c':
case 'C':
pchar = va_arg(stage, char ); // here!!!
printf("%c", pchar);
break;
how to fix


First turn your diagnostics on.

Your error is in not realizing that values are subject to default
promotions when passed to variadic functions. Try the following (my
changes are marked with /* mha ... */ comments):

/* mha: unused variables pfloat (in show) and x (in main) removed */

#include <stdio.h>
#include <stdarg.h>

void show(char *s, ...)
{
va_list stage;
char *ps;
char pchar;
int pint;
double pdouble;

va_start(stage, s);
{
while (*s)
switch (*s) {
case '%':
s++;
switch (*s) {
case 'f':
case 'F':
/* mha: because floats are promoted to doubles when
passed to variadic functions, the type in the next
line has been changed. */
pdouble = va_arg(stage, double /* was 'float' */ );
printf("%f", pdouble);
break;
case 'd':
case 'D':
pint = va_arg(stage, int);
printf("%d", pint);
break;
case 's':
case 'S':

ps = va_arg(stage, char *);
printf("%s", ps);
break;
/* mha: because chars are promoted to ints when
passed to variadic functions, the type in the next
line has been changed. */

case 'c':
case 'C':
pchar = va_arg(stage, int /* was 'char' */ );
printf("%c", pchar);
break;
}
s++;
break;
default:
printf("%c", *s);
s++;
break;

}
}
}

int main()
{
char h = 'a';
show("%c", h);
show("%c", h);
putchar('\n'); /* mha */
return 0;
}

Nov 14 '05 #4
put float to double
put char to int
why ?
before ask first question i fix the code like you
but i donot think it is right

i think sizeof (char ) != sizeof(int)
sizeof (float) != sizeof(double)

va_end(stage); // :) i lost it befor but i find the function still work
well, so .....

i want to read the printf() code!

can you post it? thank you
"pete" <pf*****@mindsp ring.com> ??????:42****** ***@mindspring. com...
ch***********@h otmail.com wrote:

void show( char *s, ...) is a function seemd like prinf
code
--------------

#include <stdio.h>
#include <stdarg.h>

void show( char *s, ...)
{
va_list stage;
char *ps;
char pchar;
int pint;
double pdouble;
float pfloat;

va_start( stage, s);


Where's va_end?
{


I don't like the extra braces.
It makes it harder to tell which switch is missing a default.
while (*s)
{
switch (*s)
{
case '%':
{
s++;
switch (*s)
{
case 'f':
case 'F':
pdouble = va_arg(stage, float );


Should be double instead of float.
printf("%f", pdouble);
break;

case 'd':
case 'D':
pint = va_arg(stage, int );
printf("%d", pint);
break;

case 's':
case 'S':

ps = va_arg(stage, char* );
printf("%s", ps);
break;

case 'c':
case 'C':
pchar = va_arg(stage, char );


Should be pint instead of pchar and int instead of char.
printf("%c", pchar);
break;

}
s++;
break;
}
default:
printf("%c",*s) ;
s++;
break;

}
}

}
}

int main()
{
int x =55;
char h ='a';
show("%c",h);
show("%c",h);
return 0;
}


/* BEGIN new.c */

#include <stdio.h>
#include <stdarg.h>

void show( char *s, ...)
{
va_list stage;
char *ps;
int pint;
double pdouble;

va_start( stage, s);
while (*s) {
switch (*s) {
case '%':
switch (*++s) {
case 'f':
case 'F':
pdouble = va_arg(stage, double);
printf("%f", pdouble);
break;
case 'd':
case 'D':
pint = va_arg(stage, int);
printf("%d", pint);
break;
case 's':
case 'S':
ps = va_arg(stage, char*);
printf("%s", ps);
break;
case 'c':
case 'C':
pint = va_arg(stage, int);
printf("%c", pint);
break;
default:
printf("%c", *s);
break;
}
break;
default:
printf("%c", *s);
break;
}
++s;
}
va_end(stage);
}

int main(void)
{
int x = 55;
char h = 'a';

show("%c", h);
show("%c", h);
putchar('\n');
show(
"%%f 9.0 is %f.\n%%d 9 is %d.\n"
"%%s nine is %s.\n%%c '9' is %c.\n",
9.0, 9, "nine", '9'
);
return 0;
}

/* END new.c */
--
pete

Nov 14 '05 #5
ch***********@h otmail.com wrote:
put float to double
put char to int
why ?
[Please keep some context of what you're replying to.]

The '...' arguments passed to variadic functions like printf are
undergo 'argument promotion'; float arguments are promoted to
double, char is generally promoted to int.
before ask first question i fix the code like you
but i donot think it is right

i think sizeof (char ) != sizeof(int)
sizeof (float) != sizeof(double)
This is irrelevant. You should learn C from a semantics point of
view, not from what or how a given machine implements C.
va_end(stage); // :) i lost it befor but i find the function
still work well, so .....
It may well work on your system, but the standard requires portable
programs to match each va_start with a corresponding va_end before
the function returns.

There have been systems where missing such a va_end will cause
problems.
i want to read the printf() code!

can you post it? thank you


Source code to printf is going to be different for each different
implementation. That said, just about every C development suite
these days comes with source code to the standard libraries.

For most, printf is implemented as a simple wrapper; it's usually
vprintf which does the hard work.

--
Peter

Nov 14 '05 #6
In article <d1**********@n ews.yaako.com>, <ch***********@ hotmail.com> wrote:
:put float to double
:put char to int
:why ?
:before ask first question i fix the code like you
:but i donot think it is right

;i think sizeof (char ) != sizeof(int)
; sizeof (float) != sizeof(double)

Effectively when you use ... as a placeholder in the function prototype
to signal a use of varargs, you are not declaring the type of each individual
item, so the compiler has to fall back to the old K&R argument calling
conventions, as if you were calling a routine that had no prototype.
The old calling conventions automatically pass floats as doubles, and
pass char as int, and short as int, so that's the behaviour you inherit.
--
Are we *there* yet??
Nov 14 '05 #7
ch***********@h otmail.com wrote:

put float to double
put char to int
why ?
%c is supposed to take an int argument in a printf call.
printf("%f", pdouble);
%f takes a double argument in a printf call.
va_end(stage); // :) i lost it befor
but i find the function still work well, so .....
"Nothing quite encourages as does one's first unpunished crime."
http://fr.ebookslib.com/?a=sa&b=1089
i want to read the printf() code!

can you post it? thank you


min_printf is as far as I've gone
trying to code something like printf.

/* BEGIN min_printf.c */

#include <stdarg.h>
#include <stdio.h>
/*
** Only 5 features from stdio.h,
** are used in min_printf.c:
**
** 1 putc(c, stream)
** 2 stdout
** 3 FILE
** 4 EOF
** 5 int ferror(FILE *stream);
*/
#define put_c(c, stream) (putc((c), (stream)))
#define put_char(c) (put_c((c), stdout))
#define fsput_char(c) \
(put_char(c) == EOF && ferror(stdout) != 0 ? EOF : 1)

int min_printf(cons t char *s, ...);

static int fsput_s(const char *s);
static int fsput_u(unsigne d integer);
static int fsput_u_plus_1( unsigned integer);
static int fsput_d(int integer);

int main(void)
{
min_printf(
"\n%%u 9u is %u.\n%%d 9 is %d.\n"
"%%s \"nine\" is %s.\n%%c '9' is %c.\n",
9u, 9, "nine", '9'
);
return 0;
}

static int fsput_s(const char *s)
{
int count;

for (count = 0; *s != '\0'; ++s) {
if (put_char(*s) == EOF && ferror(stdout) != 0) {
return EOF;
}
++count;
}
return count;
}

static int fsput_d(int integer)
{
int count;

if (0 > integer) {
if (put_char('-') != EOF) {
count = fsput_u_plus_1(-(integer + 1));
if (count != EOF) {
++count;
}
} else {
count = EOF;
}
} else {
count = fsput_u(integer );
}
return count;
}

static int fsput_u(unsigne d integer)
{
int count;
unsigned digit, tenth;

tenth = integer / 10;
digit = integer - 10 * tenth + '0';
count = tenth != 0 ? fsput_u(tenth) : 0;
return count != EOF && put_char(digit) != EOF ? count + 1 : EOF;
}

static int fsput_u_plus_1( unsigned integer)
{
int count;
unsigned digit, tenth;

tenth = integer / 10;
digit = integer - 10 * tenth + '0';
if (digit == '9') {
if (tenth != 0) {
count = fsput_u_plus_1( tenth);
} else {
count = put_char('1') == EOF ? EOF : 1;
}
digit = '0';
} else {
count = tenth != 0 ? fsput_u(tenth) : 0;
++digit;
}
return count != EOF && put_char(digit) != EOF ? count + 1 : EOF;
}

int min_printf(cons t char *s, ...)
{
int count, increment;
va_list ap;

va_start(ap, s);
for (count = 0; *s != '\0'; ++s) {
if (*s == '%') {
switch (*++s) {
case 'c':
increment = fsput_char(va_a rg(ap, int));
break;
case 'd':
increment = fsput_d(va_arg( ap, int));
break;
case 's':
increment = fsput_s(va_arg( ap, char *));
break;
case 'u':
increment = fsput_u(va_arg( ap, unsigned));
break;
default:
increment = fsput_char(*s);
break;
}
} else {
increment = fsput_char(*s);
}
if (increment != EOF) {
count += increment;
} else {
count = EOF;
break;
}
}
va_end(ap);
return count;
}

/* END min_printf.c */
--
pete
Nov 14 '05 #8
ch***********@h otmail.com wrote:
put float to double
put char to int
why ?
If you will read my post
<Wn************ ****@newsread3. news.atl.earthl ink.net> you will see that
I have answered this.
before ask first question i fix the code like you
but i donot think it is right

i think sizeof (char ) != sizeof(int)
sizeof (float) != sizeof(double)


There is no guarantee that either of these is true. They are also not
relevant.
Nov 14 '05 #9

"Martin Ambuhl" <ma*****@earthl ink.net>
??????:XA****** **********@news read3.news.atl. earthlink.net.. .
ch***********@h otmail.com wrote:
put float to double
put char to int
why ?


If you will read my post
<Wn************ ****@newsread3. news.atl.earthl ink.net> you will see that
I have answered this.
before ask first question i fix the code like you
but i donot think it is right

i think sizeof (char ) != sizeof(int)
sizeof (float) != sizeof(double)


There is no guarantee that either of these is true. They are also not
relevant.

Nov 14 '05 #10

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

Similar topics

5
2600
by: Peter Ammon | last post by:
It's my understanding that the printf() function is declared as int printf(const char * restrict format, ...); in stdio.h. And loosely speaking, if a parameter is declared as restricted, then accesses to the object must go through that parameter. Does this mean that printf("%s", "%s");
31
2195
by: muralipmanohar | last post by:
Hello all , I need a help on this code kindly help me out for the below code I worked on the Turboc the result I was expecting was different from what has been printed I have indicated the line with " /*this one*/" line no 10 I did expected some junk for the first %ld and 6553 for the second %d ,but it printed in different way I am also pasting the result printed , it is below the code . I am totally confused because I got one answer...
29
17630
by: whatluo | last post by:
Hi, c.l.cs I noticed that someone like add (void) before the printf call, like: (void) printf("Timeout\n"); while the others does't. So can someone tell me whether there any gains in adding (void) to printf. Thanks, Whatluo.
3
2024
by: nandh_dp | last post by:
When the below program is compiled and executed, #include <stdio.h> #define MASK 0xFFFFFFULL main() { unsigned long long a; unsigned long b, c;
17
2519
by: arindam.mukerjee | last post by:
I was running code like: #include <stdio.h> int main() { printf("%f\n", 9/5); return 0; }
15
2511
by: bryanvick | last post by:
I just started learning C, and wrote this small program to play around with the printf function in stdio.h. At the console, I am able to type input at the first 2 getchar() calls, but when I call it again at big = getchar(), I never get to type anything, it just reads ASCII 10 from the stdin for some reason. Can someone explain why it is reading the stdin stream at that point even though I am not typing, and how I would get this to work....
20
1815
by: sam_cit | last post by:
Hi Everyone, I have the following code, int main() { int p = {10,20,30,40,50}; int *i = &p; printf("before : %p\n",(void*)i); printf("1 %d %d\n",*++i,*i++);
10
1885
by: indigodfw | last post by:
Hi experts Consider this piece of code unsigned long data = 124; /* Data is actually part of a big struct but to simplify */ printf(" %-5llu ", (unsigned long long)(data*1000)); <memory address say 60000000>: 00000000 00000D77 00000000
4
2267
by: banansol | last post by:
I read the thread Floating point rounding error and I saw Richard Heathfield's description with several lines like: 0.1 = 1/2 = 0.5 - too large 0.01 = 1/4 = 0.25 - too large 0.001 = 1/8 = 0.125 - too large .... And I wanted to write such a program that output lines like that. My attempt is below, and I don't
0
9688
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10490
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10259
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10030
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9077
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7570
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6809
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5467
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3761
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.