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 9 2885
In article <d1**********@news.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 ch***********@hotmail.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. ch***********@hotmail.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;
}
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*****@mindspring.com> ??????:42*********@mindspring.com... ch***********@hotmail.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 ch***********@hotmail.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
In article <d1**********@news.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?? ch***********@hotmail.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(const char *s, ...);
static int fsput_s(const char *s);
static int fsput_u(unsigned 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(unsigned 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(const 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_arg(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 ch***********@hotmail.com wrote: put float to double put char to int why ?
If you will read my post
<Wn****************@newsread3.news.atl.earthlink.n et> 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.
"Martin Ambuhl" <ma*****@earthlink.net>
??????:XA****************@newsread3.news.atl.earth link.net... ch***********@hotmail.com wrote: put float to double put char to int why ?
If you will read my post <Wn****************@newsread3.news.atl.earthlink.n et> 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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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...
|
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;
|
by: arindam.mukerjee |
last post by:
I was running code like:
#include <stdio.h>
int main()
{
printf("%f\n", 9/5);
return 0;
}
|
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...
|
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++);
|
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...
|
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...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
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...
|
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
|
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...
|
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,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
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,...
| |