C Programming

Creating a C Formatted Strings

For many years, software developers have relied heavily on the C language, one of the world’s oldest and most popular programming languages. One of the most important functions of the language is its ability to create formatted strings, or strings that are formatted in a specific way.

It is easy to create a formatted string in C language by performing a few basic steps. Follow this article’s guidelines for more details.

Create a C Formatted String

The first step is to identify the variables that will be used in the string. Variables are storage spaces for specific types of data, including strings, integers, and other data structures. Moreover, they can be used to refer to memory addresses. Once the variables have been identified and declared, the programmer can begin to write the format string.

When creating a formatted string in C language, it is important to remember that a string consists of an array of characters that contain a series of characters arranged sequentially. The amount of characters in a string determines how long it is. The formatting of the strings begins with the % symbol, which is used to signify the start of a variable in the string. Variables in C are typically identified by a single letter, such as “i” for an integer or “s” for a string. This letter is followed by a type specifier (i.e. the type of data that should be printed), as well as an optional set of modifiers. For example, a string printed as an integer could be identified as “%i“, while a string printed as a floating-point number is identified as “%f“.

Below is the list of format specifiers C language uses.

Format Specifier Description
%d or %i It is used to print the signed integer value, where signed integer denotes that the variable may store both positive and negative values.
%u It is used to display unsigned integer values, which only allow positive values to be stored in the variable.
%o It is used to print octal unsigned integers, which always begin with a value of 0.
%x The hexadecimal unsigned integer value, which always begins with a 0x value, is printed using this method. A, B, C, and other alphabetical characters are printed like this in small letters.
%X It is used to print hexadecimal unsigned integers, but %X also writes uppercase letters like A, B, and C.
%f The decimal floating-point values are printed using it. The six values that follow “.” are printed by default.
%lf Used to print floating-point numbers(double)
%e or %E For scientific notation, it is employed. It also goes by the names Mantissa and Exponent.
%g It employs fixed precision and is used to print decimal floating-point numbers, thus the value after the decimal in the input would match the value in the output perfectly.
%p The address is printed using hexadecimal notation.
%c Printing the unsigned character is done using it.
%s It is applied to print the strings.
%% Used to print a literal percent sign.
% d Used to print a space for positive numbers.
%zd Used to print the size (bytes) of a data type.

Look at an example code to use format specifiers below.

#include <stdio.h>

int main() {
    int i = 45;
    char c = 'a';
    float f = 3.353;
    double d = 4.23;
    char s[] = "Linuxhint!";

    printf("Integer: %d\n", i);
    printf("Character: %c\n", c);
    printf("Floating point (float): %f\n", f);
    printf("Floating point (double): %lf\n", d);
    printf("String: %s\n", s);
    printf("Hexadecimal (int): %x\n", i);
    printf("Octal (int): %o\n", i);
    printf("Unsigned (int): %u\n", i);
    printf("Scientific notation (float): %e\n", f);
    printf("Scientific notation (double): %le\n", d);
    printf("Pointer (memory address of i): %p\n", &i);
    printf("Percent sign: %%\n");
    printf("Float value of f is: %g", f);
    printf("Space for positive numbers (int): % d\n", i);
    printf("Size (bytes) of int: %zd\n", sizeof(i));
    printf("The hexadecimal representation of %d is: %X\n", i, i);

    return 0;
}

In the above code, all the basic format specifiers are used and the values of integer, character, float, double, and string are printed using these specifiers.

Output

Conclusion

Creating a formatted string in C language is a quick and efficient way to combine multiple variables into one string and make it readable for the user. It is a crucial tool for every programmer who wants to present data in a structured manner. With a few simple steps and the “printf” function, any programmer can create a formatted string in C language.

About the author

Hiba Shafqat

I am a Computer Science student and a committed technical writer by choice. It is a great pleasure to share my knowledge with the world in which I have academic expertise.