Formatting and Styling Strings (C/C++)

So, talking about formatting and styling strings for Java/Android has got me thinking about formatting and styling strings for C/C++. For C/C++, we use printf and sprintf. Both printf and sprintf work exactly the same except sprintf puts the output to a character array and printf puts the output to standard output (such as the console/terminal).

If you just want to put a line out to standard output, you’d write something like this:

printf("Hello World!\n");

If you want to output strings and numbers, you’d use it like this:

float freqMHz = 100.2578;
printf("%-30s: %5.2f MHz\n", "Frequency", freqMHz);

The previous example is a bit more advanced, but that’s probably as advanced as it gets. %-30s means the first parameter is a string (the “s”) and we want to allot 30 spaces for the string and left align it (the “-“). The %5.2f means the second parameter is a float and we want to format it such that we want 5 digits for the integer part with a 2 digit precision for the decimal part. The string sent to standard output would be as follows:

Frequency:                    100.26 MHz

Note that the float gets rounded up when it is formatted. For sprintf, the syntax is the same, except we add a character array to the parameters. The character array is where we would save the final string. For example, the following code will output the exact same thing as the above example:

char* outputStr = new char[1024];
sprintf(outputStr, "%-30s: %5.2f MHz\n", "Frequency", freqMHz);

The cool thing about printf and sprintf is that you can add as many parameters as you want to the formatted string. The useful thing about printf and sprintf is that you can mix and match data types (like in the previous examples) without having to do typecasting or using (with cout) to get it to look nice. You can even format numbers to hexadecimal or scientific notation! Pretty slick, right?

Also, remember to add a newline character (“\n”) to the end of the string. It will run the next printf on the same line as the previous printf if you don’t.

Anyways, for all the different flags you can use to format printf and sprintf, check out the documentation at

    • kevingrant5
    • January 31st, 2011

    I never completely understood this until the other day, excellent topic.

    It works very similarly in Android. In your “strings.xml” file, format your string to look like this:

    Hello %1$s! You have %2$d new messages, yes, you really have %2$d new messages!.

    When you want to use this string and actually put the numbers in, put this in your code:

    Resources res = getResources();
    String text = String.format(res.getString(R.string.test_str), username, mailCount);

    %1$s means:
    %1: The first parameter sent through the function will be replaced here
    $s: The parameter being passed is a string

    %2$d means:
    %2: The second parameter passed in the function will be replaced here
    $d: The parameter being passed is a decimal number.

    You can call these as many times as you want in the string, as you can see we used “%2$d” twice, but it is using the same passed parameter.

    Hope that shed some light on string formatting!

    Most of this came from the Android website on String Resources

  1. May 17th, 2011

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: