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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T13:41:46+00:00 2026-06-10T13:41:46+00:00

#include <stdio.h> int main(void) { int TEST_HB[10]; memset(TEST_HB,’9′, sizeof( TEST_HB)); printf(%c\n,TEST_HB[9]); printf (TEST_HB[10]=%d\n,sizeof( TEST_HB[40]));

  • 0
#include <stdio.h>

int main(void) {

int TEST_HB[10]; 
memset(TEST_HB,'9', sizeof( TEST_HB));

printf("%c\n",TEST_HB[9]);

printf ("TEST_HB[10]=%d\n",sizeof( TEST_HB[40]));   // shows 4
printf ("Arraysize=%d\n",(sizeof(int)*10));     // gets the expectected results
return 0;

} 

I believe that sizeof (myArray) should return the total size of the array in bytes. But why does sizeof( TEST_HB[40]) returns 4 when it is not defined ?

  • 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-10T13:41:48+00:00Added an answer on June 10, 2026 at 1:41 pm

    TEST_HB[40] is an expression with type int (and undefined behavior if evaluated, since 40 is too big for the array). So sizeof(TEST_HB[40]) is the size of an int on your implementation: 4 is typical.

    Importantly, sizeof does not evaluate its operand unless it’s a VLA — it just uses the type. Therefore your code has defined behavior even though there’s no such object as TEST_HB[40].

    Actually, I say it has defined behavior, but sizeof evaluates to type size_t, which isn’t printed with %d. Use %zu where available or consult your compiler documentation. You’ve got away with it here because you’ve got lucky with the varargs calling convention, plus either size_t is the same size as int in your implementation, or your implementation is little-endian, or both.

    A fairly common use of the fact that sizeof does not evaluate the operand is to write something like this:

    struct Foo *foo = malloc(sizeof(*foo) * number_of_foos_required);
    

    sizeof(*foo) is the same as sizeof(struct Foo), but to some people it’s more “obviously” the right size to use. foo is uninitialized before the memory is allocated, so it’s just as well that sizeof doesn’t actually use the value of foo.

    A less common use, that demonstrates the behavior of sizeof:

    int i = 0;
    printf("%d\n", i);
    printf("%d\n", (int)(sizeof(i++))); // i++ is not executed
    printf("%d\n", i);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

#include<stdio.h> #include<conio.h> int main (void) { int a,b,c,d; clrscr(); a=3; b=5; c=a,b; d=(a,b); printf(c=%d,c);
#include<stdio.h> void main(){ int x,y,z; x=y=z=1; z=++x||++y&&++z; printf(%d %d %d \n,x,y,z); getch(); } the
Can someone explain this behaviour? test.c: #include <stdio.h> int main(void) { printf(%d, %d\n, (int)
see this code #include<stdio.h> int main() { void test(void) { printf(test); return; } printf(main);
#include stdio.h #define COUNT(a) (sizeof(a) / sizeof(*(a))) void test(int b[]) { printf(2, count:%d\n, COUNT(b));
Linux3.2.0 Given the following source: #include <stdio.h> #include <sched.h> int main(void) { printf(%i \n,
#include <stdio.h> #include <stdlib.h> int main(void) { int x; int *in, *begin; in =
consider the code #include<stdio.h> int main(void) { char* a; scanf(%s,a);//&a and &a[0] give same
Consider the following program: #include <stdio.h> int main(void) { return 0; } When i
Here is my code: #include <stdio.h> int main(void) { FILE *fp; unsigned int i;

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.