How do you get type from format?
-
I am learning how to use ellipsis in function. Started with putting "int" as parameters, with first entry being count of parameters passed to he function. Now I want to pass multiple types , not just int , to the function. The following snippet will do, but... How do you get "type" from passing a char * - in this case "format" variable? What did I missed ? Thanks for your help. Vaclav I am not sure if I tagged the code properly.
void my_printf( char *format, ... ) {
va_list argptr;va_start( argptr, format );
while( *format != '\0' ) {
// string
if( *format == 's' ) {
char* s = va_arg( argptr, char * );
printf( "Printing a string: %s\n", s );
}
// character
else if( *format == 'c' ) {
char c = (char) va_arg( argptr, int );
printf( "Printing a character: %c\n", c );
break;
}
// integer
else if( *format == 'd' ) {
int d = va_arg( argptr, int );
printf( "Printing an integer: %d\n", d );
}\*format++;
}
va_end( argptr );
} -
I am learning how to use ellipsis in function. Started with putting "int" as parameters, with first entry being count of parameters passed to he function. Now I want to pass multiple types , not just int , to the function. The following snippet will do, but... How do you get "type" from passing a char * - in this case "format" variable? What did I missed ? Thanks for your help. Vaclav I am not sure if I tagged the code properly.
void my_printf( char *format, ... ) {
va_list argptr;va_start( argptr, format );
while( *format != '\0' ) {
// string
if( *format == 's' ) {
char* s = va_arg( argptr, char * );
printf( "Printing a string: %s\n", s );
}
// character
else if( *format == 'c' ) {
char c = (char) va_arg( argptr, int );
printf( "Printing a character: %c\n", c );
break;
}
// integer
else if( *format == 'd' ) {
int d = va_arg( argptr, int );
printf( "Printing an integer: %d\n", d );
}\*format++;
}
va_end( argptr );
}That code looks like it should broadly work, but you are assuming any 's', 'c', or 'd' in the string is a format identifier. Your main loop should iterate the characters of the format string, copying them to the output until you see a format specifier. When you see a '%' character then you know that is the start of a formatting sequence so you need to get the width, precision and type details. Once you have extracted all those values, then you can process the next argument according to the type field character.
-
That code looks like it should broadly work, but you are assuming any 's', 'c', or 'd' in the string is a format identifier. Your main loop should iterate the characters of the format string, copying them to the output until you see a format specifier. When you see a '%' character then you know that is the start of a formatting sequence so you need to get the width, precision and type details. Once you have extracted all those values, then you can process the next argument according to the type field character.
My feeling exactly. I this whoever posted the code missed something. Here is their sample use of the function. int main( void ) { my_printf( "sdc", "This is a string", 29, 'X' ); return( 0 ); } This code displays the following output when run: Printing a string: This is a string Printing an integer: 29 Printing a character: X Now where is the "sdc" print? And I thought that "format" was some kind of special feature. I guess I ditch this, maybe I can figure out how to use templates. Thanks
-
My feeling exactly. I this whoever posted the code missed something. Here is their sample use of the function. int main( void ) { my_printf( "sdc", "This is a string", 29, 'X' ); return( 0 ); } This code displays the following output when run: Printing a string: This is a string Printing an integer: 29 Printing a character: X Now where is the "sdc" print? And I thought that "format" was some kind of special feature. I guess I ditch this, maybe I can figure out how to use templates. Thanks
-
My feeling exactly. I this whoever posted the code missed something. Here is their sample use of the function. int main( void ) { my_printf( "sdc", "This is a string", 29, 'X' ); return( 0 ); } This code displays the following output when run: Printing a string: This is a string Printing an integer: 29 Printing a character: X Now where is the "sdc" print? And I thought that "format" was some kind of special feature. I guess I ditch this, maybe I can figure out how to use templates. Thanks
As per what Richard said READ THE MANUAL. "sdc" is the format specifier for the three parameters that follow s = STRING d = Signed decimal integer c = Character "This is a string" they want displayed as a string 29 they want displayed as a decimal X they want displayed as a character Try changing changing "sdc" to "s%.3fc" and watch the output %.3f is a complex format it means write value as a float to 3 decimal places. So we still have 3 instructions s + %.3f + c So the first string is a FORMAT STRING it tells the print function what to do with the following data passed in.
In vino veritas
-
As per what Richard said READ THE MANUAL. "sdc" is the format specifier for the three parameters that follow s = STRING d = Signed decimal integer c = Character "This is a string" they want displayed as a string 29 they want displayed as a decimal X they want displayed as a character Try changing changing "sdc" to "s%.3fc" and watch the output %.3f is a complex format it means write value as a float to 3 decimal places. So we still have 3 instructions s + %.3f + c So the first string is a FORMAT STRING it tells the print function what to do with the following data passed in.
In vino veritas
Thanks for reply, figured it out myself, but did not have time to reply. As far as your first sentence goes, take a refresher course and read "how to answer the question". You reply was great, but you do not have to be so snippy about it. GOT MY POINT? Have a swell day.