3

In c, we are using the sizeof() for getting the size of the datatypes. So how it is defined. It is a macro or a function.

Because we can use that as two ways,

sizeof int

and

sizeof(int)

so how this is defined in header file.

Karthikeyan.R.S
  • 3,991
  • 1
  • 19
  • 31
  • 1
    Neither, it's a language construct. –  Feb 06 '15 at 10:00
  • it's a part of the language... which means neither. – dandan78 Feb 06 '15 at 10:00
  • 6
    `sizeof` is an *operator* and not a macro or function. The first variant, `sizeof int` can be used for types only. The second variant can be used for arbitrary expressions. The `sizeof` operator is also a compile-time only feature, there's no run-time code generated. – Some programmer dude Feb 06 '15 at 10:00

5 Answers5

8

It's neither. It's a built-in operator, whose value is computed at compile-time unless the argument is the name of a variable-length array (added in C99).

The parentheses that you often see are not part of the "call", since sizeof is not a function. They are part of the argument, and are only needed when the argument is a cast expression, i.e. the name of a type enclosed in parentheses.

I personally recommend against using sizeof with a type name as the argument whenever possible, since it's usually not needed, and creates a disconnect/de-coupling which can lead to errors.

Consider something like this:

float *vector = malloc(100 * sizeof(double));

The above, of course, contains a bug: if float is smaller than double, it will waste a lot of memory. It's easy to imagine ending up with something like the above, if vector started out as an array of double but was later changed to float. To protect aginst this, I always write:

float *vector = malloc(10 * sizeof *vector);

The above uses the argument *vector (an expression of type float) to sizeof, which is not a type name so no parentheses are needed. It also "locks" the size of the element to the pointer used to hold it, which is safer.

unwind
  • 391,730
  • 64
  • 469
  • 606
4

Sizeof is neither a macro nor a function.Its a operator which is evaluated at compile time.

Macros evaluated during pr-processing phase.

As pointed out by @Yu Hao Variable length arrays is the only exception.

For More Understanding solve this;

#include<stdio.h>
    char func(char x)
    {
           x++;
           return x;
    }

    int main()
    {
           printf("%zu", sizeof(func(3))); 
                return 0;
    }

    A) 1            B)2             C)3     D)4
Vagish
  • 2,520
  • 19
  • 32
  • Macros are also evaluated at compile time. – m0skit0 Feb 06 '15 at 10:01
  • 2
    Variable length arrays is the (only) exception, `sizeof` VLA is evaluated at run time. – Yu Hao Feb 06 '15 at 10:04
  • 1
    Dont' use `%d` to print a value of type `size_t`. Use `%zu`. – unwind Feb 06 '15 at 10:16
  • @Vagish: I was expecting sizeof functionpointer returned what exactly is happening here? and what would be the way i had to us sizeof onto a function pointer? – dhein Feb 06 '15 at 10:31
  • @Zaibis operating a sizeof on a function gives size of the type which the function is returning,which may be useful in some scenarios. BUT It i snot recommended to operate sizeof on:The sizeof operator may not be applied to: A bit field A function type An undefined structure or class An incomplete type (such as void) – Vagish Feb 06 '15 at 10:48
2

From ISO/IEC9899

6.5.3.4 The sizeof operator

Constraints

1 The sizeof operator shall not be applied to an expression that has function type or an incomplete type, to the parenthesized name of such a type, or to an expression that designates a bit-field member.

So it is neither a macro nor a function.Its a operator!

and the way it is handled is a thing of the compiler.

But regarding to compile time and runtime determination the standard says:

Semantics

2 The sizeof operator yields the size (in bytes) of its operand, which may be an expression or the parenthesized name of a type. The size is determined from the type of the operand. The result is an integer. If the type of the operand is a variable length array type, the operand is evaluated; otherwise, the operand is not evaluated and the result is an integer constant.

So it is even given by standard that it mus be determined on compile time excepting the VLA case.

Community
  • 1
  • 1
dhein
  • 6,431
  • 4
  • 42
  • 74
0

Syntax

  1. sizeof( type )
  2. List sizeof expression

Both versions return a constant of type std::size_t.

Explanation

  1. returns size in bytes of the object representation of type.
  2. returns size in bytes of the object representation of the type, that would be returned by expression, if evaluated.
LPs
  • 16,045
  • 8
  • 30
  • 61
0

The unary operator sizeof is used to calculate the size of any datatype, measured in the number of bytes required to represent the type.

In many programs, there are situations where it is useful to know the size of a particular datatype (one of the most common examples is dynamic memory allocation using the library function malloc). Though for any given implementation of C or C++ the size of a particular datatype is constant, the sizes of even primitive types in C and C++ are implementation-defined (that is, not precisely defined by the standard). This can cause problems when trying to allocate a block of memory of the appropriate size. For example, say a programmer wants to allocate a block of memory big enough to hold ten variables of type int. Because our hypothetical programmer doesn't know the exact size of type int, the programmer doesn't know how many bytes to ask malloc for. Therefore, it is necessary to use sizeof:

Pankaj Bansal
  • 367
  • 4
  • 17