+1 vote
in Programming Languages by (77.2k points)
How can I find the memory size (in bytes) of a variable in C language?

1 Answer

+3 votes
by (60.0k points)
selected by
 
Best answer

You can use the unary operator 'sizeof' to determine the size of a variable or a data type. The sizeof operator returns a long unsigned integer, so you need to print the result using the '%lu' format specifier. The size of the variable will be in bytes.

Here is an example:

#include <stdio.h>

int main() {

 int nn;

 float ff;

 double dd;

 char cc;

  printf("%lu\t", sizeof(nn));

 printf("%lu\t", sizeof(ff));

 printf("%lu\t", sizeof(dd));

 printf("%lu\n", sizeof(cc));

  

  return 0;

}

The above code will print the following: "4       4       8       1", i.e. size of the integer and float is 4 bytes, the size of the double is 8 bytes, and the size of char is 1 byte.

Related questions

+1 vote
1 answer
+1 vote
1 answer
+2 votes
1 answer

...