Table of contents
1.
Introduction
1.1.
Formatted Output Using printf()
1.2.
Syntax
1.3.
Format Specifiers
1.4.
Example
1.5.
C
2.
Formatted Input Using scanf()
2.1.
Syntax
2.2.
Format Specifiers
2.3.
Example
2.4.
C
3.
Handling Strings and Characters
3.1.
Single Characters
3.2.
Strings
4.
Frequently Asked Questions
4.1.
How do I handle spaces in strings using scanf()? 
4.2.
What are some common errors when using scanf()?
4.3.
How do I prevent buffer overflow when using scanf()? 
5.
Conclusion
Last Updated: Aug 25, 2024
Easy

Formatted Input and Output in C

Author Ravi Khorwal
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Formatted input and output in C are essential for displaying data in a readable format and capturing user input efficiently. These operations are handled using the printf() and scanf() functions. 

Formatted Input and Output in C

This article covers the syntax, examples, key features, and common issues related to these functions, ensuring an easy-to-understand guide for beginners.

Formatted Output Using printf()

The printf() function is used to display formatted output on the screen.

Syntax

printf("format string", argument1, argument2, ...);

 

  • format string: A string that contains text and format specifiers.
     
  • arguments: The values that replace the format specifiers.

Format Specifiers

  • %d - Integer
     
  • %f - Floating-point number
     
  • %c - Character
     
  • %s - String

Example

  • C

C

#include <stdio.h>

int main() {

   int age = 20;

   float height = 5.9;

   char grade = 'A';

   char name[] = "John";

   printf("Name: %s\n", name);

   printf("Age: %d\n", age);

   printf("Height: %.1f\n", height);

   printf("Grade: %c\n", grade);

   return 0;

}
You can also try this code with Online C Compiler
Run Code


Explanation

  • printf("Name: %s\n", name);: This prints the string stored in name.
     
  • printf("Age: %d\n", age);: This prints the integer value stored in age.
     
  • printf("Height: %.1f\n", height);: This prints the float value stored in height with one decimal place.
     
  • printf("Grade: %c\n", grade);: This prints the character stored in grade.


Output

Name: John
Age: 20
Height: 5.9
Grade: A

Formatted Input Using scanf()

The scanf() function reads formatted input from the standard input (keyboard).

Syntax

scanf("format string", &argument1, &argument2, ...);

 

  • format string: A string that contains format specifiers.
     
  • arguments: Pointers to variables where the input values will be stored.

Format Specifiers

  • %d - Integer
     
  • %f - Floating-point number
     
  • %c - Character
     
  • %s - String

Example

  • C

C

#include <stdio.h>

int main() {

   int age;

   float height;

   char grade;

   char name[50];

   printf("Enter your name: ");

   scanf("%s", name);

   printf("Enter your age: ");

   scanf("%d", &age);

   printf("Enter your height: ");

   scanf("%f", &height);

   printf("Enter your grade: ");

   scanf(" %c", &grade);

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

   return 0;

}
You can also try this code with Online C Compiler
Run Code


Explanation

  • scanf("%s", name);: Reads a string from the input and stores it in name.
     
  • scanf("%d", &age);: Reads an integer from the input and stores it in age.
     
  • scanf("%f", &height);: Reads a float value from the input and stores it in height.
     
  • scanf(" %c", &grade);: Reads a character from the input and stores it in grade. The space before %c is used to consume any leftover whitespace.
     

Output (example input in italics):

Enter your name: John
Enter your age: 20
Enter your height: 5.9
Enter your grade: A
Name: John, Age: 20, Height: 5.9, Grade: A

Handling Strings and Characters

Single Characters

Reading single characters requires a space before the format specifier to handle newline characters left in the input buffer.

char c;
scanf(" %c", &c);
printf("Character: %c\n", c);

Strings

To read strings with spaces, use gets() or fgets():

char str[100];
fgets(str, sizeof(str), stdin);
printf("String: %s\n", str);


Explanation:

  • fgets(str, sizeof(str), stdin);: Reads a string including spaces from the input.

Frequently Asked Questions

How do I handle spaces in strings using scanf()? 

Use gets() or fgets() for strings with spaces. 

Example:

char str[100];

fgets(str, sizeof(str), stdin);

printf("String: %s\n", str);

What are some common errors when using scanf()?

 Common errors include:

  • Forgetting the & before variables.
     
  • Not handling input errors.
     
  • Not using a space before %c when reading characters.

How do I prevent buffer overflow when using scanf()? 

Specify the maximum width for %s to prevent buffer overflow. Example:

char name[50];

scanf("%49s", name);

Conclusion

Formatted input and output are essential skills in C programming for effective interaction with users and clear data presentation. Mastering printf() and scanf() will make your programs more efficient and user-friendly. Keep practicing these functions to enhance your coding skills.

You can also practice coding questions commonly asked in interviews on Coding Ninjas Code360

Also, check out some of the Guided Paths on topics such as Data Structure and AlgorithmsCompetitive ProgrammingOperating SystemsComputer Networks, DBMSSystem Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry Experts.

Live masterclass