C language – Input and Output statements in C

input and output statements in c language

In C language input, output is the three essential features of the computer program. The program takes some input data, processes it, and gives the output.

Two methods for providing data to the program

  • Assigning the data to the variables in a program.
  • By using the Input / Output statements.

In C language, two types of Input/Output statements are available, and all input and output operations are carried out through function calls.

Several functions are available for input/output operations in ‘C’. These functions are known as the standard I/O library.

  1. Unformatted Input/Output statements
  2. Formatted Input/Output statements.
input/output statements inn c language

Unformatted input/output statement in c language

Single character Input-getchar() function

A single character can be given to the computer using the ‘C’ input library function getchar()


Syntax: char variable = getchar();

Description:

char        datatype;

variable  any valid ‘c’ variable

Example: char x;  /* declaration statement */

                  x = getchar(); /* input statement */


The getchar() function is written in the standard I/O library. It reads a single character from an input device.

This function does not require any arguments, through a pair of empty parenthesis, must follow the statement getchar().

Example program

Testing a character type.

 

Output:

 
1. Enter any Character/Digit ....a
   It is a alphabet
2. Enter any Character/Digit ....1
   It is a digit
3. Enter any Character/Digit ....#
   It is aphanumeric

Explanation: In the above program the variable ‘ch’ is declared as a character type. The getchar() function reads only one character through the keyword.

The entered character is evaluated whether it is alphabet or digit or alphanumeric by using the respective functions. According to the entered character the output was produced.

Single Character Output-putchar() function

The putchar() function is used to display one character at a time on the output device. This function does the reverse operation of the single character input function.


Syntax: putchar (character variables);

Description: The character variable is the valid ‘c’ variable of the type of char data type.

Example: char x;    /* declaration part */

              putchar(x);  /* output statements */


Example program

Convert a Character from Lowercase to Uppercase and vice-versa.

Output:

1. Enter any alphabet either in Lower or Uppercase ....s
S
2. Enter any alphabet either in Lower or Uppercase ....M
m

Explanation:

In this program, the getchar() function reads the single character from the terminal. The program finds whether the character is the lower or upper case if the character is the lower case then it is converted to uppercase using toupper() function.

else if the character is uppercase then it is changed to lowercase using tolower() function and produced the output.

The getc() function in the c language

This is used to accept a single character from the standard input to a character variable.


Syntax: character variable = getc();

Description: Variable is valid ‘c’ variable of char data type.

Example: char c;

                  c = getc();


The putc() function

This is used to display a single character in a character variable to the standard output device.


Syntax: putc(character variable);

Description: Variable is valid ‘c’ variable char data type.

Example: char c;

                  putc(c);


[Note: the getc() and putc() are often used in file processing.]
 

The gets() function

In the C programming language, gets() function is used to read the string (string is a group of characters) from the standard input device (keyword).


Syntax: gets(char type of array variable);

Description: Valid ‘c’ variable declared as one dimension char type.

Example: gets();


The puts() function

The puts() function is used to display/write the string to the standard output device(Monitor).

Syntax: puts(char type of array variable);

Description: Valid ‘c’ variable declared as one dimension char type.

Example: puts(s);

Example program

Program using gets() and puts() function

Output:

Enter Name : Abdul kalam
Print the Name : Abdul kalam

Explanation: In this program, the variable ‘scientist’ is declared as a character array. The gets() function reads character from input and point using puts() function.

The getch() and getche() Functions

The getch reads a single character directly from the keyword, without echoing to the screen. The getche reads a single character from the keyword and echoes it to the current text window.

Syntax: int getch(void);

              int getche(void);

Return value: both functions return the character read from the keyword.

Example program

Demonstration of getch()

Example: Demonstration of getche()

Explanation: The gets() and puts() functions are similar to scanf() and printf(), but the difference is in the case of scanf(), when there is a blank space in the input text, then it takes the space as an end of the string, the remaining string is not been taken.

Character Test functions in c language

The C language provides many library functions for manipulating characters. They are divided into two major groups, classifying functions and converting functions.

The prototypes of these functions are in the header file. The table below summarizes some of the ‘C’ character test functions.

Functions           Test

isalnum(ch) → Is ch an alphanumeric character?

isalpha(ch) → Is ch an alphabet character?

isdigit(ch) → Is ch a digit?

islower(ch) → Is ch lowercase letter?

isupper(ch) → Is ch uppercase letter?

isspace(ch) → Is ch a blank space character?

tolower(ch) → Convert ch to lowercase.

toupper(ch) → Convert ch to uppercase.

iscntrl(ch) → Is ch ASCII control character?

isprint(ch) → Is ch is printable?

isgraph(ch) → Is ch is a graphic character?

ispunct(ch) → Is ch is punctuation character?

isxdigit(ch) → Is ch is hexadecimal digit?

Example program

To convert a character to its lower case and vice versa.

Output:

1. Read an alphabet....a
   A
2. Read an alpahbet....A
   a

FAQ's