Thread: Strange printf function

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    182

    Strange printf function

    How does the following printf function work?
    Code:
      printf("%s\"%s\"\n%s%ld\n%s\"%s\"\n%s%ld\n",
             "The original string is ", string,
             "The converted value is ", x,
             "The remainder of the original string is ",
             remainderPtr,
             "The converted value plus 567 is ", x + 567 );
    All the data types to display are entered before hand and all the text is entered afterwards. I've never seen this before.




    Complete code below if needed
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
      long x;
      const char *string = "-1234567abc";
      char *remainderPtr;
    
      x = strtol( string, &remainderPtr, 0 );
      printf("%s\"%s\"\n%s%ld\n%s\"%s\"\n%s%ld\n",
             "The original string is ", string,
             "The converted value is ", x,
             "The remainder of the original string is ",
             remainderPtr,
             "The converted value plus 567 is ", x + 567 );
      return 0;
    }

  2. #2
    Beautiful to C Aia's Avatar
    Join Date
    Jun 2007
    Posts
    124
    Would you understand this?:
    Code:
    printf( "&#37;s", "Hello World" );
    That's the same thing you have there. Pay attention to the comas.
    Last edited by Aia; 03-03-2008 at 10:20 PM. Reason: unimportant
    When the eagles are silent, the parrots begin to jabber. ~Winston Churchill

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Code:
      printf("%s\"%s\"\n%s%ld\n%s\"%s\"\n%s%ld\n",
             "The original string is ", string,
             "The converted value is ", x,
             "The remainder of the original string is ",
             remainderPtr,
             "The converted value plus 567 is ", x + 567 );
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  4. #4
    Registered User
    Join Date
    Jan 2008
    Posts
    58
    How does the following printf function work?
    It works the same way as if you used variables.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
        // Define all of the output strings
        const char *originalMsg = "The original string is ";
        const char *convertedMsg = "The converted value is ";
        const char *remainderMsg = "The remainder of the original string is ";
        const char *convertedAddMsg = "The converted value plus 567 is ";
    
        long x;
        const char *string = "-1234567abc";
        char *remainderPtr;
    
        x = strtol( string, &remainderPtr, 0 );
        printf("%s\"%s\"\n%s%ld\n%s\"%s\"\n%s%ld\n",
            originalMsg, string,
            convertedMsg, x,
            remainderMsg, remainderPtr,
            convertedAddMsg, x + 567 );
    
        return 0;
    }
    It's a good idea to separate the text like that if you plan on translating the program to a different language. It makes it easier to find and fix the text by changing it all in one place instead of looking around for every printf call.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
        // Define all of the output strings
        const char *originalMsg = "Le string est original: ";
        const char *convertedMsg = "La valeur est convertie: ";
        const char *remainderMsg = "Le reste de la chaîne est originale: ";
        const char *convertedAddMsg = "567 ajoutée à la valeur est convertie: ";
    
        long x;
        const char *string = "-1234567abc";
        char *remainderPtr;
    
        x = strtol( string, &remainderPtr, 0 );
        printf("%s\"%s\"\n%s%ld\n%s\"%s\"\n%s%ld\n",
            originalMsg, string,
            convertedMsg, x,
            remainderMsg, remainderPtr,
            convertedAddMsg, x + 567 );
    
        return 0;
    }

  5. #5
    Registered User
    Join Date
    May 2006
    Posts
    182
    Wow, I feel dumb. Thanks for the help guys.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 03-05-2009, 10:25 AM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  3. segmentation fault upon reload
    By yabud in forum C Programming
    Replies: 8
    Last Post: 12-18-2006, 06:54 AM
  4. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM
  5. qt help
    By Unregistered in forum Linux Programming
    Replies: 1
    Last Post: 04-20-2002, 09:51 AM