Search This Blog

Friday, February 4, 2011

Embedded Engineer: integer promotion rule - Programming in C and C++

One thing Embedded engineer should know is integer promotion rule. Declaring the variable in correct makes life lot more easy in debugging and it should a good practice of programming as well.
Below example is for integer promotion rule.

unsigned int uIntVar = 2;
  int intVar = -10;
    (uIntVar+intVar > 2) ? puts("> 2") : puts("<= 2");

Answer is >2. Expression having the signed and unsigned integers, signed integer becomes a large number and evaluates to positive number in above example.
However it becomes a negative number if
int result = intVar  + uIntVar;  Or type cast the result to int

Now let me post questions.
1) What is the return value if invoke malloc with size zero?
2) How do you write a 1's compliment for given number?

Answer for the first question is strange where second one tests whether you are a embedded engineer.

No comments:

Post a Comment