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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T06:58:03+00:00 2026-05-18T06:58:03+00:00

size_t size = sizeof(int); printf(%d\n, size); int i; for (i = 0; i <

  • 0
 size_t size = sizeof(int);
 printf("%d\n", size);

 int i;
 for (i = 0; i < size; i++) {
    printf("%d ", i);
 }

The above code (using gcc) outptus

4

0 1 2 3

 size_t size = sizeof(int);
 printf("%d\n", size);

 int i;
 for (i = -1; i < size; i++) {
    printf("%d ", i);
 }

This code (i is initialized to -1) outputs only 4 and nothing in the loop.

 size_t size = sizeof(int);
 printf("%d\n", size);

 int i;
 for (i = -1; i < (int) size; i++) {
    printf("%d ", i);
 }

Adding a cast makes the code run fine again. The output is

4

-1 0 1 2 3

What’s going wrong in the second code? Why doesn’t printf go wrong anywhere?

  • 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-18T06:58:03+00:00Added an answer on May 18, 2026 at 6:58 am
    i < size
    

    When i is signed and size is unsigned, then i is converted to unsigned before the comparison is performed. This is part of what are called the usual arithmetic conversions.

    When -1 is converted to an unsigned type, the result is the largest possible value representable by the unsigned type, thus i < size is false when i is -1 for any value of size.

    When you use i < (int)size instead, both operands of < are of type int, so no conversions need to be performed and since both operands are signed, you get the expected result.

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

Sidebar

Related Questions

if I'm using the sizeof operator and making use of size_t in my code,
I am working on network programming using epoll and I have this code... int
const size_t size = 5; int *i = new int[size](); for (int* k =
I have some C++ code that prints a size_t : size_t a; printf(%lu, a);
Is the following code 100% portable? int a=10; size_t size_of_int = (char *)(&a+1)-(char*)(&a); //
I have the following code: #include <stdlib.h> #include <stdio.h> #define SIZE 100 int* arr;
The given C code #include <stdio.h> int x = 14; size_t check() { struct
This is the main code of my server program in C: int main(int argc,
i have a function defined as follows: void AddHeadCode(std::ofstream &ostream, size_t length){ ostream.write((char*)length, sizeof(length));
I'm having trouble understanding why this doesn't sort the vector by the time/size of

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.