KEMBAR78
C Programming: Basic I/O Guide | PDF | Computer Science | Computer Engineering
0% found this document useful (0 votes)
45 views10 pages

C Programming: Basic I/O Guide

Uploaded by

Deepak
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views10 pages

C Programming: Basic I/O Guide

Uploaded by

Deepak
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Programming With C-Basic Input Output

Input Output Statements

In programming we have to accept users input and after evaluation of the result output should be displayed.
C provides functions that interact with the standard input usually keyboard and standard output usually
screen. C has also functions to accept data from files and store output in the specified file.
Single Character Input output
To accept single character input we can use the function, getchar (), getch () and getche() .
The getchar () function
 The function getchar () is provided through stdio.h header file.
 This is a buffered input i.e. after typing the character input we have press enter key, then the character
will be stored to assigned variable.
 The typed character will be echoed on the screen.
The getch () and getche () function
 The function getch () is provided through conio.h header file which is part of non standard C .
 This is direct input i.e. after typing the character input we do not have to press enter key, it will directly
send the pressed character to assigned variable.
 The typed character will not be echoed on the screen in case with getch () while with getche () typed
character will be displayed on the screen .
Created and verified By: B. K. Deepak 39
Dated:20/12/2004
Rev:00
Programming With C-Basic Input Output

For Example
char ch;
ch=getchar();

ch is declared as character variable in which we can store character type data. The second statement
caused the computer to halt and weight for character input. It will accept character after receiving the enter
key and pressed character will be assigned to variable ch.
We can use getch () and getche () in place of getchar () as per program requirement. Nothing should be writ-
ten inside the parenthesis.

To print the computer output we can use putchar () and putch ().
For example
putchar (ch);
The value stored in variable ch will be displayed on the screen. We can use putch () in place of putchar().
The getchar(), getch() and getche() does not take any parameter and returns the value of input charcter.
In putchar() and putch() we have to pass the character variable as parameter which will be displayed on the
screen.

Created and verified By: B. K. Deepak 40


Dated:20/12/2004
Rev:00
Programming With C-Basic Input Output

String input output


The function gets() receives the string from the standard input device, while puts output the string to standard
output device. For example
char s[80];
gets(s);
puts(s);

The char s[80] will create a variable that will store character up to 79 character long i.e. s becomes a string of
characters.
The gets(s) cause computer to accepts the user input until enter key is pressed. After getting enter key the
string of character stored in buffer will be assigned to variable s.
The puts(s) will display the string on the screen.
Escape sequence:
Inside the single quote, a backslash character starts an escape sequence.
\a Beep (alert) \b Backspace \0 Null \0ooo code in octal
\f formfeed \n newline \? Question mark \xhh code in hex
\t horizontal tab \r carriage return \v vertical tab
\\ backslash \’ single quote \” double quote

Created and verified By: B. K. Deepak 41


Dated:20/12/2004
Rev:00
Programming With C-Basic Input Output

The general Input/output function printf() and scanf()


The printf () function is a very versatile output function. It can handle any basic data type and offer formatted
output to standard output device. The printf () function does the following:
 Accept a format string followed by series of arguments.
 Apply to each argument, the corresponding format specifiers contained in the format string.
 Output the formatted data in desired format.
1. printf(”format string”);
2. printf(”format string”,var1,var2,var3);
Format string consists of Prompt Message with/without format specifier.
Format specifier used when printf () function contains variables whose values is to be displayed.

Format Specifiers Type of arguments Output


%d int signed decimal integer
%i int signed decimal integer
%u unsigned int unsigned decimal integer
%ld long decimal long integer
%li long decimal long integer
%lu unsigned long decimal unsigned long integer

Created and verified By: B. K. Deepak 42


Dated:20/12/2004
Rev:00
Programming With C-Basic Input Output

Format Specifiers Type of arguments Output


%hd short int decimal short integer
%hi short int decimal short integer
%hu unsigned short decimal unsigned short integer
%o int octal integer
%x, %X int Hexadecimal integer
%f float fractional form
%e, %E float exponential form
%g, %G float exponential form
%lf, %le, %lg double signed double
%Lf, %Le, %Lg long double signed, long double
%lo long octal long integer
%lx long hexadecimal long int
%c char single character
%s sequence of character print until null character
%% None print % character
%p None pointer type

Created and verified By: B. K. Deepak 43


Dated:20/12/2004
Rev:00
Programming With C-Basic Input Output

float f_var=10.12576893;
int wid_prec=13;
printf(”\n\tOutput 1: %f”, f_var);
printf(”\n\tOutput 2: %20f”, f_var);
printf(”\n\tOutput 3: %-20f”, f_var);
printf(”\n\tOutput 4: %+20f”, f_var);
printf(”\n\tOutput 5: %*f”, wid_prec, f_var);
printf(”\n\tOutput 6: %.5f”, f_var);
wid_prec=8;
printf(”\n\tOutput 7: %20.*f”,wid_prec, f_var);
printf(”\n\tOutput 8: %-+20.5f”, f_var);

