1a Input and Output in C

Input and Output in C Programming

  1. 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

  1. Add Two Numbers

int a, b, sum;
scanf(“%d %d”, &a, &b);
sum = a + b;
printf(“Sum = %d”, sum);

  1. 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.
In C programming, Input/Output (I/O) functions are used to read data from the keyboard and display data on the screen. These functions are classified into Formatted I/O functions and Unformatted I/O functions.

1. Formatted I/O Functions

Formatted 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
·         scanf() – used for formatted input

Syntax

printf()
printf(“format string”, variable1, variable2, …);scanf()
scanf(“format string”, &variable1, &variable2, …);

Example Program

#include <stdio.h>
int main()
{
int age;
float salary;
printf(“Enter age and salary: “);
scanf(“%d %f”, &age, &salary);
printf(“Age = %d\n”, age);
printf(“Salary = %.2f”, salary);
return 0;
}

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 Functions

Unformatted 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

Syntax

getchar()
char_var = getchar();putchar()
putchar(char_var);gets()
gets(string);puts()
puts(string);

Example Program

#include <stdio.h>
int main()
{
char ch;
char name[20];
printf(“Enter a character: “);
ch = getchar();
printf(“Enter a name: “);
gets(name);
putchar(ch);
puts(name);
return 0;
}

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

Formatted I/O Unformatted I/O
Uses format specifiers Does not use format specifiers
Allows formatted data input/output Handles simple character or string input/output
Examples: printf(), scanf() Examples: getchar(), putchar(), gets(), puts()