Formatted Output in C



In the C programming language, output functions perform the essential task of displaying or storing information in a well-formatted manner. The Standard Library <stdio.h> provides functions that allow programmers to control how data is presented. Among these, the three most commonly used functions are −

  • printf() − Displays formatted output on the standard output (console).
  • sprintf() − Stores formatted output in a string buffer (memory).
  • fprintf() − Writes formatted output to a file stream.

Although these functions share a common structure and formatting style, each serves a distinct purpose depending on the output target or destination. First, we will look at the common format specifiers used in these functions, and then we will discuss each function in detail with examples.

Following are the common format specifiers −

  • %d or %i: Integer
  • %f: Floating-point number
  • %c: Character
  • %s: String
  • %x or %X: Hexadecimal
  • %o: Octal
  • %%: Literal percent sign

printf() - Printing to the Console

In C, printf is the most commonly used function to display information on the standard output device (usually the terminal or console). It is used for debugging, user interaction, and displaying program results.

The syntax of the printf() function is as follows −

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

Here, format is a string that contains text and format specifiers, which are placeholders for the values to be printed. The ellipsis (...) indicates that the function can take a variable number of arguments corresponding to the format specifiers.

Example of using printf()

In this C example, we demonstrate the working of the printf() function −

#include <stdio.h>

int main() {
   int age = 25;
   float height = 5.8;
   char grade = 'A';
   char name[] = "Aman Kumar";

   printf("Name: %s\n", name);
   printf("Age: %d years\n", age);
   printf("Height: %.1f feet\n", height);
   printf("Grade: %c\n", grade);

   return 0;
}

Following is the output of the code −

Name: Aman Kumar
Age: 25 years
Height: 5.8 feet
Grade: A

sprintf() - Storing in a String Buffer

In C, sprintf() function is used to format and store a series of characters in a string buffer. It is similar to printf(), but instead of printing to the console, it writes the formatted output to a character array (string).

The syntax of the sprintf() function is as follows −

int sprintf(char *str, const char *format, ...);

Here, str is a pointer to the character array where the formatted string will be stored, format is a string containing text and format specifiers, and the ellipsis (...) indicates that the function can take a variable number of arguments corresponding to the format specifiers.

Example of using sprintf()

In this C example, we demonstrate the working of the sprintf() function −

#include <stdio.h>

int main() {
   int age = 25;
   float height = 5.8;
   char grade = 'A';
   char name[] = "Aman Kumar";
   char buffer[100];

   sprintf(buffer, "Name: %s\nAge: %d years\nHeight: %.1f feet\nGrade: %c\n",
           name, age, height, grade);

   // Print the formatted string stored in buffer
   printf("%s", buffer);

   return 0;
}

Following is the output of the code −

Name: Aman Kumar
Age: 25 years
Height: 5.8 feet
Grade: A

fprintf() - Writing to a File

In C, fprintf() function is used to format and write a series of characters to a file stream. It is similar to printf(), but instead of printing to the console, it writes the formatted output to a file.

The syntax of the fprintf() function is as follows −

int fprintf(FILE *stream, const char *format, ...);

Here, stream is a pointer to a FILE object that identifies the file where the formatted output will be written, format is a string containing text and format specifiers, and the ellipsis (...) indicates that the function can take a variable number of arguments corresponding to the format specifiers.

Example of using fprintf()

In this C example, we demonstrate the working of the fprintf() function −

#include <stdio.h>
int main() {
   FILE *file = fopen("output.txt", "w");
   if (file == NULL) {
      printf("Error opening file!\n");
      return 1;
    }

   int age = 25;
   float height = 5.8;
   char grade = 'A';
   char name[] = "Aman Kumar";

   fprintf(file, "Name: %s\n", name);
   fprintf(file, "Age: %d years\n", age);
   fprintf(file, "Height: %.1f feet\n", height);
   fprintf(file, "Grade: %c\n", grade);

   fclose(file);
   return 0;
}

This code will create a file named output.txt with the following content.

Name: Aman Kumar
Age: 25 years
Height: 5.8 feet
Grade: A

Conclusion

In this article, we have discussed the formatted output functions in C programming language. These functions are essential for displaying and storing information in a structured manner. By using format specifiers, we can control how different data types are represented in the output. The printf(), sprintf(), and fprintf() functions provide flexibility in directing output to various destinations, such as the console, memory buffers, or files. Understanding these functions is crucial for effective C programming and data presentation.

Advertisements