Variables, Constants, Keywords & Datatypes

All programs deal with some sort of data, a program accepts some data from the user and manipulates it and provide the manipulated/processed data - the information. We may use different kinds of data in our programs, it may be a constant value, or a varying type known as variable. The type of data's used may be different, for example in a mark list preparing program the datas may include student_name,subject,mark,roll_no,avg etc., student_name is a data which uses some characters to form a student name, roll_no represents student's roll number and it may be a number-an integer. Likewise avg may be a decimal value. So we are dealing with different kinds of data.

Constants are data value which is predefined before a program is used. Values remain unchanged during the program execution. For example "C Program" is a string constant.

Variables are identifiers which we used to represent or identify a data. Variables are varying type values. Here the data may change or be assigned different values as the program runs. For example

emp_salary=12000; (emp_salary is a variable which holds the value 12000)
emp_salary=12500; (now emp_salary contains the value 12500)

There are some rules when using variable names.

Rules of naming variables

1. Variable name should only contain alphabets (a-z,A-z), digits(0-9) and underscore(_).
2. Variable name must start with an alphabet or an underscore.
3. Variable name should not contain any blank space.
4. Pre defined words in C (known as Keywords) cannot be used as a variable name.

Care should be taken while program in C as keywords or variables used in C are case sensitive.

Keywords are the basic constructs used to write a program. Keywords have a predefined meaning in C. For example int is a keyword used to declare an integer variable. Keywords cannot be used as a variable/function name.

Variables are used to represent a data in a program, each data may be of different type, age data may be a numeric type(integer). Grade of a student can be a character. So all variables used in a C program must be declared to the appropriate type before we use those variables. Each variables thus declared possess some space of memory to hold the value it represents. In C data types can be divided as Primary and Secondary(Derived).

Primary Data types : int, char, float, double. int data type can be used to represent an integer value, char data type is used to represent a single character, float data type is used to store a decimal value, double data type is used to declare a double precision floating point. Each type have a range of values that it can hold, and each type allocates a specific byte of memory for the value.

1. int
Range : -32768 to +32767
Memory : 2 bytes
Control String : %d

2. char
Range : -128 to +127
Memory : 1 byte
Control String : %c

3. float
Range : 3.4e-38 to 3.4e+38
Memory : 4 bytes
Control String : %f

4. double
Range : 1.4e-308 to 1.4e+308
Memory : 8 bytes
Control String : %lf

int data type allocates 2 bytes to store an integer number. A variable declared as int can hold an integer value in the range of -32768 to +32767. If you want to store an integer above this range instead of int you may use long int, control string of long int is %ld. Control string is associated with C data types and you are required to use appropriate control string with each variable used in your program while you read a data to the variable or print a data in variable. Control string enables the format of data in the variable.

Secondary Data types can be created from the existing data types. It includes arrays, structures, unions, enums, linked lists, pointers etc.

Declaring a Variable : You can declare a variable by specifying the data type of that variable.

Syntax : <datatype> <variable_name>;

int age; /*Declare an integer variable age.*/

float avg,mark; /*2 variables are declared. 4 bytes allocated to each variable, and can store any values which falls in the float range.*/

float avg; /*variable avg is declared*/
avg=98.74;/*avg is initialized with value 98.74*/

char ch='A'; /*a character variable ch is declared with the value A.*/

So now have a look at the program with some variables

/*Program to print variable values */
#include<stdio.h>
#include<conio.h>
main( )
{
int num=850;
float avg=98.54;
double dblval;
char ch='S';
dblval=99745.5684;
clrscr( );
printf("Character ch=%c",ch);
printf("\nInteger num=%d",num);
printf("\nFloat avg=%c",avg);
printf("\nDouble dblval=%lf",dblval);
/*printf("ch=%c\nnum=%d\navg=%f\ndblval=%f",ch,num,avg,dblval);*/
getch( );
}

Previous
Next

0 comments:

Design by The Blogger Templates The Blog Full of Games

© CourseZone
2008 - 2010 All Rights Reserved.