Unit-2

INPUT/OUTPUT

 

Console I/O functions - functions to receive input from keyboard and write output to VDU(Monitor).

stdin and stdout

The C language has a notion of input coming from a place known as standard input. It is written as stdin. By default, standard input comes from the keyboard.

And, output goes to a place known as standard output. It is written as stdout. By default, standard output goes to the screen.

Console I/O Functions:

Console I/O functions can be further classified.

Console Input/Output functions

 

 

 

   Formatted functions

 

  Unformatted functions

Type 

Input

Output

 

Type 

Input

Output

char 

scanf()

printf()

 

char

 getch()

putch()

 

 

 

 

 

 getchar()

putchar()

 

 

 

 

 

getche( )

 

int 

scanf()

printf()

 

int

-

-

float  

scanf()

printf()

 

float

-

-

string    

scanf()

printf()

 

string

gets()

puts()

 

The basic difference between formatted and unformatted I/O functions is that the formatted functions allow the input read from the keyboard or the output displayed on the VDU to be formatted as per our requirements. For example, if values of average marks and percentage marks are to be displayed on the screen, then the details like where this output would appear on the screen, how many spaces would be present between the two values, the number of places after the decimal points etc., can be controlled using formatted functions.

printf( ):
The output function printf() translates internal values to character.

printf(char format, arg1, arg2, ... )

The format string can contain:

·   Characters that are simply printed as they are.

·   Conversion specification that begins with a % sign.

Printf() converts, formats, and prints its arguments on the standard output under the control of the format.

    #include <stdio.h>
    main()
    {
        int x;                                             OUTPUT
        x = 354;
        printf("[%d]\n", x);            [354]
        printf("[%1d]\n", x);          [354]
        printf("[%4d]\n", x);          [ 354]
        printf("[%04d]\n", x);                    [0354]
        printf("[%-4d]\n", x);         [354 ]
    }
 
#include <stdio.h>
    main()
    {
        float x;                                          OUTPUT
        x = 3.546;
        printf("[%f]\n", x);             [3.546]
        printf("[%5.3f]\n", x);        [3.546]
        printf("[%5.1f]\n", x);        [  3.5]
    }

scanf( ):

scanf( ) allows us to enter data from the keyboard that will be formatted in a certain way. The general form of scanf( ) statement is as follows:

scanf("%d %f %c",&c,&a,&ch);

Note that we are sending the addresses of variables (addresses are obtained by using & - 'address of' operator) to scanf( ) function. This is necessary because the values received from keyboard must be dropped into variables corresponding to these addresses. The values that are supplied through the keyboard must be separated by either blank(s), tab(s), or newline(s). Do not include these escape sequences in the format string.

Following is the list of conversion characters that can used with printf( ) and scanf( ) function.

Data type

 

 

Conversion character

Integer

 

int

%d

 

 

hexadecimal

%x

 

 

octal

%o

Real

 

float

%f

 

 

double

%lf

Characters

 

char

%c

string

 

 

%s

 

gets( ):

The gets( ) function receives a string from the keyboard. The scanf( ) function has some limitations while receiving a string of characters because the moment a blank character is typed, scanf( ) assumes that the end of the data is being entered. So it is possible to enter only one word string using scanf( ). To enter multiple words in to the string, the gets( ) function can be used. Spaces and tabs are perfectly accepted as part of the string. It is terminated when the enter key is hit.

gets(variable name);

For example:

gets(name);

puts( ):

The puts( ) function works exactly opposite to gets( ) function. It outputs a string to the screen. Puts( ) can output a single string at a time.

puts(variable name);

For example :

#include<stdio.h>
main( )

{

char name[40];
puts("Enter your name");
gets(name);
puts("Your name is");
puts(name);

}

getch() after accepting a char terminates the program without displaying the character.
getche() accepts a character, displays it and then terminates the program.
getchar()accepts a character, and then terminates the program with enter key.

Putch( )and putchar( ) This function prints a character on the screen.

For example :

#include<stdio.h>
main( )

{

char ch;
puts("Enter character");
ch=getchar( );
putch(ch);
putchar(ch);

}

Category of operators

Unary Operators
A unary operator is an operator, which operates on one operand.

Binary
A binary operator is an operator, which operates on two operands

Ternary
A ternary operator is an operator, which operates on three operands.

There is a single ternary operator:

   Symbol       Example      Explanation
     ?:          a?b:c     ternary operation

Usage of incremental and decremental operators

In most programming languages, incremental and decremental operators can be prefixed and suffixed to the variable. The prefix and suffix can be useful when manipulating variables. A suffixed operation increments the value after it has been called.

Order of evaluation for arguments in function calls is unspecified according to C99. Because of this, a compiler might produce results that appear to be transposed. The output of the examples below are compiler-specific and depends on implementation. To avoid this behavior and minimize dependence, the code could be rewritten.

In the examples below, the integer arguments are evaluated left to right.

Example 1

int i = 0;
printf (" %d \n %d ", i++, i++);

Output:

0
1

Whereas a prefixed operation will increment the value before it has been called.

Example 2

int i = 0;
printf (" %d \n %d ", ++i, ++i);

Output:

1
2

The usage of incremental and decremental operators is usually found in For loops

 

Postfix notation

In the postfix notation, the value of the operand is used first and then the operation of increment or decrement takes place, e.g. consider the statements below:

               int a,b;
               a = 10;
               b = a++;
               printf(“%d\n”,a);
               printf(“%d\n”,b);

Program Output
10
11

Prefix notation

In the prefix notation, the operation of increment or decrement takes place first after which the new value of the variable is used.

               int a,b;
               a = 10;
               b = ++a;
               printf(“%d\n”,a);
               printf(“%d\n”,b);

Program Output
11
11

Important Note:

Remember that there is a remarkable difference between the equality operator (==) and the assignment operator (=). The equality operator is used to compare the two operands for equality (same value), whereas the assignment operator is used for the purposes of assignment.

Multiple assignments:

You can use the assignment for multiple assignments as follows:

a = b= c = 10;

At the end of this expression all variables a, b and c will have the value 10. Here the order of evaluation is from right to left. First 10 is assigned to c, hence the value of c now becomes 10. After that, the value of c (which is now 10) is assigned to b, hence the value of b becomes 10. Finally, the value of b (which is now 10) is assigned to a, hence the value of a becomes 10.