C Input and Output function

 

There are a number of built in functions to input the data and to display the data on the screen.

 

Standard Output:

The information or the processed data given by the computer is known as output. The output that is displayed on the monitor is the hardcopy and the printed form of output is a softcopy. In C programming language we use various outputs.

Some important output functions in C are as follows:

  • printf ()
  • puts ()

The functions used for input and output are stored in the header file <stdio.h>. Header file is necessary to include in the program it input/output functions are used.

 

Printf () Function:

This function is used to display output on the monitor. It can display text, constants or values of variables on screen in specified format. It is most commonly used to display output in C language.

SYNTAX:

The syntax of printf statement is as follows:
printf (“Format string”);

Format string is given in double quotes. It is also called control string. Format string may include text that is the message to be printed, escape sequences that are used to specify the format of output e.g. \n, \t, etc.

To display the value of the variable we use the following statement:

printf (“Format string”, argument list);

The format string in printf () function consists of the following components:

  1. Text: Text is the message that will be displayed on the screen
  2. Format specifiers: Format specifiers are used to specify that how the values of the variables will be displayed.
  3. Escape sequences: These are used to specify the format of output

In the printf () statement, the argument list consists of constants, variables or expressions whose values are to be printed on the screen. The variables in argument list are separated by comma. The value of the argument is printed according to the corresponding format specifier given in format string. Different format specifiers are used for different types of values.

 

Using Format string without argument list:

The following example displayes a simple message using printf function:

printf (“Hello World”);

In the above example, printf function contains only format string. There is no argument list, format specifier or escape sequence. It will display the message “Hello World” on the screen.

 

Using Format string with single argument:

The following example uses an integer variable as argument and displays its value:

printf (“Marks obtained are %d”, marks);

In the above example, format string contains %d which is a format specifier. It is related to the variable ‘marks’. It indicates that the value of marks will be displayed on output screen as an integer.

 

Using format string with multiple arguments:

Many variables can be used in one printf function. Consider the following example:

printf (“Marks obtained are %d and grade is %c”, marks, grade);

The above example uses two format specifiers %d and %c. %d is related to the variable marks and %c is related to the character type variable grade. %d is used with integers and %c with characters. The sign % indicates the beginning of a format specifier.

Consider the following code in which we have used printf function:

EXAMPLE:

Following example is a program that displays a message and values of integer and character variables.

# include < stdio. h>

# include <conio. h>

void main()

{Output

int n=10;

char ch=’*’;

printf (“testing output”);

printf (“%d”, n);

printf (“\n”);

printf(“testing output %c”, ch);

}

Working of the above program:

The above program declares and initializes two variables n and ch. It displays message “testing output” on the screen. It then displays the values of n and ch.

 

Puts () function:

The puts () function is used to display string on the screen. It can display a string constant or string variable.

SYNTAX:

The syntax of puts () function is as follows:

puts (parameter);

Parameter indicates the string value in which the string is stored. In case of string constant, it is written in double quotes.

EXAMPLE:

The following example displays the contents of a string variable str on screen:

Puts (str);

The following example displays a string constant on the screen:

Puts (“programming makes life interesting”);

PROGRAM:

# include < stdio. h>PROGRAM

#include < conio. h>

void main()

{

char book[50];

printf (“enter name of your favorite book:”);

gets(book);

printf (“your favorite book is:”);

puts (book);

getch();

}

 

putchar () function:

The putchar () function is used to output a single character on the output screen. The putchar () function is defined in the C standard library < stdio. h>.

The following is the declaration of putchar () function:

int putchar (int char);

Here char is the character that is to be printed on the output screen. This function also returns character which is casted to an integer. Consider the following code in which we have used the putchar () function to output a single character:

CODE:

# include <stdio. h>

# include < conio. h>

void main ()

{

char ch;

ch = ‘a’;

putchar (ch);

}

OUTPUT:

a

 

Standard Input:

The process of giving something to the computer is known as input. The input is mostly given by keyboard. The term standard input refers to the input using keyboard. A program may need certain input from the user for working properly. C programming language provides many functions to get input from the user.

Some important input functions are as follows:

  • scanf
  • gets ()
  • getch ()
  • getche ()

 

scanf Function:

This function is used to get input from the user. The input is stored in a variable in a specified form.

SYNTAX:
The syntax of scanf function is as follows:

scanf (format string, &variable1, &variable2, &variable3….);

Format string: The format string is given in double quotes. It may consist of different format specifiers that indicate the format according to which the values will be entered.

Variables: The variables are used to store the value entered by the user through keyboard. Each variable refers to a memory location. The symbol ‘&’ indicates the memory location of variable in which the input is stored. It is called address operator.

 

Getting Single Input:

The following example inputs a single value:

scanf (“%d”, &n);

The above statement is used to get an integer input. The value entered by the user will be stored in a variable n.

 

Getting Multiple Inputs:

scanf function can be used to input many values at the same time. Consider the following line of code:

scanf (“%d %d”, &n, &m);

The above statement will get two inputs in n and m respectively. It is also possible to get different types of inputs in the same function, consider the following line of code in which we have input variables of different data type:

scanf (“%d %f”, &n, &m);

