Basic input, output and variables

1.5 Basic Input, Output and Variables
Let’s learn about Input and Output in our programs along with variables.
There are many ways by which an input may be taken into the program, keyboard, mouse or touchscreen! Here we only deal with the keyboard and use the included “stdio.h” file which provides us the basic input-output functions. For now, remember that functions are bunch of instructions written for the sake of using it multiple times when necessary.
So, as the function “printf()” or any which is defined in the file “stdio.h” which is included in our program, we can use “printf()” anywhere necessary.
Similarly, “scanf()” is a function which can take input from the keyboard and is defined in “stdio.h”. But where will you store the input took from “scanf()” ? You’ll store all kind values or the inputs from the user in the memory and a simple way to do this is by storing them in Variables.
In simple terms variables are certain names given to a particular kind of memory allocation. Let’s get into variable declaration. General syntax or rule by which variable is defined in C is
 data_type   variable_name ;
The first part is of datatype as we’ve discussed in earlier tutorial, you may use any keyword such as int, float etc. to define the datatype.
Next you can name the variable in any way you desire with specific regulations. The variable name couldn’t start with numbers or any special characters except an underscore. The semicolon indicates termination of variable declarations
So in general a variable may be declared as
int count ;
You may use “,” (comma) to declare several variables at once
int count, _Hello, _num ;
Now the above statements only allocate memory the size of which is defined by the datatypes. To store any data in the memory you can simply assign it via assignment operator.
int count;
count = 100;
or simply
int count = 100;
Always the memory allocated is referred by the variable name. And if you try to assign any float value to a variable of int it will generate an error while compilation.
So, now let’s go with “scanf()”. We use it to take input from the user. Lets understand how it works. Scanf() takes two parameters. The parameters are separated by a comma(,).
In the first parameter we must specify the datatype by means of a datatype qualifier. Then the corresponding variable in which data to be stored is entered in the second parameter.
For example,
int numberEntered;
scanf( “%d”, &numberEntered );
In the above example first we’ve declared variable numberEntered and then by using scanf() we’ve took an integer input from the user and stored it in the variable.
If we look closely in the above example in the first parameter we’ve used “%d” as the qualifier which is of int datatype. Then the variable numberEntered is in second parameter with “&” sign placed before it.
This sign is called “address of” operator and it gives the address of variable. It simply indicates to store the value entered by the user in the address of the variable numberEntered.
You can combine multiple user inputs into one scanf() statement
For example
int  numberEntered;
char selectedSymbol;
scanf(“ %d %c”, &numberEntered, &selectedSymbol);
Now “%c” is the data-type qualifier for char. The user must input the data accordingly as the first entered data must be an integer and the next be a character and hence the variables are ordered accordingly.
To print characters on the screen we use printf(). It takes a string as a parameter. In our first hello world code we passed “Hello world” which is a string. String are characters enclosed in quotes ( “ ” ).
printf(“ This is a string ”);
This outputs – This is a string on the screen. But in order to print any data in the variables, we can do it by the same way as that of scanf().
int numberEntered;
printf(“Enter any Number: ”);
scanf( “%d”, &numberEntered );
printf(“You’ve entered %d”, numberEntered);
In the above code we’ve declared a variable, asked the user for the input by the printf() statement, took the input by scanf() statement and finally displayd the user entered number in a pleasant way.

Here in the last statement we’ve used “%d” in the string passed as the first parameter to the printf() statement. That datatype corresponds to the numberEntered variable which is ought to be printed on to the screen.

Comments

Popular posts from this blog

Data Types in C

Variable