Oct 28, 2008

FAQ'S in C- II


  1. How much space does a variable of type int occupy in memory?


The size of int data type is not fixed. It depends upon the operating system you are using. On 16-bit operating systems like DOS, its size is 2-bytes (16-bits) and on 32 bit operating systems such as Windows, and Linux, it is 4-bytes (32-bits).


  1. What do you mean by typecasting?


Typecasting is a mechanism used to convert a value from one data type into another.


This is how you can typecast an expression. Let us assume that the variables i and j as integers. By writing the following expression


f=(float) i/j;


you are forcibly converting the numerator (i) into a float.



  1. Why should one declare variables?


There are several reasons for declaring variables:


·         When all the variables are in one place it becomes easy for a reader to quickly understand what the program is about.


·         As a programmer, having to declare the variables forces you to do some planning before writing the program. You have to think about what information the program needs; what exactly you want the program to produce as output; the best way to represent the data; and so on.


·         Declaring variables also helps to prevent a common but hard-to-find bug — a misspelled variable name. If you make a mistake in the spelling of a variable name, and enter LENGTHl instead of LENGTH1, the compiler complains that an undeclared variable has turned up.


·         Your C program will not be compiled if you don’t declare variables.


  1. Why is an & needed before a variable name in the scanf() function?


When you place an '&' before a variable, you inform the compiler of the memory address reserved for the variable in the computer’s main memory. This is similar to an address that tells the postman where to deliver a letter. Using this memory address, the scanf() function will store the value entered by the user from the keyboard in that location.


5.     What is initialization?


Initialization is a process by which a variable is assigned an initial value after it is declared. Initialization is required in the program when you know in advance the value a variable is going to contain. Also initialization is useful in programs that manipulate the initial value contained in a variable and results in a different value later. For example, if you are writing a program to find the sum of the first 5 natural numbers, you initialize a variable, say, sum to 1 and add to it the remaining numbers resulting in the final result of 15.


  1. What do you mean by operator precedence?


When you are evaluating an arithmetic expression that has two or more operators, you may not be clear about how it gets executed. For example, does the expression a*b/c correspond to (a*b)/c or a*(b/c)? A programmer is confused regarding this. So, to resolve this confusion, you need to understand the ‘hierarchy’ of operations, which only means the priority or precedence in which the operations in an expression are performed.


The table given here shows the hierarchy of commonly used arithmetic operators.









*     /      %

Multiplication, Division, and Modulo division





+    -

Addition, and Subtraction









  1. What is operator association?


Take a look at the expression:


X=3 * 16 % 11 / 4;


In this expression, you will find that the operators * and % share the operand 16 and similarly % and / share the operand 11. In such cases, C uses left-to-right association rule, which means that the operator to the left of the operand is evaluated first and then the one on the right.


If you consider the assignment operator, its association is right-to-left, which means that the value on the right of the operator is finally assigned to the variable on the left and not the other way round. The table shows you the association of all the arithmetic operators.




*     /      %

Left to right

+    -

Left to right


Right to left.



  1. What does logical NOT operator do?


Logical NOT (!) operator is a unary operator. This means it will operate on only one operand. This operator is used to negate a condition or a value. What this means is that using this operator you can make a condition FALSE, if it is TRUE and vice versa. Consider the following example.





   int x=10,y=5;






          printf(“Good bye”);



In this program, the condition (x>y) is actually true. However, since there is a ! operator preceding the condition, the condition becomes false and the control is transferred to else  statement, which displays Good bye.