Input/Output instructions in C

Input-Output Instruction:

1)scanf()===Input instruction
2)printf() or getch()==Output instruction
User interaction with a program on machine required input/output device
The standard Input device is keyboard and the 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);
The format string is a message that you want to print on the screen.
Variable list is optional. We can also write an expression in place of the 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
The 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 press a key. The last line is the 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 the later chapter covering functions topic.
Just for a glimpse of what function is all about, the function is a block of the instruction set. you can say it is a subprogram. Function has a name for identification (like printf() and getch()). Function names in the program are an actual 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 an on the screen. So whenever you want to print the 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 the above example, variable is of type int, therefore, %d is used.
Write a program to display the 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 the variable list
int main()
{
int a=3,b=56;
printf(“%d %d %d”,a,b, a+b);
getch();
return(0);
}

No comments:

Post a Comment

Featured Post

Core and Advance Python

                      Language Fundamentals