C String Format – A beginner’s guide

c string to intC is a general purpose and procedural computer programming language. It is a very popular language with powerful capabilities. C is widely used for system programming. In fact, the UNIX operating system was written in C. The language is easy to learn and runs on a variety of computer platforms. It is a structured program that can handle low level programming. There exist many C’ compilers today that compile C code to machine language code. Many programming languages are the descendants of the C language. Now that we’ve got you all pepped up about C, let’s actually go see some

In this beginner’s level tutorial, we walk you through how to format strings in the C programming language. We assume you have a basic knowledge of programming concepts. If you’re new to C, you may want to first take this beginners course on C programming.

What is a string

In the C programming language a string is basically any text. If stored in a variable, it is represented as an array of characters which terminates with the NULL character ”. C provides a host of functions to perform operations on strings. These include functions to calculate the length of a string, concatenate two or more strings, compare strings and more. We’ll take a look at some of these.

Formatting strings using the printf() function

printf() is a useful function which comes from the standard library of functions accessible by C programs. To use the printf() function in C programs you have to include the “stdio.h” header file. Printf(lets you print strings or other variables, in any format you want, on to the screen.  The syntax is as follows

int printf(const char *format, ...)

This function writes output to the standard output string stdout according to a specified format.

  • %s is the format specifier for strings.
  • %i or %d is the format specifier for integer variables.
  • %c is the format specifier for characters.
  • %f is the format specifier for floating points.

The printf() function returns the number of characters printed. In case of error(s), it returns a negative number. You can learn more about using the printf function with strings in this Udemy.com course.

A simple C program using printf() function and strings

#include <stdio.h>

main()

{

printf(“Welcome to C Programming\n”);

}

The command “#include <stdio.h>” tells the C compiler to include the stdio.h header file in the program. The printf() function is a part of stdio.h file. Hence the file must be included to use printf() function in a C program. main() function initiates every C program. Hence it is mandatory. The program code is placed inside the curly braces of the main() function. Here main() calls the printf() function to print the string “Welcome to C Programming\n” on the screen. Here “\n” is an invisible escape character which prints new line. Each statement in C is terminated by a semicolon as shown in the above program. Learn how to write your own basic C programs with this course.

How to modify the %s format specifier

A format flag begins with the % character. After the %, one of the following can be put prior to the conversion character.

  • Flag that modifies the specification for %s
  • A number which specifies the minimum field width.
  • A period which separates field width from precision
  • A number which specifies the precision length

We walk you through a few simple examples which illustrates different ways of formatting strings using printf() function.

Example 1:  Simple string format

printf("%s\n", " Good Morning");

The output of this would be Good Morning

Example 2:  String format which specifies the minimum field width

printf(“:%15s:\n”, “Good Morning”);

Here the string is printed with whitespaces to make it a total of 15 characters. This command ensures that 15 characters are printed. In case the string length is less, whitespaces are used to make it  a total of 15 characters.

The output of this would be :   Good Morning:

In this example the specified field width is 15. However the string is only 12 characters long. Hence three whitespaces are printed before the string is printed.

Example 3:  String format which specifies the precision length

 printf(“:%.10s:\n”, “Good Morning”);

This statement prints only 10 characters of the string. Here the string has a total of 12 characters which includes the space character. Hence the output will omit the last two characters.

The output looks like this – :Good Morni:

Here the precision length is 10. Hence only 10 characters of the string is printed. Note that the ‘.’ character precedes the precision length value.

Example 4:  String format which specifies the ‘-‘  left adjustment operator

printf(“:%-10s:\n”, “Good Morning”);

Here the statement prints at least 10 characters. In case the string is shorter than 10 characters space characters are added at the end.

The output looks like this – :Good Morni:

Here the minimum field width is 10. However the string passed has 12 characters. Hence the last two characters of the string are not printed.

Example 5:  String format which specifies the minimum field width and the precision length

printf(“:%15.10s\n”, “Good Morning”);

Here the statement has 15 characters as width. If the string is shorter than 15, space characters are used at the beginning. Also as the precision length is 10, not more than 10 characters of the relevant string is printed.

The output looks like this :    Good Morn:

The minimum field width is 15. So the output will have that length. However the precision length is 10. Hence only 10 characters of the string will be printed with 5 leading space characters making up the total field width of 15.

Example 6:  String format which specifies the minimum field width and the precision length along with the left adjustment flag.

 printf(“:%-15.10s:\n”, “Good Morning”);

This is similar to the previous example except that the space characters come at the end. Notice the ‘-‘ left adjustment flag.

The output looks like this :Good Morni    :

Here there are 3 trailing white space characters.

Hope this article helped you understand how to format strings. Programming is best learnt by writing your own programs. Feel free to experiment with the code to try out different kinds of string manipulation. At any time, for more details, feel free to refer to this advanced course on the C programming language.