Sub topic 1
Data types, sizes, and addresses
Prerequisites
This chapter assumes you know basic C syntax: variables, operators, control flow, and functions. You should be able to read binary and hexadecimal numbers, and to compile and run a small C program with gcc. Nothing about pointers is assumed. This chapter only introduces addresses, and later chapters build pointers on top of them.
The memory model in one paragraph
A running C program sees memory as a long sequence of bytes. Each byte has a number called its address. A variable is a named region of consecutive bytes. Its type decides two things: how many bytes the region has, and how those bytes are interpreted. The address of a variable is the address of its first (lowest) byte.
GATE questions about C rest on this model. Before you reason about pointers, arrays, or recursion, you need to be able to answer "how many bytes is this object, and where does it start?"
Basic types and their sizes
The C standard does not fix the size of most types. It guarantees only minimums and relations between them. The guaranteed facts are these:
sizeof(char)is always 1. Acharis one byte, though a byte need not be 8 bits on exotic hardware.shortis at least 16 bits,intat least 16 bits,longat least 32 bits, andlong longat least 64 bits.- The sizes are ordered:
char≤short≤int≤long≤long long.
In practice, GATE questions state or assume a common platform. The table shows the usual sizes.
| Type | Typical size on a 32-bit system | Typical size on a 64-bit Linux system |
|---|---|---|
| `char` | 1 byte | 1 byte |
| `short` | 2 bytes | 2 bytes |
| `int` | 4 bytes | 4 bytes |
| `long` | 4 bytes | 8 bytes |
| `long long` | 8 bytes | 8 bytes |
| `float` | 4 bytes | 4 bytes |
| `double` | 8 bytes | 8 bytes |
| any pointer | 4 bytes | 8 bytes |
Always read the question for a stated assumption. If none is given, int as 4 bytes is the safe default. Pointer size depends on the machine's address width, not on the type pointed to.
The sizeof operator
sizeof is an operator, not a function. It is evaluated at compile time (except for variable-length arrays) and yields a value of type size_t, an unsigned integer type. Print it with %zu.
#include <stdio.h>
int main(void) {
int x = 5;
double d = 1.5;
char c = 'A';
int arr[10];
printf("%zu %zu %zu\n", sizeof(int), sizeof x, sizeof(double));
printf("%zu\n", sizeof arr);
printf("%zu\n", sizeof arr / sizeof arr[0]);
printf("%zu\n", sizeof(c));
printf("%zu\n", sizeof('A'));
return 0;
}On a typical platform this prints 4 4 8, then 40, then 10, then 1, then 4.
The last line is a classic trap. In C, a character constant such as 'A' has type int, not char, so sizeof('A') is sizeof(int). In C++ it would be 1. The variable c, however, is a char.
Two more points about sizeof:
- The operand is not evaluated.
sizeof(x++)does not changex. - Parentheses are required for a type name (
sizeof(int)) but optional for an expression (sizeof x).
Signed and unsigned integers
An integer type with n bits holds one of 2^n bit patterns. The two interpretations differ in which values they represent.
- Unsigned: values from 0 to 2^n − 1.
- Signed, in two's complement: values from −2^(n−1) to 2^(n−1) − 1.
For a 32-bit int, the range is −2,147,483,648 to 2,147,483,647. For an 8-bit signed char, it is −128 to 127.
Overflow behaves differently for the two kinds:
- Unsigned overflow wraps around modulo 2^n. This is defined behavior.
(unsigned char)255 + 1stored back into anunsigned chargives 0. - Signed overflow is undefined behavior. The compiler may assume it never happens.
Converting between them keeps the bit pattern when the value does not fit. Assigning -1 to an unsigned int gives 4294967295 on a 32-bit int, because the pattern is all ones.
#include <stdio.h>
int main(void) {
unsigned int u = 0;
u = u - 1;
printf("%u\n", u);
signed char sc = -1;
unsigned char uc = (unsigned char)sc;
printf("%d %d\n", sc, uc);
return 0;
}This prints 4294967295 and then -1 255.
A related trap is comparison between signed and unsigned values. In -1 < 1u, the -1 is converted to unsigned first, becomes a huge number, and the comparison is false.
Addresses and the address-of operator
The unary & operator gives the address of an object. You can print it with %p, which expects a void *, so cast the argument.
#include <stdio.h>
int main(void) {
int a = 10;
int b = 20;
char c = 'x';
printf("%p\n", (void *)&a);
printf("%p\n", (void *)&b);
printf("%p\n", (void *)&c);
return 0;
}The exact addresses differ on every machine and often on every run, because of address space layout randomization. What you can rely on is structure, not values:
- Each address is the lowest byte of the object.
- Two distinct live objects never overlap.
- The compiler is free to place local variables in any order.
So you should never write a GATE answer that depends on &a being smaller or larger than &b unless the question gives the addresses.
Memory layout of one object
An object larger than one byte is stored as several consecutive bytes. The order in which they are stored is the endianness of the machine.
- Little-endian: the least significant byte is at the lowest address. This is x86 and most ARM systems.
- Big-endian: the most significant byte is at the lowest address.
Suppose a 4-byte int holds 0x01020304 starting at address 1000. The layout is:
| Address | Little-endian byte | Big-endian byte |
|---|---|---|
| 1000 | 04 | 01 |
| 1001 | 03 | 02 |
| 1002 | 02 | 03 |
| 1003 | 01 | 04 |
Endianness appears in GATE when a question reads an int through a char *. Pointers get full treatment in a later chapter, but this short test shows the idea.
#include <stdio.h>
int main(void) {
unsigned int n = 0x01020304;
unsigned char *b = (unsigned char *)&n;
printf("%02x\n", b[0]);
return 0;
}On a little-endian machine this prints 04. Reading an object's bytes through unsigned char * is always allowed by the standard.
Alignment and padding
Many processors read multi-byte values most efficiently, or only, at addresses that are multiples of the value's size. This requirement is alignment. A compiler therefore places each object at an address that suits its type, and inserts unused padding bytes where needed.
Padding matters most in structures. The compiler keeps each member aligned, and it pads the end of the structure so that arrays of the structure stay aligned.
#include <stdio.h>
struct A {
char c;
int i;
};
struct B {
int i;
char c;
};
struct C {
char c1;
char c2;
int i;
};
int main(void) {
printf("%zu %zu %zu\n", sizeof(struct A), sizeof(struct B), sizeof(struct C));
return 0;
}With 4-byte int aligned to 4, this prints 8 8 8.
struct A:cat offset 0, three padding bytes,iat offset 4. Total 8.struct B:iat offset 0,cat offset 4, three bytes of tail padding so that the next array element'siis aligned. Total 8.struct C:c1at 0,c2at 1, two padding bytes,iat 4. Total 8.
Ordering members from largest to smallest alignment usually removes padding. The standard lets a compiler pick any padding that keeps alignment, so state your assumption when you answer.
Diagram: one variable in memory
Common errors
- Printing
sizeofwith%d. Use%zu. A mismatch is undefined behavior, though it often seems to work. - Assuming
sizeof(int)is 4 orsizeof(long)is 8 without a stated platform. - Forgetting that
sizeof('A')is the size of anintin C. - Treating the order of local variable addresses as fixed.
- Expecting
sizeofof a struct to equal the sum of its members. - Relying on signed overflow to wrap.
Exercise
On a platform where char is 1 byte, short is 2, int is 4, and double is 8 with 8-byte alignment, what does this program print? struct S { char a; double b; short c; }; printf("%zu %zu", sizeof(struct S), sizeof(char) + sizeof(double) + sizeof(short));
Show answer
It prints 24 11. The member a is at offset 0, then 7 padding bytes put b at offset 8. Member c sits at offset 16, and 6 bytes of tail padding round the total up to a multiple of 8, giving 24. The second value is the plain sum 1 + 8 + 2 = 11, which shows that a struct's size is not the sum of its members.
Exercise
A 32-bit unsigned int u holds 0x000000FF. After the statement unsigned char p = (unsigned char )&u; on a little-endian machine, what are p[0] and p[3] in hexadecimal?
Show answer
p[0] is FF and p[3] is 00. On a little-endian machine the least significant byte is stored at the lowest address, so p[0] holds 0xFF, and the most significant byte, 0x00, is at p[3].
Exercise
What does this fragment print, assuming a 4-byte int: int x = 3; printf("%zu %d", sizeof(x++), x);
Show answer
It prints 4 3. The operand of sizeof is not evaluated, so x++ never runs and x stays 3.