C-Programming

Data input and output

The important of C programming is its ability to fascinate the user as far as possible. To do this we should able to handle input and output (I/O). All the input/output operations are carried out through function call such as printf( ), scanf ( )etc.

These types of functions which are used to read and display the data are collectively known as standard I/O library functions. Each program that uses these types of functions should include the standard header file <stdio.h> which stands for standard input output header file. It is included in the program by writing the code #include<stdio.h> which tells the compiler to search a file named stdio.h and place its content at the point of the program. The contents of the file became part of the source code when it is compiled.

For inputting the data (which is called reading of data in C program), the input function scanf( ) is used which ca n read the data from a terminal. In the same way for displaying (outputting) the output function ‘printf ( )’ is used. Hence the printf( ) function moves the data from computer memory to standard output device i.e monitor where as scanf( ) function enters the data from standard output device i.e. keyboard and stores it in computer main memory.

We have studied about this earlier, therefore we are not going to discuss further on this topic.

More data input and out put functions are as follows:

Data input functions:

  1. getch( )
  2. getche( )
  3. getchar( )
  4. gets( )

Data output functions:

  1. putchar( )
  2. putc( )
  3. puts( )

Escape sequence characters:

Those characters which escape the sequence of the data are called escape sequence characters. The complete set of escape sequences is

Character Function Character Function
 \a   alert (bell) character   \\   backslash
 \b   Backspace  \?  To print question mark
 \f   form feed   \' to print single quote
 \n   new line  \"   to print double quote
 \r   Carriage return  \ooo   octal number
 \t   horizontal tab  \xhh   hexadecimal number 
 \v   Vertical tab  

The character constant '\0' represents the character with value zero, the null character. '\0' is often written instead of 0 to emphasize the character nature of some expression, but the numeric value is just 0.

Use of getchar ( ) and putchar ( ) functions:

Both functions are character holding functions that means they can process only one character at a time. By the name of the function getchar ( ), it can be guess that this function is used to read a character from standard input device i.e. keyboard and actually it is. Similarly the function putchar ( ) is used to put or print a character on the monitor. The general syntax of these functions is:

variable = getchar( );

putchar(variable);

Example: write a program which reads a character from the keyboard and print in upper case.

#include<stdio.h>

#include<conio.h>

#include<ctype.h>

void main( )

{

char ch;

printf(“please enter a character:  “);

ch=getchar( );  /*reading a character */

ch=toupper(ch); /*changing the character in to upper case*/

putchar(ch); /*printing the character*/

getch();

}

We can write in place of putchar(ch); the statement printf(“%c”,ch); both result same output.

#include<stdio.h>

#include<conio.h>

#include<ctype.h>

void main( )

{

char ch;

int var;

printf(“please enter a character:  “);

ch=getchar( );

var=toascii(ch);

val-=32;

putchar(val);

getch();

}

 

 

 

 

 

 

Use of gets( ) and puts( ):

As in the case of getchar( ) and putchar( ) where ‘char’ represents character in the same way here in gets ( ) and puts ( ), ‘s’ represents string. String is set of character. In detail about the string we will study later in the section Arrays. For this instant let us understand string as a set of character only. These functions are string functions which are used in manipulation of strings i.e. reading and writing the string.

Control statement (Prev Lesson)
Back to C-Programming

No Comments

Post a Reply

Teacher
Bhim Gautam
Role : Lecturer
Read More
error: Content is protected !!