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 7129713
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T11:16:19+00:00 2026-05-28T11:16:19+00:00

Possible Duplicate: signed to unsigned conversions A riddle (in C) I am trying to

  • 0

Possible Duplicate:
signed to unsigned conversions
A riddle (in C)

I am trying to understand why this program is not working

 #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;
  }

I came across this program when I was going through automatic type conversions in C.But I don’t understand how conversions happen between signed and unsigned data types.Please explain.
Thank you,
Harish

  • 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-05-28T11:16:19+00:00Added an answer on May 28, 2026 at 11:16 am

    sizeof() is of type unsigned, wile d is signed.

    you check if d is smaller then an unsigned integer. Thus, the signed d is converted to unsinged.

    But the bits representaion of the signed -1 when read as unsigned is greater then 2^31, and obviously greater then TOTAL_ELEMENTS-2, thus the condition is never met and you do not enter the for loop even once.

    Look at this code snap, it might clear up things for you:

    #include <stdio.h>
    #include <stdlib.h>
    int main() { 
       unsigned int x = 50;
       int y = -1;
       printf("x < y is actually %u < %u which yields %u\n", y,x,y < x);
       return 0;
    }
    

    The above code prints:

    x < y is actually 4294967295 < 50 which yields 0

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

Sidebar

Related Questions

Possible Duplicate: performance of unsigned vs signed integers I have read somewhere that it's
Possible Duplicate: signed to unsigned conversion in C - is it always safe? Let's
Possible Duplicate: Problem in calculating checksum : casting int to signed int32 This should
Possible Duplicate: Why not use tables for layout in HTML? Under what conditions should
Possible Duplicate: How do I calculate someone's age in C#? Maybe this could be
Possible Duplicate: php == vs === operator Reference - What does this symbol mean
Possible Duplicate: Learning to write a compiler I looked around trying to find out
Possible Duplicate: Reading through file using ifstream I'm trying to find a way to
Possible Duplicate: How do I convert hex string into signed integer? example:3A convert to
Possible Duplicate: how to use foursquare API in android application? This is a question

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.