So far we have learned the basic C programming architecture. However, that is not all with programming. Mostly, it is required to accept some input data from user, process it and then produce and show results.
So , in this chapter, we will learn following concepts
* Variables: definition, declaration and usage
* Input Function
* Processing Data
* Showing Results
What is meant by the term VARIABLE?
Variable can be defined as “a named memory space to store data of particular type”. The type of data being stored in variable is known as the DATA TYPE of Variable.
The term DATA TYPE
C offers 5 primary data types to allocate space within memory and store data. These are:
* int (occupies 2 Bytes in memory, accepts whole numbers)
* char (occupies 1 Bytes in memory, accepts single character,alphabet or Symbol )
*float (occupies 4 Bytes in memory, accepts large fractional/double precision value)
* double (occupies 8 Bytes in memory, same as float)
* void (occupies 0 Bytes in memory, valueless, used as return types of functions)
The other data types defined using PRIMARY DATA TYPES are known as SECONDARY DATA TYPES such as ARRAY and STRUCTURE.
Data Type signifies the type of value along with the Storage Capacity of the variable in the memory.
Variable means changeable ;thus the value within variable can be changed at any moment within program scope.It must be remembered that the data stored within variables gets destroyed as soon as the program terminates.
How to define VARIABLE?
<type> var_name; -----------------(1)
or,
<type> var1, var2, … , varn; -----------------(2)
For single variable declaration of any type, use the syntax (1) and for multiple variable declaration of same type use syntax (2).
Rules for Naming Variable
* Must be declared just at the beginning of main block().
* Variable name must begin within an alphabet.
* No spaces allowed.
* No two variables(same or different data types) can have same name.
* Special characters/symbols such as :;,./<> @#$%^&*()!~+=|\ etc not allowed. In case, variable name is multiword, join it using hyphen(-).
Example:
int ecode; --------------------(Correct)
but, the below given examples of variable declaration are all incorrect
int e code; --------------------(Space Not Allowed)
or
char e%name; --------------------(Special Symbol Not Allowed)
or
double 1sal; -----------------(Must begin within an Alphabet)
How to Input Values in variable?
Like the Output Function(used to show any message on screen), C offers Input Function to accept input from CONSOLE(i.e. Keyboard) and assign it to the specified variable. Following is the general syntax of input function,
scanf(“Format Specifier”, & var_name);
Understanding the scanf()
scanf() let your program to wait for console input by user and assign the data in mentioned format to the variable’s address.
Following is the list of format specifiers for the different data types , you could use to define variables:
int ---------- %d
char -------- %c
float -------- %f
double------%lf
It must be noted that for Double Precision Values , Double Data Type is preferred rather than float.
Also, in case you forget to mention & while scanning the variable, the entered data will not be assigned to actual memory location and your variable will contain GARBAGE DATA.
How to Output Variable’s Value
printf(“format specifier”,var_name);
or , if you want it with some message,
printf(“Message1 format specifier ”, var_name);
How to prepare EXPRESSION
Any C Expression can be prepared using following general format
res_var= var1 op1 var2 op2 …;
Here, the var1, var2,… are known as Operands and Op1, Op2,… are known as Operators(Mathematical Operators, the symbols that defines the working among two values of same type). The Data Type of Operands and the Result Variable must be same;otherwise Implicit Data Conversion will take place and may sometimes cause, DATA LOSS.
The permissible Mathematical Operators are:
+ – / % * ()
Along with this you may use the MATH.H library function (discussed in later chapters)
Implementing the Data Input, Processing and OUTPUT
This program accepts Employee Code, Salary from User and calculates Bonus as 12.5 % and Net Salary (Salary+Bonus)
#include <stdio.h>
main()
{
int ecode;
double esal,ebonus,net_sal;
clrscr();
printf(“"\n Enter Employee Code:”);
scanf(“%d”,ecode);
printf(“\n Enter Salary:”);
scanf(“%lf”,&esal);
// bonus being calculated as 12.5% of salary.
ebonus= esal* 12.5/100;
net_sal=esal+bonus;
printf(“\n Employee Code is :" %d”,ecode);
printf(“\n Employee Basic Salary : %lf”,esal);
printf(“\n Employee Bonus : %lf”,ebonus);
printf(“\n Employee Net Salary : %lf”,net_sal);
getch();
}
Press CTRL +F9 to compile and execute the above code.
Self Assessment Question
1. What is Operator and Operand in C Expression?
2. Give the Mathematical Operator Permissible in C Expressions?
3. Define the default Input Function scanf() in C?
4. How to Output any variable’s value along with some message?
5. Give the list of Format specifiers used to accept values in Primary Data Types in C?
6. What is variable. How to define it. Give the general rules to be remembered while defining variables?
7. Write C program code for following
a. Accept two values from user and calculate their SUM, DIFFERENCE, MULTIPLICATION and DIVISION and Output the results on screen.
b. Accept Sales amount from user and calculate the SALES Commission as 15 % of the sales amount