Output 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

Output 1 1 0 . 1 2 5 7 6 9

Output 2 1 0 . 1 2 5 7 6 9

Output 3 1 0 . 1 2 5 7 6 9

Output 4 + 1 0 . 1 2 5 7 6 9

Output 5 1 0 . 1 2 5 7 6 9

Output 6 1 0 . 1 2 5 7 7

Output 7 1 0 . 1 2 5 7 6 8 9 3

Output 8 + 1 0 . 1 2 5 7 7

Created and verified By: B. K. Deepak 44


Dated:20/12/2004
Rev:00
Programming With C-Basic Input Output

printf(”Output 8: % - + 20.5 f \n”, f_var);

Type Specifier
Precision Specifier

Width Specifier

Flags

Beginning of the format specifier

Input function scanf()

scanf () is used to accept any type of input through standard input device. scanf () needs to know where
one input field ends and where the next begins, the input fields separated by typing spaces, tabs, newline
(enter) characters or combination of these. Six white space characters are used to separate input fields.
These are white space, newline, tab, vertical tab, carriage return, form feed.
The function scanf () accepts format string as first parameter and followed by series of addresses of va-
riables whose values are input.
It returns an integer that gives the number of input fields scanned successfully.
Syntax: scanf(”format strings”, &var_1, &var_2);

Created and verified By: B. K. Deepak 45


Dated:20/12/2004
Rev:00
Programming With C-Basic Input Output

Types of Characters in format string


Whitespace
If scanf () encounters a white space character in the format string, it reads, but does not store consecutive
whitespace characters till the next non-whitespace character in the input

Ordinary Characters
The ordinary characters are characters that are not whitespace characters, except the percent sign (%). If
scanf () encounters an ordinary character in the format string, it will read but not store a matching charac-
ter.

Format Specifiers
The format specifiers direct scanf () to read and convert the characters from the input field into specific
types of values and then store them in the locations given by the address arguments.

printf(”\n\tInput the day, followed by the date (dd-mm-yyyy) : ” );


scanf(”%s %d-%d-%d”, day, &dt, &mn, &yy);
printf(”\n\tDay : %s”,day);
printf(”\n\tDate : %d %d %d”, dt, mn, yy);

Created and verified By: B. K. Deepak 46


Dated:20/12/2004
Rev:00
Programming With C-Basic Input Output

In the previous program segment, the format specifiers are %s and the three %d specifiers. There is the
format string %s %d-%d-%d. There is a whitespace character after the first format specifier %s. We can
type any number of whitespace to separate first input (a string) from rest of input. The two hyphens are
ordinary characters. These instruct scanf () to look matching character in input stream. If any character
other than a hyphen is entered to separate the three integers, scanf () would not have succeeded in
scanning the input field.
If we provide input as: Sunday 12-11-1987
Then the output will be:
Sunday
12 11 1987

If we provide input as: Sunday 12 11 1987


Then the output will be:
Sunday
12 0 2345
The scanf () will scanned successfully the first two fields successfully. After first integer it expect (–) sign
but could not find it. So the variables mn and yr are not scanned at all. Hence garbage value displayed in
the output.

Created and verified By: B. K. Deepak 47


Dated:20/12/2004
Rev:00
Programming With C-Basic Input Output

In the scanf () function we can use the width specifier in format string. We can use the statement like
scanf(”%4s %5s”, string1, string2);
Where string1 and string2, both are character array. If input will be given as: Morning comes
The first four character (Morn) will be assigned to string1 and (ing) will be assigned to string2.
Search set
We can use search set in format string. Search sets are the set of characters enclosed in square bracket [
and ]. These search set are the parts of some format specifier. E.g. %[/-] . It tells the scanf to match any
sequence of characters consisting / and - . All these characters are assigned to the corresponding argu-
ment. If the first character is ^ (caret) the search set is inverted. We can also use range facility by using –
sign.
 To search the input field for any of the characters a, b, c, and d %[abcd] or %[a-d]
 To search the input field for any character except a, b, c, and d %[^abcd] or %[^a-d]
 To scan all decimal digits and letters %[0-9A-Za-z]
Assignment suppression character : The assignment suppression character is %*.
scanf(”%d%[/-]%d%[/-]%d”, &dt, sep, &mn, sep, &yr);
scanf(”%d%*[/-]%d%*[/-]%d”, &dt, &mn, &yr );
Exercise: What is the difference between scanf () and gets () function with respect to data input with em-
bedded white space character?

Created and verified By: B. K. Deepak 48


Dated:20/12/2004
Rev:00

You might also like