Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. How do you get type from format?

How do you get type from format?

Scheduled Pinned Locked Moved C / C++ / MFC
helptutorialquestionlearning
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    Vaclav_
    wrote on last edited by
    #1

    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 );
    }

    L 1 Reply Last reply
    0
    • V Vaclav_

      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 );
      }

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      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.

      V 1 Reply Last reply
      0
      • L Lost User

        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.

        V Offline
        V Offline
        Vaclav_
        wrote on last edited by
        #3

        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

        L L 2 Replies Last reply
        0
        • V Vaclav_

          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

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          Or you could just read the proper documentation: Format Specification Syntax: printf and wprintf Functions[^].

          1 Reply Last reply
          0
          • V Vaclav_

            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

            L Offline
            L Offline
            leon de boer
            wrote on last edited by
            #5

            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

            V 1 Reply Last reply
            0
            • L leon de boer

              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

              V Offline
              V Offline
              Vaclav_
              wrote on last edited by
              #6

              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.

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups