#4 Input Output Instruction
#4 Input Output Instruction
Computer program can communicates with human with the help of input output devices. You
provide input to the program by using various input devices like keyboard, mouse, scanner, touch
pad, etc. Similarly computer program can generate output of the processing on output devices like
monitor, printer, speaker, etc.
Among all such available input and out devices, monitor is standard output device and keyboard is
standard input device.
An instruction which is responsible to handle input from input device is called input instruction.
Similarly, an instruction which is responsible to handle output on output device is called output
instruction.
Output instruction
To output any text on the screen of monitor, we use a predefined function called printf().
What is function? You will come to know about function in great detail in functions chapter. As of
now, function is a block of code, designed to perform a task (assume it as a sub program ). Function
has a name for identification. Function name can be used as a representation of some code.
Function printf() is also a representative of some code residing in the C library. You can invoke that
code any number of times by using name printf(). The code is responsible to print something on the
screen.
Above screen shot is the program to print name Saurabh Shukla. You can write anything in double
quotes inside the parenthesis of printf().
Note:
() pronounced as parenthesis
< > Angular brackets
[ ] Square brackets
{ } Curley braces
About getch()
You can use it as a trick. In the previous program, you cannot see output because as soon as the
printf() statement completed its job, program terminates, output screen vanishes. There is only one
line in the program, so we strategically placed getch() function after printf() to avoid screen
termination. Until you presses a key, the last line (getch()) is not over. So you will be able to see
output screen, as it will only shut down when the last line finishes its job.
You can use clrscr() function to clear screen. It erases all the content which is present on the screen.
You cannot use clrscr() in code blocks. You can use system(“cls”) in its place, if you want.
You can print your name on the first line and surname on the second line by using special character
‘\n’.
#4 Input Output Instruction
C Notes by Saurabh Shukla
www.mysirg.com
Remember, using another printf is not going to print surname on the next line. You have to use ‘\n’
which is a special character also known as new line character.
You can experiment other such special characters, known as escape sequences.
Escape Sequences
\r carriage return, it moves cursor at the starting place of the current line
You can experiment these special character by using them in printf and see the output.
Remember: In C language you cannot write declaration statements after action statement.
(Although in C++ it is permitted)
%d for int
%c for char
%f for float
%d, %c, %f, %lf are called format specifiers. They are special symbols used to tell printf about the
format in which data should be printed.
printf(“%d”,a);
The above statement prints the value of variable a. Here you have to assume that the variable is of
int type. Basically %d, tells the printf to print content of a (which is a sequence of 0s and 1s) after
converting into a number in base 10, that is decimal number system.
#4 Input Output Instruction
C Notes by Saurabh Shukla
www.mysirg.com
You can print a mixed message which contains your text as well as value of variable.
Input Instruction
Most of the time you want to process data which is provided by user through keyboard. You expect
from user to provide needful data through keyboard, which you can take in program’s variables, and
then use them in processing task. To achieve this goal you need to write some instruction, better
known as input instruction.
scanf() function
Just like printf, scanf is also a predefined function. The job of scanf() to input data from keyboard.
(Actually, data which comes from keyboard, settled in a standard input buffer, from where scanf()
pulls data and convert it to the specified format, which you have mentioned during call of scanf )
Syntax of scanf()
For example, if there is a variable x of type int, now you want to allow user to input an integer value
through keyboard and then your program can store it to variable x. The job can be done as:
scanf(“%d”,&x);
In the above statement, ‘&’ is a dereferencing operator which you will see later in pointers chapter.
It should be read as ‘address of’.
#4 Input Output Instruction
C Notes by Saurabh Shukla
www.mysirg.com
Remember: Do not forget to mark address of operator before variable name in scanf function, it is
important. Also, do not accidently use address of operator in printf. You will understand its usage in
pointers chapter.
You can input more than one value using single scanf statement.
Remember:
User proper format specifier in printf and scanf, according to the need and type of variable
Do not forget to use address of (&) operator in scanf before variable name
Do not write any symbol between two %d when used in scanf, like comma, it will then not
work according to expectation.
Here is a sample program to calculate square of a number, where number is given by user.
#4 Input Output Instruction
C Notes by Saurabh Shukla
www.mysirg.com
Answers
Program lines executes line by line. If we write scanf without prior printf, you would see a blank
output screen. No message (no direction) for the user. So it is a nice idea to guide user what he
should do. Printing a message like “Enter a number” makes your program more users friendly.
Statement s=x*x; should processed only when you received value in variable x. So writing statement
s=x*x just after declaration and before scanf is a big mistake, as in that case it would be processed
before getting value in variable x.
Yes you can make the same program without using variable s.
#4 Input Output Instruction
C Notes by Saurabh Shukla
www.mysirg.com
References
Exercise
1. Write a program to print Name on the first line and surname on the second line
#4 Input Output Instruction
C Notes by Saurabh Shukla
www.mysirg.com