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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T15:42:28+00:00 2026-05-13T15:42:28+00:00

Consider the following code: #include <stdio.h> int main(void) { int a[10]; printf(%d,(int)sizeof(a)); //prints 10*sizeof(int)

  • 0

Consider the following code:

  #include <stdio.h>
  int main(void)
  {
    int a[10];
    printf("%d",(int)sizeof(a)); //prints 10*sizeof(int) (40 on my compiler)
    printf("%d",(int)sizeof(a-3)); //prints sizeof(int) (4 on my compiler)

  }

I know that sizeof() is a compile time operator but I was surprised to see the output of second printf(). What could be the reason? Is there an implicit conversion of the argument of sizeof() from an array-type to an integer type?

  • 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-13T15:42:28+00:00Added an answer on May 13, 2026 at 3:42 pm

    The sizeof operator doesn’t evaluate its argument, it only looks at the type of its operand.

    Let’s say you have an array a with type “array [N] of type T”. Then, in most cases, the type of the name a is “pointer to T” (T *), and the value of the pointer is the address of the first element of the array (&a[0]). That is, the name of an array “decays” to a pointer to its first element. The “decaying” doesn’t happen in the following cases:

    • when a is used with the address-of (&) operator,
    • in the initialization of a (it is illegal to assign to arrays in C), and
    • when a is the operand of the sizeof operator.

    So, sizeof a gives you N times sizeof(T).

    When you do sizeof(a-3), the type of the operand to sizeof is determined by the expression a-3. Since a in a-3 is used in a value context (i.e., none of the three contexts above), its type is “pointer to int”, and the name a decays to a pointer to a[0]. As such, calculating a-3 is undefined behavior, but since sizeof doesn’t evaluate its argument, a-3 is used only to determine the type of the operand, so the code is OK (see the first link above for more).

    From the above, sizeof(a-3) is equivalent to sizeof(int *), which is 4 on your computer.

    The “conversion” is due to the subtraction operator. You can see a similar, and perhaps more surprising, result with the comma operator:

    printf("%zu\n", sizeof(1, a));
    

    will also print sizeof(int *), because of the comma operator resulting in a getting used in a value context.

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

Sidebar

Related Questions

Consider the following code which shows compile time error : #include <stdio.h> int main(int
Consider the following code: #include <stdio.h> namespace Foo { template <typename T> void foo(T
Consider the following code. #include <stdio.h> #include <vector> #include <iostream> struct XYZ { int
Consider the following code: class Program { static void Main(string[] args) { A a
Consider the following code: void Handler(object o, EventArgs e) { // I swear o
Consider the following code: abstract class SomeClassX<T> { // blah } class SomeClassY: SomeClassX<int>
Consider the following code snippet private void ProcessFile(string fullPath) { XmlTextReader rdr = new
Consider the following code: template <int dim> struct vec { vec normalize(); }; template
Consider the following code: private static void WriteProcesses(StreamWriter sw, DateTime d) { sw.WriteLine(List of
Consider the following code: $(a).attr(disabled, disabled); In IE and FF, this will make anchors

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.