Basic Input /Output Statements in C
C provides standard input and output (I/O) functions for interaction with the user. These
functions are available in the <stdio.h> library.
Output Statements
printf() (Formatted Output)
• Used to print text, variables, and formatted data on the screen.
• Supports format specifiers like %d, %f, %c, %s, etc.
Example: Printing Different Data Types
#include <stdio.h>
int main() {
int age = 25;
float pi = 3.1415;
char grade = 'A';
char name[] = "Alice";
printf("Age: %d\n", age); // Prints integer
printf("Pi: %.2f\n", pi); // Prints float (2 decimal places)
printf("Grade: %c\n", grade); // Prints character
printf("Name: %s\n", name); // Prints string
return 0;
}
putchar() (Single Character Output)
• Prints a single character.
• Returns the printed character.
Example: Using putchar()
#include <stdio.h>
int main() {
char ch = 'A';
putchar(ch); // Prints 'A'
putchar('\n'); // Moves to a new line
return 0;
}
1.3. puts() (String Output)
• Prints a string followed by a new line (\n).
• Safer than printf("%s", str) because it automatically adds a newline.
Example: Using puts()
#include <stdio.h>
int main() {
char str[] = "Hello, C!";
puts(str); // Prints "Hello, C!" and moves to a new line
return 0;
}
Input Statements
scanf() (Formatted Input)
• Reads input from the user.
• Uses format specifiers to match variable types.
Data Type Format Specifier
int %d
float %f
char %c
string %s
Example: Reading Different Data Types
#include <stdio.h>
int main() {
int age;
float pi;
char grade;
char name[20];
printf("Enter age: ");
scanf("%d", &age); // Reads an integer
printf("Enter Pi value: ");
scanf("%f", &pi); // Reads a float
printf("Enter grade: ");
scanf(" %c", &grade); // Space before %c prevents buffer issue
printf("Enter name: ");
scanf("%s", name); // Reads a single-word string
printf("\n--- Output ---\n");
printf("Age: %d\nPi: %.2f\nGrade: %c\nName: %s\n", age, pi, grade, name);
return 0;
}
Limitation: scanf("%s", name); does not read spaces in multi-word input.
getchar() (Single Character Input)
• Reads one character from the keyboard.
• Pressing Enter terminates input.
Example: Using getchar()
#include <stdio.h>
int main() {
char ch;
printf("Enter a character: ");
ch = getchar(); // Reads a character
printf("You entered: ");
putchar(ch); // Prints the character
putchar('\n');
return 0;
}
gets() (String Input)
• Reads a full line of text (including spaces).
• Unsafe (can cause buffer overflow) – use fgets() instead.
Example: Using gets()
#include <stdio.h>
int main() {
char name[50];
printf("Enter your full name: ");
gets(name); // Reads a full line (deprecated in modern C)
printf("Hello, %s\n", name);
return 0;
}
Warning: gets() is unsafe and should not be used in modern C.
fgets() (Safe String Input)
• Reads a full line, including spaces.
• Prevents buffer overflow by limiting input size.
Example: Using fgets()
#include <stdio.h>
int main() {
char name[50];
printf("Enter your full name: ");
fgets(name, sizeof(name), stdin); // Reads input safely
printf("Hello, %s", name); // `fgets()` includes newline
return 0;
}
3. Summary Table
Function Purpose Example
printf() Prints formatted output printf("Age: %d", age);
putchar() Prints a single character putchar('A');
puts() Prints a string (adds newline) puts("Hello!");
scanf() Reads formatted input scanf("%d", &age);
getchar() Reads a single character char c = getchar();
gets() Reads a full line (unsafe) gets(name);
fgets() Reads a full line (safe) fgets(name, 50, stdin);
4. Example: Full Program with I/O
#include <stdio.h>
int main() {
int age;
float salary;
char initials[3];
char fullname[50];
// Input
printf("Enter your age: ");
scanf("%d", &age);
printf("Enter your salary: ");
scanf("%f", &salary);
getchar(); // Consume leftover newline
printf("Enter your initial: ");
fgets(initials, sizeof(initials), stdin);
getchar(); // Consume leftover newline
printf("Enter your full name: ");
fgets(fullname, sizeof(fullname), stdin); // Reads full name safely
// Output
printf("\n--- User Details ---\n");
printf("Age: %d\n", age);
printf("Salary: %.2f\n", salary);
printf("Initials: %s\n", initials);
printf("Full Name: %s", fullname);
return 0;
}
NB
Use printf() and scanf() for most input/output tasks.
Use fgets() instead of gets() for safe string input.
Use getchar() and putchar() for single character I/O.
Format specifiers must match the variable type in scanf().