BEGINNING WITH C – INPUT-OUTPUT IN C LANGUAGE

BEGINNING WITH C LANGUAGE
BEGINNING WITH C – READING INPUT DATA

Generally, we all enter data into the computer memory i.e. from a standard input device [keyboard]. Here, C provides us a number of library functions for entering input data. They are scanf(), getchar()getch(), etc.

 

First of all, we will discuss all about the Function scanf()..

1. scanf()

Generally, the syntax for scanf() Function is –

scanf(“control string”, address 1,…);

Basically, the scanf() function above should have atleast two parameters​. While, the first parameter will be a control string, that contains conversion specification characters. Also, we should always write it within double quotes( ” ” ). Furthermore, the conversion specification characters may be one or more. It only depends on the number of variables we or the user wishes to input.

Now, the other parameters in the syntax are the addresses of the variables. Generally, in the scanf() function at least one address should be present. Certainly, the address of any particular variable is found by preceding the variable name by ampersand sign[&].

Furthermore, the Ampersand sign[&] is the address operator. Generally, it gives the starting address of the variable name in the computer memory. Most noteworthy, to get the address of a string variable, we do not place & sign prior to the variable.

Following are some of the examples of scanf() function –

EXAMPLE 1 – TAKING INTEGER VALUE AS INPUT

#include<stdio.h>

#include<conio.h>

void main()

{

clrscr();

int number;

scanf(“%d”, &number);

…………..

getch();

}

In the above example, the control string contains only one conversion specification character i.e. %d.. Generally, this implies that the user should enter one integer value as input. Further, the compiler will store that value in the variable name. 

EXAMPLE 2 – TAKING CHARACTER VALUE AS INPUT

#include<stdio.h>

#include<conio.h>

void main()

{

clrscr();

char character;

scanf(“%c”, &character);

…………..

getch();

}

In the above example, the control string contains one conversion specification character i.e. %c.. Generally, this implies that the user should enter one character value as input. Further, the compiler will store that value in the variable name i.e. character.

Also, this procedure will be same for taking float values as INPUT.

Leave a comment

Your email address will not be published. Required fields are marked *