ASIC Verification: C Basics

Friday, February 29, 2008

C Basics

What are constant and Variables in C?

A constant is an entity that doesn’t change whereas a variable is an entity that may vary during program execution. The results of any calculations are stored in computers memory. To make the retrieval and usage of these results easy, these memory locations are given names. Since the value stored in each location may change, the names given to these locations are called variable names. These locations can contain integer, real or character constants.

What are the types of constants used in C?

Integer constants - Maximum allowable range is b/w -32768 and 32767.
Ex: 426, +782, -8000, -7605

Real constants expressed in Fractional form - It must have a decimal point.
Ex: +325.34, 426.0, -32.76, -48.5792

Real constants expressed in Exponential form - Used, if the constant value is too small or too high. Range of these constants is -3.4e38 to 3.4e38.
Ex: +3.2e-5, 4.1e8, -0.2e+3, -3.2e-5

Character constants - A character constant is a single alphabet, a single digit or a single special symbol enclosed within single inverted commas. Both the inverted commas should point to the left.

What are the C keywords?

Keywords are the words whose meaning has already been explained to the C compiler. The keywords cannot be used as variable names because if we do so, we are trying to assign a new meaning to the keyword, which is not allowed by the computer.

What is the meaning of ampersand (&) operator?

The ampersand (&) before the variables in the scanf( ) function is a must. & is an ‘Address of’ operator. It gives the location number used by the variable in memory. When we say &a, we are telling scanf( ), at which memory location should it store the value supplied by the user from the keyboard.

What are the types of instructions in C?

  • Type Declaration Instruction - To declare the type of variable used in C
  • Arithmetic Instruction - To perform an arithmetic operations
  • Control Instruction - To control the sequence of execution
What are the rules used for converting integer and float values?
  1. An arithmetic operation between an integer and integer always yields an integer result.
  2. An operation between a real and real always yields a real result.
  3. An operation between an integer and real always yields a real result. In this operation the integer is first promoted to a real and then the operation is performed. Hence the result is real.
How a decision control instruction is implemented in C?
  1. The if statement
  2. The if-else statement
  3. The conditional operators
Hierarchy of operators

! - Logical NOT
* / % - Arithmetic and modulus
+ - - Arithmetic
< > <= >= - Relational
== != - Relational
&& - Logical AND
|| - Logical OR
= - Assignment

What is the conditional operators in C?

The conditional operators ? and : are sometimes called ternary operators, since they take three arguments. Their general form is,

expression 1 ? expression 2 : expression 3;

“if expression 1 is true (that is, if its value is non-zero), then the value returned will be expression 2, otherwise the value returned will be expression 3”.

What are the compound assignment operator?

Consider the following program:

main()
{
int i=1;
while (i<5)
{
printf("%d", i);
i+=1;
} // End of while loop
} // End of main()

Here += is an compound assignment operator. It increments the value of i by 1. Other compound operators are -=, *=, /+, %=.

Tell me about the break statement

We often come across situations where we want to jump out of a loop instantly, without waiting to get back to the conditional test. The keyword "break" allows us to do this. When break is encountered inside any loop, control automatically passes to the first statement after the loop. A break is usually associated with an if statement.

Tell me about the continue statement

In some programming situations we want to take the control to the beginning of the loop, bypassing the statements inside the loop, which have not yet been executed. The keyword continue allows us to do this. When continue is encountered inside any loop, control automatically passes to the beginning of the loop.

What is the difference b/w while and do-while loop?

This difference is the place where the condition is tested. The while tests the condition before executing any of the statements within the while loop. As against this, the do-while tests the condition after having executed the statements within the loop.

Tell me about the switch control statement

The control statement that allows us to make a decision from the number of choices is called a switch-case-default, since these three keywords go together to make up the control statement. They most often appear as follows:

switch ( integer expression )
{
case constant 1 : do this ;
case constant 2 : do this ;
case constant 3 : do this ;
default : do this ;
}

The integer expression following the keyword switch is any C expression that will yield an integer value. It could be an integer constant like 1, 2 or 3, or an expression that evaluates to an integer.

Is switch a replacement for if?

Yes and no. Yes, because it offers a better way of writing programs as compared to if, and no because in certain situations we are left with no choice but to use if. The disadvantage of switch is that one cannot have a case in a switch which looks like:

case i <= 20 :

All that we can have after the case is an int constant or a char constant or an expression that evaluates to one of these constants. Even a float is not allowed.

What is the advantage of switch over if?

Switch leads to a more structured program and the level of indentation is manageable, more so if there are multiple statements within each case of a switch.

What are the things that you can't do with switch?
  1. A float expression cannot be tested using a switch
  2. Cases can never have variable expressions (for example it is wrong to say case a +3 : )
  3. Multiple cases cannot use same expression. Thus the following switch is illegal:
switch ( a )
{
case 3 :
...
case 1 + 2 : // Illegal
...
}

(a), (b) and (c) above may lead you to believe that these are obvious disadvantages with a switch, especially since there weren’t any such limitations with if-else.


No comments: