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

The Archive Base Latest Questions

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

The expected output of the following C program is to print the array elements.

  • 0

The expected output of the following C program is to print the array elements. 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;
}

What is the reason ?

  • 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-16T02:28:55+00:00Added an answer on May 16, 2026 at 2:28 am

    When you do the comparison d <= (TOTAL_ELEMENTS-2), a type conversion is performed. d is of type signed int while (TOTAL_ELEMENTS-2) is of type size_t, which is an unsigned type. The rules of C say that when an operator has a signed and an unsigned argument, and the unsigned argument is of greater or equal size to the signed argument, then the signed argument is converted to unsigned.

    That is, the comparison ends up as:

    (size_t) d <= (TOTAL_ELEMENTS-2)
    

    And because size_t is unsigned, (size_t) -1 is a really, really large number, not -1 any more. For a 32-bit size_t it would be 232 – 1 = 4,294,967,295.

    To fix this, you can explicitly cast the right-hand side to signed int:

    d <= (int) (TOTAL_ELEMENTS-2)
    

    Or, better, just get rid of the weird negative indexing and such.

    For future reference, turn on all the compiler warnings you can. gcc, for instance, will print a warning if you turn on -Wall -Wextra:

    $ gcc -o arrayprint -Wall -Wextra -ansi arrayprint.c 
    arrayprint.c: In function ‘main’:
    arrayprint.c:11: warning: comparison between signed and unsigned
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

String to split: java[d]program Expected output: java and program but got output: java[ and
Following is the program that raised the mentioned doubt for me. #include <stdio.h> int
This code when executed displays the expected output but prints segmentation fault (core dumped)
I expected this to print [b] but it prints [] : $x = abc;
The output from the following program is: Non-Static Static Non-Static Is this a compiler
Constructor and method not working as expected in Java program I have the following
The following program compiles with ifort (version 12) but not with GFortran (up to
I tried the following program on Visual Studio 2010. #include <iostream> using namespace std;
What will be the output of the following program? int *call(); void main() {
My following code does not produce expected output: public static void main(String[] args) throws

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.