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