Input and Output in C Programming
- What is Input and Output?
- Input→ Data given to the program (keyboard, file, user entry).
- Output→ Information displayed by the program (screen, printer, file).
In C, input and output operations are performed using functions from the header file:
#include <stdio.h>
Output Functions in C
printf() Function
The printf() function is used to display output on the screen.
Syntax
printf(“format string”, variables);
Example
#include <stdio.h>
int main()
{
int age = 18;
printf(“My age is %d”, age);
return 0;
}
Common Format Specifiers
| Specifier | Meaning |
| %d | Integer |
| %f | Float |
| %c | Character |
| %s | String |
| %If | Double |
Example with Multiple Values
printf(“Name: %s Age: %d”, name, age);
Input Functions in C
scanf() Function
The scanf() function is used to take input from the user through the keyboard.
Syntax
scanf(“format string”, &variable);
The & (address operator) is used to store input in the variable’s memory location.
Example
#include <stdio.h>
int main()
{
int number;
printf(“Enter a number: “);
scanf(“%d”, &number);
printf(“You entered %d”, number);
return 0;
}
Input of Different Data Types
Integer Input
int a;
scanf(“%d”, &a);
Float Input
float b;
scanf(“%f”, &b);
Character Input
char ch;
scanf(” %c”, &ch);
(Note the space before %c to avoid newline issues.)
String Input
char name[20];
scanf(“%s”, name);
(No & is needed for strings because the array name already represents an address.)
Example Program: Input and Output
#include <stdio.h>
int main()
{
int age;
float marks;
char grade;
printf(“Enter age: “);
scanf(“%d”, &age);
printf(“Enter marks: “);
scanf(“%f”, &marks);
printf(“Enter grade: “);
scanf(” %c”, &grade);
printf(“\nAge = %d”, age);
printf(“\nMarks = %.2f”, marks);
printf(“\nGrade = %c”, grade);
return 0;
}
Special Output Characters (Escape Sequences)
| Escape Sequence | Meaning |
| \n | New line |
| \t | Tab space |
| \ | Backslash |
| “ | Double quote |
Example:
printf(“Hello\nWorld”);
Output:
Hello
World
Simple Practice Programs
- Add Two Numbers
int a, b, sum;
scanf(“%d %d”, &a, &b);
sum = a + b;
printf(“Sum = %d”, sum);
- Area of Rectangle
float l, b, area;
scanf(“%f %f”, &l, &b);
area = l * b;
printf(“Area = %f”, area);
|
Formatted and Unformatted I/O functions of C language with syntax and example. 1. Formatted I/O FunctionsFormatted I/O functions allow input and output of data in a specific format using format specifiers such as %d, %f, %c, %s. Common Formatted I/O Functions· printf() – used for formatted output Syntaxprintf() Example Program#include <stdio.h> Explanation· %d is used for integers. · %f is used for floating-point numbers. · The output is displayed in the specified format. 2. Unformatted I/O FunctionsUnformatted I/O functions read or write single characters or strings without using format specifiers. Common Unformatted I/O Functions· getchar() – reads a single character · putchar() – writes a single character · gets() – reads a string · puts() – writes a string Syntaxgetchar() Example Program#include <stdio.h> Explanation· getchar() reads a single character. · putchar() displays a single character. · gets() reads a string. · puts() displays a string.
Difference Between Formatted and Unformatted I/O
|