Welcome, %1$s. Please login or register.

November 22, 2017, 08:18:33 PM
: 1
: Guess the output : Macros  ( 5193 )
« : September 24, 2007, 11:08:44 AM From Shrinidhi»



Guess the output : Macros


#include <stdio.h>

  #define TOTAL_ELEMENTS (sizeof(array) / sizeof(array[0]))
  int array[] = {23,34,12,17,204,99,16};

  int main()
  {
      int d;

      for(d = -1;d <= (TOTAL_ELEMENTS - 2); d++)
          printf("%d\n", array[d+1]);

      return 0;
  }


Can anyone tell what could be the output of the program?

 
Liked It? Share it!

              


« #1 : September 24, 2007, 10:37:06 PM From spazinvader»

Is it
23
34
12
?
« #2 : September 28, 2007, 11:08:13 AM From vishwakarma.deepak»

U'll be able to compile program but in GNU compiler it will give segmentation fault as the first array index accessed in loop in -1.

any other issues...???

Deepak
« #3 : September 28, 2007, 11:13:44 AM From Shrinidhi»

I haven't tried it in GNU compiler. Try running the program in VC++ compiler.
Nothing gets printed. Can anyone figure out why?
« #4 : October 01, 2007, 09:48:50 PM From spazinvader»

I guessed that it will print array[0],array[1],array[2] as per the logic.
If it is not printing anything then i guess it should have taken the integer size as 4.
Even at that case,i think it should print the first element.
Any body,have the answer for this?
« #5 : October 02, 2007, 02:02:19 AM From Sudeep»

I typed the code in TC and it didnt give any compiler errors.
And it printed all the array elements...
its absolutely.. working fine fro me
« #6 : October 06, 2007, 10:22:16 AM From vishwakarma.deepak»

 for(d = -1;d <= (TOTAL_ELEMENTS - 2); d++)

the whole problem is with the for loop initialization statement. There is no segmentation fault, program gets compiled, executes and exits normally.

We expect for loop to print array elements and as it not doin any so we suspect macro for some bug...But there is no problem with macro...debugging with gdb I saw we were missing a simple concept of type cast.
all the macro replacements are treated as unsigned values...n we have declared d as  signed int... so as d (a signed value) is compared with the macro replaced value ( an unsigned value), d is converted in unsigend representation.

Here d is  negative hence the unsigned representation comes to be very huge and that's why control is not entering the for loop and hence not printing anything...


Knowledge grows when we learn to share...pls correct me if there is any issue with above explanation...


Deepak

PPL say C is a SEA and this poor guy does not know swimming!!!
« #7 : October 06, 2007, 09:59:28 PM From spazinvader»

Really nice explanation Deepak.
But i got some doubt in your explanation.
Why is d converted into unsigned representation?why not the macro element is changed into signed?

Is it depends on the type of compiler we are using?
« #8 : October 08, 2007, 10:57:05 AM From vishwakarma.deepak»

implicit operad conversion depends on the conversion rank of the operands...
Even if two operands are of same type then also they may have different conversion ranks. An operand with lesser conversion rank is converted into one with higher conversion rank.

sizeof( ) Is an Unsigned long. obviously its conversion rank is higher than that of an integer operand. That's y d is implicitly casted into unsigned long value.

Hope this clears your doubt. You can visit links to know more about conversion ranks and implicit conversion rules.

https://www.securecoding.cert.org/confluence/display/seccode/INT02-A.+Understand+integer+conversion+rules
http://docs.sun.com/app/docs/doc/819-5265/6n7c29cnb?a=view


DEEPAK
« #9 : January 10, 2008, 01:49:12 PM From thequark»

Bingo,
even I missed the point at first glance. Had to run gdb to check it out!

Section 5.4 in K&R says sizeof evaluates to size_t which is unsigned integer.
« #10 : January 30, 2011, 04:21:32 PM From badboy711»

 ;D ;D
: 1
« previous next »

 

Best RatedList All>>



Latest
Random



SMF 2.0.10 | SMF © 2015, Simple Machines | Contact Webmaster | OnlineFunDb.com © 2009/10 | Legal Disclaimer