Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7710211
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T01:04:02+00:00 2026-06-01T01:04:02+00:00

Possible Duplicate: Confused about C macro expansion and integer arithmetic A riddle (in C)

  • 0

Possible Duplicate:
Confused about C macro expansion and integer arithmetic
A riddle (in C)

The expected output of the following C program is to print the elements in the array. But when actually run, it doesn’t do so.

#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;
  }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T01:04:03+00:00Added an answer on June 1, 2026 at 1:04 am

    Because sizeof gives you an unsigned value, which you probably would have noticed had you turned up the warning level, such as using -Wall -Wextra with gcc (a):

    xyzzy.c: In function 'main':
    xyzzy.c:8: warning: comparison between signed and unsigned
    

    If you force it to signed, it works fine:

    #define TOTAL_ELEMENTS (int)((sizeof(array) / sizeof(array[0])))
    

    What happens in detail can be gleaned from the ISO standard. In comparisons between different types, promotions are performed to make the types compatible. The compatible type chosen depends on several factors such as sign compatibility, precision and rank but, in this case, it was deemed that the unsigned type size_t was the compatible type so d was upgraded to that type.

    Unfortunately, casting -1 to an unsigned type (at least for two’s complement which is almost certainly what you’re using) results in a rather large positive number.

    One that’s certainly larger the the 5 you get from (TOTAL_ELEMENTS-2). In other words, your for statement effectively becomes:

    for (d = some big honking number way greater than five;
         d <= 5;
         d++
    ) {
        // fat chance of getting in here !!
    }
    

    (a) This requirement to use extra remains a point of contention between the gcc developers and myself. They’re obviously using some new definition of the word “all” of which I was previously unaware (with apologies to Douglas Adams).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: PHP htmlentities() not working as expected I'm confused as to why it
Possible Duplicate: “Least Astonishment” in Python: The Mutable Default Argument I'm very confused about
Possible Duplicate: Property Declaration and Automatic Backing Storage Allocation I'm really confused about this
Possible Duplicate: What do the brackets mean in x86 asm? I've been confused about
Possible Duplicate: Static vs global I'm confused about the differences between global and static
Possible Duplicate: Why is my return type meaningless? Hi, I'm confused about a particular
Possible Duplicate: Singleton: How should it be used Following on from Ewan Makepeace 's
Possible Duplicate: Malloc thread-safe? I am not a bit confused while I am reading
Possible Duplicate: Weird Java Boxing Recently I saw a presentation where was the following
Possible Duplicate: When is a language considered a scripting language? I am really confused

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.