Input Output Instruction

User interaction with a program on machine required input/output device
Standard Input device is keyboard and standard output device is monitor.

An instruction which is used to print something on the screen or any other output device is known as output instruction. Similarly, an instruction which is used to input information from the user is known as input instruction.






Output Instruction
printf() is a pre-defined function used to display text on the monitor. You can learn about printf() through my video lecture Click here for video

Syntax

printf(“format string”, variable list);

Format string is a message that you want to print on the screen.
Variable list is optional. We can also write expression in place of variable list.

Example

printf(“Saurabh Shukla”);
printf(“Hello SCA”);

Write your first program
int main()
{
printf(“Hello SCA”);
return(0);
}

Step 1: Save this program as first.c
Step 2: build it
Step 3: Run
Note: when you run exe file of this program directly (by clicking on it and without code::blocks) your program executes in a flash and output window disappears immediately. To overcome this issue, modify your program as follows:

int main()
{
printf(“Hello SCA”);
getch();
return(0);
}

Output:

Hello SCA

Program executes line by line, printf is the first line, which prints Hello SCA on the screen. getch() is the next line. The job of getch() function is to take one character from the keyboard. So the program will not terminate until you presses a key. Last line is return statement. return is a keyword in C language whose job is to return a number (zero in our example) to the caller (caller is Operating system in our example).

As a beginner, you might get confused with the terms ‘function’, ‘return a value’ and ‘caller’. You are going to learn about these terms in later chapter covering functions topic.

Just for a glimpse of what function is all about, function is a block of instruction set. you can say it is a sub program. Function has a name for identification (like printf() and getch()). Function names in the program are actually representation of code, which is already created and stored somewhere.

Printing values of the variables

Suppose you have declared a variable in a program and want to display its value on the screen.

int main()
{
int a=5;
printf("%d",a);
getch();
return(0);
}

Output:
5

In the above program we have declared a variable ‘a’ of type int and assigned a value ‘5’. Next we print value of a. If you would write printf(“a”) it will have output a on the screen. So whenever you want to print value of a variable, always use special symbols like %d (format specifier) in the double quotes and corresponding variable name should be without quotes.

Please refer to the chart for the complete list of format specifier. In the chart you can see, which format specifier should be used to a specific data type.

In above example variable is of type int therefore %d is used.

Write a program to display values of two variables on the monitor. Declare these two variables of any type and assigned some values to them.


int main()
{
int a=3,b=56;
printf(“%d %d”,a,b);
getch();
return(0);
}


We can write expressions in place of variable list

int main()
{
int a=3,b=56;
printf(“%d %d %d”,a,b, a+b);
getch();
return(0);
}

Input Output Instruction
Tagged on: