You are here:

C/How many bytes

Advertisement


Question
Hello.
I'm trying to check how many bytes are occupied by function scanf("%d",&i) (where i is int).

I tried to write this:

printf("%d\n",sizeof(scanf("%d",&i)));

and program writes number 2.
I don't know, if this result is correct (only two bytes for this function?) or incorrect. Was my method correct?

Answer
Hello Rudolph.

It is not possible to find how many bytes a function occupies in this way. What sizeof is returning here is the size of the return value of the function. I don't know why it is 2 in your case. Perhaps you are using a very old system. On my system it returns 4.

Try this:
int i(void) {}
short s(void) {}

int main(void)
{
  printf("%d\n", sizeof(i()));
  printf("%d\n", sizeof(s()));
}

I get results of 4 and 2, matching the size of the return values of the functions.

Best Regards
Zlatko

C

All Answers


Answers by Expert:


Ask Experts

Volunteer


Zlatko

Expertise

No longer taking questions.

Experience

No longer taking questions.

Education/Credentials
No longer taking questions.

©2012 About.com, a part of The New York Times Company. All rights reserved.