.:: Welcome To My Personal Blog ::.

Sunday, July 3, 2011

Programming with C Tutorial - Part 2

Variables and Arrays :



A variable is an identifier that is used to represent a single data item i.e. a numerical quantity or a character constant. The data item must be assigned to the variable and this will then be easily accessible from the program by referring the variable name. A simple example of declaring and using variables is given below :
int a, b, c;
char d;
float e, f; 
a = 1;
b = 2;
c = 3; 
d = 'a'; 
e = 4.01;
f = 5.02; 
c = a + b;

The first three lines are the declaration of the variable of specific data types. The following lines are the use of those variables inside the program.

Array is another kind of variable but it refers to a collection of data item of same data type. They are represented with a single name. The individual array elements are distinguished from one another by the value that is assigned to a subscript. An example is given below:

int array[10];

This is the declaration of a variable for a collection of data items of integer data type. The name of this variable is "array" and it is a 10 element array. The first element is referred to as array[0], the second as array[1] and so on. The last element will be referred to as array[9]. For an n-element array, the subscripts always will be from 0 to n-1.

The description given here is for one-dimensional array. Following the same way, the two-dimensional array, three-dimensional array and other multi-dimensional arrays can be created.




Declaration:

A declaration associates a group of variables with a specific data type. All the variables used in a program must be declared before their use.

A declaration consists of the data type followed by one or more variable names. This line must be ended with a semicolon(;). For each array variable, it must be followed by a pair of square brackets containing a positive integer which specifies the size of that array.

       Example:
int a, b, c[10];
float d, e, f[15];
char g, h, i[20];
Here, the first line is the declaration for two single integer type variables named 'a' and 'b'. This also declares an array which may contain 10 integer type variables (represented by c[0], c[1], ....., c[9]).
The second line is the declaration for two single floating-point variables named 'd' and 'e'. This also declares an array which may contain 15 floating-point variables (represented by f[0], f[1], ....., f[14]).
The third line is the declaration for two single character type variables named 'g' and 'h'. This also declares an array which may contain 20 character type variables (represented by i[0], i[1], ....., i[19]).

The initialization of a variable can be done within it's declaration.
int a = 5;
float d = 2.5;
These are the initialization of 'a' and 'd'.

For an array, this can be done by the following way.


int a[5] = {5, 3, 8, 2, 9};


For the character type array, it can be done in two ways:


char arr[] = "Programming";
Or,
char arr[12] = "Programming";


For both the ways, an extra '\0' character will be added at the end of the string.




Expressions:


An expression represents a single data item, such as a number or a character. The expression may consist of a
single entity, such as a constant, a variable, an array element or a reference to a function. It may also consist
of  some  combination  of  such entities, interconnected  by  one or more  operators. Expressions can also represent logical conditions that are either true  or  false. However,  in  C  the conditions  true  and false  are represented  by  the integer values  1  and  0, respectively. Hence logical-type expressions really represent numerical quantities.

         Example:
a + b
x = y
c = a + b
x <= y
x == y
++i



Statements:

Expressions are the actions that are used in a program. There are three different classes of statements in C.  They are expression statements, compound statements and control statements. An  expression statement consists  of  an  expression followed  by  a semicolon. The execution  of  an expression statement causes the expression to be evaluated.

Example for expression statement:
int a=5, b=3, c;
c = a+b;
printf("%d", c);
Every statement must be ended with a semicolon(;). Only a semicolon with anything before it, means an empty statement.


A  compound statement consists of  several individual statements enclosed within  a pair  of braces  {  }. The  individual statements  may  themselves be expression statements, compound statements or control statements. A compound statement does not end with a semicolon.

Example for compound statement:
{        c = a + b;
          d = a - b;
          r = c * d;
          printf("r is %d", r);
}

Control statements are used to create logical tests, loops and branches and so on. Many control statements require that other statements be embedded within them. Control statements are normally used with "if", "while", etc.

Example for control statement:
while(c < n)
{
          c = c + 2;
          printf("c is : %d\n", c);
}




Symbolic Constant :

These are the constants assigned to a specific variable. The constant will be represented by that variable through the whole program. This is done by using "define". Symbolic constants are usually defined at the beginning of a program.

The format of this is the following:
#define   variable_name   value
Example:
#define PI 3.1415926535897932
#define TRUE 1
#define FALSE 0
In this example, the value 3.1415926535897932 will be used through the whole program by writing "PI" only.




Courtesy :
Schaum's Outline of Programming with C - Byron S. Gottfried, 2nd Edition. McGraw-Hill Publications













No comments:

Post a Comment

Popular Posts