Oct 28, 2008

FAQ's in C

  1. Why there are so many data types in C?

 

Ans: Let us assume that there is only one data type that can hold any kind of data and its size in memory is say, 10 bytes. Now, to store a character, as you might know only 1-byte is enough. This causes 9 bytes of memory to go waste. Similarly if you want to store a number between 1 and 10000, 2-bytes are sufficient, again wasting 8-bytes of memory. Memory is a precious resource and in any case it should not be wasted. This is the reason why there are so many data types, each addressing the specific requirements.

 

  1. What is the difference between float and double?

 

Ans: Both float and double are data types in C/C++. They are used to represent fractional numbers. However, they differ in what is called precision. Precision refers to the number of digits after the decimal point. While float gives a precision of 6-digits, the double data type gives a precision of 15, which is more than double of what a float offers. That is the reason why the double is always referred to as double precision floating point. For normal computations float data type is sufficient. However, in the programs that require greater precision the double data type is used.

 

  1. How do I find out the size of a particular data type?

 

Ans: C and C++ has an operator called sizeof, using which one can find the amount of space occupied by a variable of a particular type in computers’ memory. For example, if you have declared a float variable, say, f in your program. To find the amount of space it occupies in memory, write a statement in your program as follows:

 

          printf(“The size of float=%d”,sizeof(f));

 

  1. What does the % operator do?

 

Ans: The % operator is called modulus operator. It returns the remainder after integer division. For example, consider the following statement:

 

          a=5%2;

 

In this expression, the variable a is going to contain 1, because when 5 is divided by 2, the remainder is 1. You cannot use % operator with float and double data types.

 

  1. Which format specifier should I use to represent a long double?

 

Ans: The format specifier that is used to represent a long double is %Lf.

 

  1. I heard that increment (++) and decrement (--) operators are used differently in different situations. Could you please explain?

 

Ans: The increment (++) operator and decrement (--) operator in C/C++ always increment and decrement respectively, the value of a variable by 1. They can be used in two forms: postfix and prefix.

 

In postfix form, the operators are used after the variable, for example, a++, a--. In the prefix form, the operators are used before the variable, for example, ++a, --a. In both the cases the value of a variable is simply incremented or decremented by 1. However, when these operators are used in conjunction with other operators or in a large expression, their behavior slightly differs. Consider the following cases:

 

Case-1:

 

int a=10,b;

b=++a;

 

Observe the second statement in the above two statements. In this case, first the value of the variable a increments to 11, and this value is assigned to the variable b. Now, the variable a contains 11 and the variable b also contains 11.

 

Case-2:

 

          int a=10,b;

          b=a++;

 

Observe the second statement in the above two statements. In this case, first the value of the variable a, i.e., 10 is assigned to the variable b first and then the value of variable a increments to 11. Now, the variable a contains 11, and the variable b contains 10.

 

The decrement operator also behaves in the same manner, when it is used in a large expression.

 

 

 

**********