Introduction
Knowing storage sizes helps you understand how much memory different data types use in your program.
Knowing storage sizes helps you understand how much memory different data types use in your program.
sizeof(type_or_variable)
sizeof returns the size in bytes of a data type or variable.
You can use it with a type name inside parentheses or with a variable name.
int type in bytes.sizeof(int)char type, usually 1 byte.sizeof(char)x which is an int.int x; sizeof(x);This program prints the size in bytes of common C data types using sizeof.
#include <stdio.h> int main() { printf("Size of char: %zu byte(s)\n", sizeof(char)); printf("Size of int: %zu byte(s)\n", sizeof(int)); printf("Size of float: %zu byte(s)\n", sizeof(float)); printf("Size of double: %zu byte(s)\n", sizeof(double)); return 0; }
Sizes can vary depending on your computer and compiler.
sizeof(char) is always 1 byte by definition.
Use %zu in printf to print sizeof results safely.
sizeof tells you how many bytes a type or variable uses.
Knowing sizes helps manage memory and optimize programs.
Always check sizes when working on different systems or with special data types.
char usually takes 1 byte, int 4 bytes, float 4 bytes, and double 8 bytes.char uses the least memory.double in C?double num; follows this rule: double num;. Others have invalid syntax like colon, reversed order, or invalid characters.printf("%zu", sizeof(long));long is 8 bytes (64 bits).sizeof(long)sizeof operator returns the size in bytes, so it returns 8.short variable but causes a compilation error:short s;
printf("%d", sizeof(s));
sizeof operator returns a value of type size_t, which is an unsigned integer type.%d expects an int, but sizeof returns size_t. The correct specifier is %zu.int is 4 bytes, how much memory in bytes will the array consume?int typically uses 4 bytes.Pass the test and this pattern moves unknown