FILE *fp;What is size of FILE data type???

Questions by vignesh007   answers by vignesh007

Showing Answers 1 - 14 of 14 Answers

Pradeep Patil

  • Aug 31st, 2006
 

*fp will be size of 2 Bytes because every pointer is of 2 bytes

  Was this answer useful?  Yes

someone

  • Aug 31st, 2006
 

FILE*fp

sizeof(fp) will give 4 since fp is a pointer

FILE is a structure.....

  Was this answer useful?  Yes

Preetham

  • Sep 25th, 2006
 

Actually the size of fp (i.e the file pointer) is 2, where as size of *fp is 16 bytes.

  Was this answer useful?  Yes

Guna S

  • Jul 12th, 2007
 

FILE *fp;
printf("%d...%d...%d",sizeof(FILE),sizeof(fp),sizeof(*fp));


Output: (In Turbo C)
16...2...16

  Was this answer useful?  Yes

ardashev

  • Nov 17th, 2007
 

do 16 bit machines still exist ???

on 64-bit machine pointer is 8 bytes !!!

    FILE* p = NULL;
    printf("size of pointer =  %d n",sizeof(p));

output:
size of pointer =  8

so, the correct answer is :

ANY pointer has size depending on your machine's architecture.

kbjarnason

  • Jul 1st, 2010
 

sizeof(FILE), of course.

How large that actually is depends entirely on how the implementation defines the FILE structure, which won't be the same from implementation to implementation.

  Was this answer useful?  Yes

If you see the declaration of FILE in stdio.h:

struct _iobuf {

char *_ptr;

int _cnt;

char *_base;

int _flag;

int _file;

int _charbuf;

int _bufsiz;

char *_tmpfname;

};

As there is no packing, the default will be taken as 4 bytes. 8 element with highest of 4 bytes in size will make 4*8 = 32 bytes of the structure.

Regards,
Kapil

  Was this answer useful?  Yes

kbjarnason

  • Jul 7th, 2011
 

"If you look in stdio.h, you see..."  - which stdio.h?  The one from visual studio 95?  gcc for Linux?  Some other one?  Those header files are not identical from implementation to implementation.

For the record: sizeof(FILE) is the only meaningful answer to "what is the size of the FILE data type", and that number is not guaranteed to be the same from one implementation to the next.

Someone said sizeof (fp) is 2, because fp is a pointer, but nothing in C requires that a pointer be any specific size, nor that a byte be any specific size, nor are pointers restricted to simply being addresses.

There are two further complications. 

First, C rarely (if, indeed, ever) defines the size of any types.  Instead it defines _minimum ranges_.  A signed char must be able to store a range of _at least_ -127 to 127, but as far as C is concerned there is nothing to stop it storing much larger ranges - meaning that a char may be anywhere from 8 bits wide on up.

Second, where size _is_ relevant in C - such as when evaluating sizeof(FILE) - the result is defined in bytes, but as C uses the term, meaning size-equivalent to "char".   So if sizeof(x) evaluates to 2 - x is 2 bytes in size - this does *not* mean x is 16 bits wide, but 2 chars wide, which could easily mean it is 512 bits wide.  Or some other value.

Do *not* get your information on C from reading header files or printing the results of things such as sizeof(x) or INT_MAX; these tell you only the details of the particular compiler/version/mode/etc you personally are using at the moment, and may have absolutely nothing to do with the C language itself,  nor is there any guarantee the results will be the same even if run on a different version of the same compiler, never mind different compilers, different operating systems, 16 vs 32 vs 64 bit modes and so forth.

What's the size of the FILE data type?  There isn't one size.  The size depends upon the implementation.  If you have to evaluate the size, the only meaningful answer is sizeof(FILE) chars.


  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions