Friday, July 21, 2017

Input Output in C

Input and Output in C

Declaration and Initialization of Variables:

Any variable in C programming language must be declared before using it. Variables are declared using the following rules-

Integer type variables are declared using the “int” keyword before the variable name. More than one variable can be declared in a single statement. Syntax for declaring integer type variables is given below-

int <list_of_variable_names>;

For example:

int n;
int basic, hra;

Real or floating type variables are declared using the keyword “float” before the variable name. Syntax for declaring floating type variables is given below-

float <list_of_variable_names>;

For example:

float a;
float rate, gross_salary, weight;

Character type variables are declared using the keyword “char” before the variable name. Syntax for declaring character type variables is given below-

char <list_of_variable_names>;

For example:

char c;
char x, y, symbol;

Variables can be initialized or given values at the time of declaration.

For example:

int n=12, a=5;
float x=1.5, y=3.7;
char a=’t’, b=’#’;

In C programming language variables should be declared in the beginning of any function.

For example:

int main()
{
      int n1, n2=25;
      float length=45.8;
      statement;
      …;
      …;
}

Input and Output:

There should be a way for interacting with the outside world. There are so many readymade library functions for input and output in C. Two of them are explained here. Others will be explained, whenever and wherever they are used.

For outputting messages, values of variables, results of expressions, etc. function printf() is used. The format of this function is given below-

printf(“<format_string”, <list_of_variables>);

In the format string, “%d” is used for printing integer values, “%f” for floating point values, and “%c” for character values. These are called format specifiers. You can give any other characters too in the format string. These characters will be printed as they are. The number and order of format specifiers should be same as that of the variables to be printed. To use the function printf(), include the instruction #include<stdio.h> at the beginning of the program. This instruction is preprocessor directive and will be explained later. A few examples of this function are given below-

printf(“Number of students : %d”, num);
printf(“%d %f”, num, weight);
printf(“%c”, a);
printf(“Basic salary is %d\nGross salary is %f”, basic, gross);

Please note that in the last statement, “\n” has been used in the format string. This is the newline character, and it takes the cursor to the new line, wherever it is used. That’s why; output of this statement will be shown in two lines like this-

Basic salary is 5000
Gross salary is 8500.50

If you want a program to receive input from keyboard during execution, here is a library function, function scanf(). It is the counterpart of function printf(). The format of this function is given below-

scanf(“<format_string>”, &variable1, &variable2,…);

The explanation of format string is same as in the function printf(). You should give the format specifiers in the same number and order you want to receive the values of variables from the keyboard. The ampersand (&) before the variable name is the “address of” operator. It gives the location number or address of variable in the memory. When we say &variable_name, it tells to the function scanf() at which memory location, value, supplied by the user through the keyboard, should be stored.

For example:

scanf(“%d %d %d”, &n1, &n2, &n3);
scanf(“%f”, &weight);
scanf(“%c”, &ch);

If you want to receive more than one value at a time from the keyboard using only one scanf() statement, supply these values using either <space>, or <tab>, or <enter> keys during program execution.

Now, whatever you have learnt till now, apply in a C program. Please type this program in your editor.

/* Program to add three numbers */
#include<stdio.h>
#include<conio.h>
int main()
{
       int n1, n2, n3, sum;
       clrscr(); /* clears the screen */
       printf("Enter 3 numbers: ");
       scanf("%d %d %d", &n1, &n2, &n3);
       sum=n1+n2+n3;
       printf("Sum of 3 numbers is %d", sum);
       getch();  /* gets a character from
                          keyboard, but does not
                          echo it */
       return 0;
}

Compile and execute this program. Output of this program would be like this-

Enter 3 numbers: 12
25
33
Sum of 3 numbers is 70

In this program, whatever written between /* */ is comment entry, and will not be executed.

/* Program to add three numbers */

Next two lines are preprocessor directives, which you will learn in later posts. These lines simply asks to compiler to add two files (stdio.h and conio.h) in the program. File “stdio.h” is needed for printf() and scanf() functions, and file “conio.h”, for clrscr() and getch() functions.

#include<stdio.h>
#include<conio.h>

In this program, there is only one function, called main(). A C program can contain more than one function. Function main() is necessary for execution and execution starts from function main(). Return type of this function is “int”, which means, it returns an integer value to the operating system. You will learn more about return type of a function in the later posts.

int main()
{
       …;
       …;
       …;
return 0;
}

The first thing we do in a function is to declare all the necessary variables. In this program, we have declared four integer type variables.

int n1, n2, n3, sum;

Next statement clears the screen and positions the cursor at the top-left corner of the screen. We do this, because it may be that the screen is showing something from the previous program. Function clrscr() is a library function.

clrscr();

Next statement prompts the user to enter three numbers.

printf("Enter 3 numbers: ");

Now, function scanf() waits for the numbers to be entered. You can enter three numbers separating them either by pressing <space>, or <tab>, or <enter> key.

scanf("%d %d %d", &n1, &n2, &n3);

The next statement adds the numbers and assigns the result in the variable “sum”.

sum=n1+n2+n3;

+” is the arithmetic operator for addition. “=” is the assignment operator. It is different from “equal to” operator in mathematics. It assigns the value of right-hand-side expression to the left-hand-side variable. There are 45 operators in the C computer language. You will learn more about operators in the later posts.

Next statement prints the result on the screen.

printf("Sum of 3 numbers is %d", sum);

Now, the function getch() waits a key-press. If we don’t use this function, output screen would not show up. That’s why, to hold the screen we have used this function.

getch();

Last statement returns the value ‘0’ to the operating system. ‘0’ indicates success.

return 0;

Now, this simple program is well explained. I hope you have a good foundation at your disposal. I recommend you to read all my posts from the very beginning.


If you are interested in how to create simple graphics in C, please read my blog Graphics in C.

No comments:

Post a Comment

C Operator Precedence and Associativity

C Operator Precedence and Associativity Precedence of Operators in C: Operator precedence tells the order, in which operations are per...