Input / Output Functions in C

 Input/ Output Functions

Like other languages, C does not have any built-in input/output statements as part of its syntax. All input/output operations are carried out through printf() and scanf() function. There exist several functions that have more or less become standard for input and output operations in C. These functions are collectively known as the Standard I/O library because it can take or accept any type of data.

Each program that uses a standard input/output function must contain a statement #include<stdio.h> at the beginning. The instruction #include<stdio.h> tells the compiler to search for a file named stdio.h and place its contents at this point in the program. The contents of the header file become part of the source code when it is compiled.

C language consists of some I/O functions like getchar(), putchar(), scanf() gets(), puts(), getch(), putch(), getche() etc.

 

Input / Output Functions

The printf() and scanf() functions are known as formatted I/O functions because they can take any type of format of data from the I/O devices. 


printf() can be used to display some conversion characters along with some unchanged characters. Such conversion characters may include format specifiers or escape sequences.


Syntax: 

printf( "control string", arg1, arg2, ......); 

Here control string may consist of any simple characters or format conversion specifiers or escape sequences and arg1, arg2,.... are arguments (variables) that represent the individual data item.


Example 1

printf(“Hello”);

Here, the statement consists of simple characters without arguments and will display output ‘Hello’.



Example 2

int a=5;

printf(“%d”,a);

Here, printf() statement contains a format conversion specifier “%d”  and an argument ‘a’ which will display output 5.


Example 3

float a=0.5;

float b=987.8145;

printf(“ \n here are two values %f and %.3f ”, a, b);

Here, printf() statement contains characters "here are …….” , format  conversion specifiers “ %f ”, “ %.3f ”, escape sequence ‘\n’ and arguments ‘a’, ‘b’ which will display Output: 

here are two values 0.500000 and 987.814”.


scanf() can be used to get inputs from the user.

Syntax:

scanf (" control string " , argl, arg2,....)

Here control string may consist of any format conversion specifiers and arg1, arg2,.... are arguments specifying the address of locations where the data is stored. Control string and arguments are separated by commas. For example, if ‘k’ is a variable or argument then '&k' is its address. So, it must be written as &k with scanf function.


Examples:

int a;

float b;

char c;

scanf(“%d%f%c”,&a,&b,&c);

If input is 4,0.8 and m then values of a, b and c are respectively 4,0.800000 and m.


Program 1   Write a program to find the sum of two input numbers.

#include<stdio.h>

int main()

{

int a,b,c;

printf(“\n Enter first number”);

scanf(“%d”,&a);

printf(“\n Enter second number”);

scanf(“%d”,&b);

c=a+b;

printf(“Sum is %d” ,c);

return 0;
}




Program 2   Write a program to find the square of a number.

#include<stdio.h>

int main()

{

int a,b;

printf(“\n Enter a number”);

scanf(“%d”,&a);

b=a*a;

printf(“Square = %d” ,b);

return 0;
}


Other Input/Output Functions

  1. getchar () and putchar():

The getchar() function is used to read (or accept) a single character. It can not take more than one character. 

Syntax: 

variable_name = getchar(); 

Here variable_name is a valid C name that has been declared as char type. 


The putchar() function is used to display the character contained in the variable name at the output screen / terminal. 

Syntax: 

putchar(variable_name); 

Where variable_name is a type char containing a character. 

Example 

#include<stdio.h>

int main()

{ char a; 

printf("Enter a character"); 

a=getchar(); 

putchar(a);

return 0;

}

In the above example, getchar() function accepts a character which stores in a variable 'a' and putchar() used to display stored character. 


  1. gets and puts():

The gets() function is used for a completely different purpose. It reads a string (group of characters) from the standard input and stores it in the given variable.

It reads a whole line of input until a newline. 

Syntax: 

gets (variable); 

Example: 

char name[25]; 

gets(name); // it will ask the user for a name and save the name into name

The puts() function is used to display text in the monitor which is stored in the variable. But the variable is always string data type. 

Syntax: 

puts (variable); 

or puts("string");

Example:

char name[25]={“Alex”};

puts(name);

This example will display name “Alex”


Program 3   Write a program to enter full name and print it on screen.

#include<stdio.h>

int main()

{

char name[100];

puts(“Enter your full name”);

gets(name);

puts(“Your Name is:”);

puts(name);

return 0;
}



Program 4   Write a program to input name,age and salary and print them on monitor.

#include<stdio.h>

int main()

{

char name[50];

int age;

float salary;

printf(“Enter name”);

gets(name);

printf(“Enter age”);

scanf(“%d”,&age);

printf(“Enter salary”);

scanf(“%f”,&salary);

printf(“Name=%s”,name);

puts(name);

printf(“\n Age=%d”,age);

printf(“\n Salary = %.2f” ,salary);

    return 0;
}


Worked Out Examples:

Program 5   Write a program to input two numbers and print remainder and quotient.

#include<stdio.h>

int main()

{

int a,b,c,d;

printf(“ Enter two numbers”);

scanf(“%d%d”,&a,&b);

c=a/b;

d=a%b;

printf(“Quotient = %d” ,c);

printf(“Remainder =%d”,d);

return 0;
}

Program 6   Write a program to input seconds and convert it into hours,minutes and seconds.

#include<stdio.h>

int main()

{

int s,h,r,d,m,a;

printf(“Enter seconds”);

scanf(“%d”,&S);

h=s/3600; // for hour

r=s%3600;

m=r/60; // for minutes

d=r%60; // for seconds

printf(“Hour =%d Minutes=%d Seconds=%d” ,h,m,d);

return 0;
}


Program 7   Write a program to calculate area of a circle.

#include<stdio.h>

int main()

{

float r,a;

printf(“Enter radius”);

scanf(“%f”,&r);

a=3.14*r*r;

printf(“Area = %.2f” ,a);// upto two digits

return 0;
}


Netra Koirala

Netra Koirala

Computer Science Educator

Passionate computer science educator and author. Provides free study notes, practical guides, and tutorials for Class 9, 10, 11, 12, and B.Sc CSIT students in Nepal. Years of teaching experience in computer science fundamentals.

Computer Science notes, tutorials, MCQs, and educational resources for Nepal students. Covering Class 9, SEE preparation, Class 11, Class 12, SLC, programming, DBMS, networking, HTML, JavaScript, PHP, OOP and more.

Featured Post

Grade 10 Computer Science: Specification Grid & Model Questions

Specification Grid & Model Questions of Computer Science | Grade 10 📚 Examination Resource Specification Grid & M...

Followers