C Programming

What Does %s and %d Mean in printf – C Language

In C language, format specifiers are used to accept inputs and print a type’s output. Every format specifier uses the symbol %. The kind of data that must be provided as input and the type of data that must be displayed on the screen are both specified by format specifiers to the compiler. It is used to format and print output of a code. It takes a string of characters as an argument and includes various format specifiers and variable arguments.

The two most used format specifiers in C language are %s and %d.

What Does %s and %d Mean in printf in the C Language

Here, we will talk about the %s and %d specifiers in detail.

%s Format Specifier

A string of characters can be represented with the format specifier %s. The contents of a string must be enclosed within double quotes. This format specifier is used to display literal strings, text messages, and other textual data on the output device.

Let’s follow it with an example given below:

#include <stdio.h>
int main()
{
    char s[15]="Hello World";
    printf("The string value of s is %s \n",s);
    return 0;
}

Output

As in the above mentioned code, inside the main function an array of characters is stored and then it is printed using the %s specifier as it is used to print a string or sequence of characters in the printf() statement.

%d Format Specifier

%d is a format specifier used to represent integers. This format specifier is used to insert integer numbers into the output message. It is important to note that %d only accepts decimal integers, not octal or hexadecimal components.

#include <stdio.h>
int main()
{
    int num = 9;

    //print value using %d
    printf("Value of num using %%d is = %d\n", num);
    return 0;
}

Output

As in the above-mentioned code, inside the main function, 9 is stored in the variable ‘num’, and then the number is printed using the printf statement with %d specifier.

Now consider this example of using both the specifiers in the same code.

#include <stdio.h>

int main() {
  int num = 10;
  char my_name[] = "Abraham";
  printf("num = %d \n", num);
  printf("My name is %s", my_name);
  return 0;
}

Output

The %d format specifier instructs printf to insert the numerical value of the argument 10 into the output message, while the %s format specifier specifies that the entire string should be included in the output message.

When using printf, the format specifiers %s and %d are always followed by the variable arguments. These variable arguments supply the actual text or numbers to be shown on the output device. They must appear immediately after the format specifier and before the closing parenthesis.

Conclusion

Using printf effectively requires an understanding of format specifiers, how they are used, and how they interact with variable arguments. By using the %s and %d format specifiers, along with their variable arguments, you can include text and integer numbers in the output message. This offers a potent collection of capabilities for precisely presenting data on the output device.

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.