DISHA NIRDESHAN

Enlightens your career

Wednesday, June 8, 2011

Questions on Variables and Arrays

 

What is the difference between declaring a variable and defining a variable?

Declaring a variable means describing its type to the compiler but not allocating any space for it. Defining a variable means declaring it and also allocating space to hold the variable. You can also initialize a variable at the time it is defined.

 

In C, what is the difference between a static variable and global variable?

A static variable declared outside of any function is accessible only to all the functions defined in the same file (as the static variable). However, a global variable can be accessed by any function (including the ones from different files).

  What is difference between for loop and while loop and do-while loop in C language?

for loop: When it is desired to do initialization, condition check and increment/decrement in a single statement of an iterative loop, it is recommended to use 'for' loop.

Syntax:

for(initialisation;condition;increment/decrement)
{
//block of statements
increment or decrement
}

 

 

 

while loop: When it is not necessary to do initialization, condition check and increment/decrement in a single statement of an iterative loop, while loop could be used. In while loop statement, only condition statement is present.

Syntax:

While(cond)

{

Block of statements

}

do-while loop: in this kind of looping first time statements in the block gets executed and then checks for the condition, if condition becomes true then it executes the loop or comes out of the loop.

Syntax :

Do{

Block of statements

}while(comdition)

 

What are the characteristics of arrays in C?

1) An array holds elements that have the same data type.

2) Array elements are stored in subsequent memory locations

3) Two-dimensional array elements are stored row by row in subsequent memory locations.

4) Array name represents the address of the starting element

5) Array size should be mentioned in the declaration. Array size must be a constant expression and not a variable.

6)While declaring the 2D array, the number of columns should be specified and its a mandatory. where as for number of rows there is no such rule.

7)An Array index by default starts from 0 and ends with the index no. (n-1).
8)The size of an Array can not be changed at Run Time

 

0 comments :