The above statement will get two inputs in n and m. The first input is of integer data type and the second input is of float data type. The scanf function is normally used in combination with printf function. the purpose of using printf function before scanf function is to tell the user that computer is waiting for the input. Consider the following example:

printf (“Enter your marks”);

scanf (“%d”, &marks);

In the above example, the first line displays message on the screen to tell the user about the input. It makes program easier to use by the user.

EXAMPLE:

The following is the program to convert distance from kilometers into meters.

# include < stdio. h>

# include < conio. h>

void main()

{

float k;Enter distance in kilometers

double m;

printf (“enter distance in kilometers”);

scanf (“%f”, k);

m=k*1000;

printf (“%f kilometers = %f meters”, k, m);

}

The above program declares two variables m and k. It displays a message to the user and gets input of distance in kilometers. The symbol ‘&’ is used with variable k. The symbol refers to the address of the variable. The value entered by the user is stored in k. The program calculates the number of meters and displays the result on the screen.

 

getch() function:

The word getch () stands for get character. The function is used to input single character from the user. When this function is executed, it waits for any key to be pressed. The character entered by the user is not displayed on the screen.

The function getch () is defined in the header file conio.h. This header file must be included in the program to use this function.

SYNTAX:

The syntax of getch () function is as follows:

getch ();

Another way to use this function is as follows:

variable = getch ();

Here variable indicates the variable in which the character is stored. The use of variable is optional.

This function is normally used in the program to stop execution of program temporarily. For example, the user can use it at the end of program so that the control remains on DOS screen until the user presses any key.

EXAMPLE:
# include <stdio. h>

# include <conio. h>Enter character

void main()

{

char c;

printf (“enter character:”);

c = getch ();

printf (“you entered: %c”, c);

getch ();

}

 

getche() function:

The letter e in getche stands for echo. The getche () function is used to input single character from the user. When this function is executed, it waits for any key to be pressed. The function echoes (displays) the character on the screen entered by the user.

This function is defined in the header file conio.h.

SYNTAX:

The syntax of getche () function is as follows:

getche();

 

gets() function:

This function is used to enter string value from the user. The input is stored in a string variable. After typing the string from keyboard, the user presses ENTER key and the string is stored in the variable. The null character \0 is automatically entered at the end of string.

SYNTAX:

The syntax of gets() function is as follows:

gets(variable);

variable indicates the string variable in which the string is stored.

EXAMPLE:

The following statement inputs a string from user and stores it in a string variable str.

printf (“Enter a string”);

gets (str);

Suppose the user types “Pakistan” on the screen and then presses the ENTER key. The string will be stored in str as follows:

P a k i s t a n \0

 

Difference between scanf and gets functions:

The only difference between scanf and gets function is that whenever a space is encountered the scanf function stops reading characters but gets function reads the space as well.

 

Format Specifier:

Format specifier is used to specify the format according to which the values will be read and displayed on the output screen. The following are the things that are determined by format specifier:

  1. data type of variable
  2. Field width
  3. Format of the value

Format specifier is started with the symbol %. Different format specifiers are used for different data types of variables. The following are some of the format specifiers:

  1. Integer format specifier
  2. Character format specifier
  3. Floating point format specifier

 

Integer Format specifier:

Different format specifiers for integer values are as follows:

Format Specifier Type
%d It is used for signed decimal integer value
%I It is used for signed integer value
%o It is used for unsigned octal value
%u It is used for unsigned decimal integer value
%x It is used for unsigned hexadecimal values with both lowercase and uppercase.

 

Floating point format specifier:

Different format specifiers for floating point values are as follows:

Format Specifier Type
%f It is used for signed float or double value
%e It is used for signed float or double in exponential notation
%g It is used for large floating point in exponential notation with no extra zeros.

 

Character Format specifier:

Different format specifiers for character values are as follows:

Format Specifier Type
%c It is used for character value
%s It is used for strings

 

Field width Specifier:

The number of columns used to display a value on the screen is called field width. A field width specifier describes the number of columns that should be used to print a value. It determines how the value will be printed on the screen.

SYNTAX:

The general syntax of using field width specifiers is as follows:

flag width. precision

Some of the values of flag are as follows:

Flag Description
The output will be left justified
+ It always displays sign with values
space Displays space if there is no sign

Width: It specifies total number of columns used to display a value.

Precision: It indicates number of columns used after decimal point

 

Specifying Field Width:

Field width specifier is used to specify number of columns in which the value is printed. It includes a positive after % symbol that indicates the field width. If the specified field width is not enough to print the value, the additional space is automatically used. If the value is less than the specified width in the format specifier, the value is printed right adjusted in the field.

 

Escape Sequences:

Escape sequences are special characters used in format string to modify the format of output. These characters are not displayed in the output. These characters always begin with backslash “\”. The backslash is known as escape character.

The following are some of the escape sequences used in C programming language:

Escape Sequence Purpose
\b Backspace
\f Form feed
\n New Line
\r Carriage return
\t Tab
\’ Single quote
\” Double quote
\xdd ASCII code in hexadecimal notation. Each d represents a digit.
\ddd

ASCII code in octal notation. Each d represents a digit.