Day#16 'C' Coding Challenge

Day#16 'C' Coding Challenge

What is the output of the below program?

#include <stdio.h> void main() { int var1 = 1; var1<<1; printf("%d",var1); }

--------------------------------------------------------

The above code tests the following scenarios:

1. left shift operator
2. common scenarios seen in embedded programming

--------------------------------------------------------

variable var1 is left shifted once.
If we consider 1 in binary it is 0001, when left shifted by one, it becomes 0010.
After left shift the var1 is not assigned back to var1 and only shift operation is done, so, var1 value doesn't change.
So, the output is 1.

This is a commonly made mistake in embedded programming. While the firmware engineers do write the logic correctly, these kind of small mistakes could give headaches while debugging.

Post a Comment

0 Comments