Input and Output

 

When we say Input, it means to feed some data into a program. An input can be given in the form of a file or from the command line. C programming provides a set of built-in functions to read the given input and feed it to the program as per requirement.When we say Output, it means to display some data on screen, printer, or in any file. C programming provides a set of built-in functions to output the data on the computer screen as well as to save it in text or binary files.

The Standard Files

C programming treats all the devices as files. So devices such as the display are addressed in the same way as files and the following three files are automatically opened when a program executes to provide access to the keyboard and screen.

Standard File

File Pointer

Device

Standard input

stdin

Keyboard

Standard output

stdout

Screen

Standard error

stderr

Your screen

The file pointers are the means to access the file for reading and writing purpose. This section explains how to read values from the screen and how to print the result on the screen.

The getchar() and putchar() Functions

The int getchar(void) function reads the next available character from the screen and returns it as an integer. This function reads only single character at a time. You can use this method in the loop in case you want to read more than one character from the screen.

The int putchar(int c) function puts the passed character on the screen and returns the same character. This function puts only single character at a time. You can use this method in the loop in case you want to display more than one character on the screen. Check the following example −

#include <stdio.h>

int main( ) {

    int c;

    printf( "Enter a value :");

    c = getchar( );

    printf( "\nYou entered: ");

    putchar( c );

    return 0;

}

When the above code is compiled and executed, it waits for you to input some text. When you enter a text and press enter, then the program proceeds and reads only a single character and displays it as follows −

$./a.out

Enter a value : this is test

You entered: t

The gets() and puts() Functions

The char *gets(char *s) function reads a line from stdin into the buffer pointed to by s until either a terminating newline or EOF (End of File).

The int puts(const char *s) function writes the string 's' and 'a' trailing newline to stdout.

NOTE: Though it has been deprecated to use gets() function, Instead of using gets, you want to use fgets().

#include <stdio.h>

int main( ) {

   char str[100];

   printf( "Enter a value :");

   gets( str );

   printf( "\nYou entered: ");

   puts( str );

   return 0;

}

When the above code is compiled and executed, it waits for you to input some text. When you enter a text and press enter, then the program proceeds and reads the complete line till end, and displays it as follows −

$./a.out

Enter a value : this is test

You entered: this is test

The scanf() and printf() Functions

The int scanf(const char *format, ...) function reads the input from the standard input stream stdin and scans that input according to the format provided.

The int printf(const char *format, ...) function writes the output to the standard output stream stdout and produces the output according to the format provided.

The format can be a simple constant string, but you can specify %s, %d, %c, %f, etc., to print or read strings, integer, character or float respectively. There are many other formatting options available which can be used based on requirements. Let us now proceed with a simple example to understand the concepts better −

#include <stdio.h>

int main( ) {

   char str[100];

   int i;

   printf( "Enter a value :");

   scanf("%s %d", str, &i);

   printf( "\nYou entered: %s %d ", str, i);

   return 0;

}

When the above code is compiled and executed, it waits for you to input some text. When you enter a text and press enter, then program proceeds and reads the input and displays it as follows −

$./a.out

Enter a value : seven 7

You entered: seven 7

Here, it should be noted that scanf() expects input in the same format as you provided %s and %d, which means you have to provide valid inputs like "string integer". If you provide "string string" or "integer integer", then it will be assumed as wrong input. Secondly, while reading a string, scanf() stops reading as soon as it encounters a space, so "this is test" are three strings for scanf().

Formated Input and output

1)      Format for integer Input:

Syntax:

%wd

Here “d” is the conversion specification char for integer value and “w” is an integer no. specifying the miximum firld width of input data. If the length of input is more than this maximum field width then values are not stored correctly

scanf(“%2d%3d”,&x,&b);

a)      When input data length is less than or equal to given width, the input values are unaltered and stored in given variables.

Input:

6   394

Output:

6 is stored in x and 394 is stored in y.

b)      When input data length is more than the given width, the input values are unaltered and stored in given variables.

Input:

269   3845

Output:

26 is stored in x and 9 is stored in y and rest input is ignored.

2)      Format for Integer Output

Syntax:

%wd

Here “w” is the integer no specifying the field width of the output data. If the length of the variable is less than the specified field width, the variable is right justified with the leading blanks.

printf(“a=%3d,b=%4d”,x,y);

a)      When the length of variable is less than the width specifier

Input:

78     9

Output:

a

=

 

7

8

;

b

=

 

 

 

9

 

b)      When the length of variable is equal

Input:

263 1941

Output

a

=

2

6

3

;

b

=

1

9

4

1

 

c)       When length of the variable is more than the widt specifier, then also the output is printed correctly.

Input:

2691 19412

a

=

2

6

9

1

;

b

=

1

9

4

1

2

3)      Format for floating point Numeric Input

Syntax

%wf

Here “w” is the total width (including the digits before and after decimal and the decimal itself)

scanf(“%3f %4f”,&x,&y);

a)      When the input data length is less than or equal to the given width, values are unaltered and stored in the variables.

Input:

5       5.92

Output:

5.0 is stored in x and 5.92 is stored in y.

b) When Input data length is more than the given width the entered values are  altered and store in the given variables

                Input:

5.93       65.875

Output

5.9 is stored in x and 3.00 in y.

4) Format for floating point Numeric Output

Syntax

%w.nf

Here “w” is integer no specifying the total width of the input data and n is the no of digits to be printed after decimal points. By default 6 digits are printed after decimals.

printf(“x=%4.1f,y=%7.2f”,x,y);

a)      If the total length of the variable is less than the specified width w, then the value is right justified with leading blanks. If the no of digit after decimal is more than n the digits are rounded off.

Input :

8    5.9

X

=

 

8

.

0

,

y

=

 

 

 

5

.

9

0

b)      If the total width are equal then

Input

25.3     1635.92

x

=

2

5

.

3

,

y

=

1

6

3

5

.

9

2

c)       If the total width is greater then

Input

15.231     65.87948

x

=

1

5

.

2

,

y

=

 

 

6

5

.

8

8

4)      Format for string input

Syntax

%ws

Here “w” is total no of char that will be stored in the string

char str[8]

scanf(“%3s”,str);

Input:

srivastava

Only first three char of this input will be stored in the string

‘s’,’r’,’i’,’\0’

The null character ‘\0’ is automatically stored at the end.

5)      Format for string output

Syntax

%wns

Here “w” is width, decimial point and n are optional. If present then n specifies that only firat n char of the string will be displayed and (w-n) leading blanks arte displayed before string.

a)      printf(“%3s”,”Sureshkumar”);

S

u

r

e

s

h

k

u

m

a

r

b)      printf(“%10s”,”Seeta”);

 

 

 

 

 

s

e

e

t

a

c)       printf(“%.3s”,”Sureshkumar”);

 

 

 

 

 

 

 

 

S

u

r

d)      printf(“%8.3s”,”Sureshkumar”);

 

 

 

 

 

S

u

r

(8-3=5 Leading Blanks)

Suppression Character in scanf()

If we want to skip any field, we can specify (*) between the % sign and the conversion specification. The input field is read but its value is not assigned to any address. Thus char * is called suppression character.

For example

scanf(“%d%*d%d”,&x,&y,&z);

Input:

25    30     35

25 is stored in x

30 is skipped

35 is stored in y

35 is stored in y

and in z garbage value is stored.

 

 

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.