Input and output are important parts of programming. Input
allows a program to receive data from the user, while output
allows the program to display information. In C, functions such as
printf() and scanf() are commonly used for
formatted output and input.
<stdio.h>.
Input is data provided to a program while it is running. For example, a user can enter their name, age, marks, or other information.
Enter your age: 20
Here, 20 is input provided by the user.
Output is information produced by a program and displayed on a screen or sent to another output device.
Hello World
A C program commonly displays output using printf().
C provides standard input/output facilities through the
<stdio.h> header.
#include <stdio.h>
This header provides declarations for functions such as
printf(), scanf(), getchar(), and
putchar().
The printf() function is used to display formatted output.
#include <stdio.h>
int main()
{
printf("Hello World");
return 0;
}
Output:
Hello World
Text can be printed by placing it inside double quotation marks.
printf("Welcome to C Programming");
Output:
Welcome to C Programming
The %d conversion specifier can be used to display an
int.
int age = 20;
printf("Age = %d", age);
Output:
Age = 20
Floating-point values can be displayed using %f.
float price = 99.50f;
printf("Price = %f", price);
The %c conversion specifier can be used to display a character.
char grade = 'A';
printf("Grade = %c", grade);
Output:
Grade = A
The %s conversion specifier can be used to display a
null-terminated character string.
char name[] = "Rahul";
printf("Name = %s", name);
The escape sequence \n moves the cursor to the next line.
printf("Hello\nWorld");
Output:
Hello
World
A single printf() call can display multiple values.
int age = 20;
int marks = 85;
printf("Age = %d, Marks = %d", age, marks);
The scanf() function is used to read formatted input from the
standard input stream, normally the keyboard.
int age;
scanf("%d", &age);
Use %d with scanf() to read an
int.
int age;
printf("Enter age: ");
scanf("%d", &age);
The value entered by the user is stored in age.
For a typical scalar variable, scanf() needs the address where
the input value should be stored. The address-of operator &
provides that address.
int age;
scanf("%d", &age);
Use %f in scanf() when reading a
float.
float price;
printf("Enter price: ");
scanf("%f", &price);
Use %lf in scanf() to read a
double.
double salary;
printf("Enter salary: ");
scanf("%lf", &salary);
The %c conversion specification can be used to read a character.
char grade;
printf("Enter grade: ");
scanf(" %c", &grade);
The leading space before %c tells scanf() to skip
leading whitespace.
A character array can be used to store a string.
char name[20];
printf("Enter name: ");
scanf("%19s", name);
The array name already represents the address of its first element, so
& is not used here.
With %s, scanf() normally reads characters until
whitespace is encountered.
char name[30];
scanf("%29s", name);
For example, entering Rahul works, but a multi-word name would
be split at the whitespace.
#include <stdio.h>
int main()
{
int age;
printf("Enter your age: ");
scanf("%d", &age);
printf("Your age is %d", age);
return 0;
}
A program can receive input and then display the result.
#include <stdio.h>
int main()
{
int number;
printf("Enter a number: ");
scanf("%d", &number);
printf("You entered: %d", number);
return 0;
}
Multiple values can be read using multiple conversion specifications.
int a, b;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
The user can enter the values separated by whitespace.
#include <stdio.h>
int main()
{
int a, b, sum;
printf("Enter first number: ");
scanf("%d", &a);
printf("Enter second number: ");
scanf("%d", &b);
sum = a + b;
printf("Sum = %d", sum);
return 0;
}
#include <stdio.h>
int main()
{
float price, quantity;
printf("Enter price: ");
scanf("%f", &price);
printf("Enter quantity: ");
scanf("%f", &quantity);
printf("Total = %.2f", price * quantity);
return 0;
}
The putchar() function writes a single character to standard
output.
#include <stdio.h>
int main()
{
putchar('A');
return 0;
}
Output:
A
The getchar() function reads one character from standard input.
#include <stdio.h>
int main()
{
char ch;
ch = getchar();
printf("You entered: %c", ch);
return 0;
}
The printf() function supports width and precision to control
formatted output.
float price = 99.5678;
printf("%.2f", price);
Output:
99.57
scanf() returns the number of input items successfully assigned.
This can be used to check whether the expected input was received.
int age;
if (scanf("%d", &age) == 1)
{
printf("Valid input");
}
#include <stdio.h>.& when it is required by scanf().& incorrectly with a character array used as a string.printf().#include <stdio.h>
int main()
{
char name[30];
int age;
float marks;
printf("Enter your name: ");
scanf("%29s", name);
printf("Enter your age: ");
scanf("%d", &age);
printf("Enter your marks: ");
scanf("%f", &marks);
printf("\n--- Student Details ---\n");
printf("Name = %s\n", name);
printf("Age = %d\n", age);
printf("Marks = %.2f\n", marks);
return 0;
}
This program demonstrates basic keyboard input and formatted output using different data types.
<stdio.h> provides standard input/output functions.printf() is commonly used for formatted output.scanf() is commonly used for formatted input.%d is commonly used for int.%f is used to read a float with
scanf().%lf is used to read a double with
scanf().%c is used for characters and %s for strings.getchar() reads one character and putchar()
writes one character.Question: Which function is commonly used to take formatted input from the user in C?