Formatting Strings ------------------ The simplest way to format an output to display a string is done like this: char msg[] = "hi there"; printf("The message is %s\n", msg); The output would be: The message is hi there If you wanted to justify the inserted string in the printed string, you could use a specifier for width like this: %ns Where n is the size of the string field width. If you wanted "hi there" to be spaced one more to the right then you could use the width to the string length + 1. The string length is 8, so the width specifier is %9s. The output would be: The message is hi there If you use the "-" sign, it will left justify the string like this: printf("The message is %-10s, so there.\n", msg); The output is: The message is hi there ,so there. As you can see, 2 extra spaces were inserted at the end of the string "hi there". If you want to specify how many characters of a string are displayed, this may be done by using the decimal notation (%n.ds). The value for 'd' is the number of characters from the left of the string that will be displayed. For example: printf("|%10.5s|\n", msg); This outputs: | hi th| The first 5 characters of "hi there" are right justified. The sprintf() command --------------------- The sprintf() command looks and is similar to the printf() command. This command simply may be used for string formatting to other string variables. For example, if you wanted to take 2 integers and make a string out of them, you could use the sprintf() command like this: sprintf(strout, "%d %d", num1, num2); If num1 and num2 are integers, they are printed into the string variable called strout like this: num1 num2 If num1 = 10 and num2 = -999 then the strout becomes: "10 -999" You may also do the reverse with a similar command to scanf() called sscanf(). The sscanf() command -------------------- The sscanf() command looks and is similar to the scanf() command. This command simply is the reverse of the sprintf() command. It essentially disassembles the string into separate variables. Let us perform the reverse operation on the string created above: sscanf(strout,"%d %d", &num1, &num2); This will take the string "10 -999" and assign 10 to num1 and -999 to num2.