These are the notes for C episode 29. In an interview you get a short printed C program and one question: what does this print? These questions are chosen because the obvious answer is wrong. So for every question we predict, run, watch the obvious answer fail, find the reason, and fix it. Four rounds (sizes, types, pointers and strings, macros), a coding round (reverse a string in place, check a power of two), quick-fire theory, and how to answer like an engineer.
Every example was compiled with gcc -O2 -Wno-unused-result main.c -o main -lm (gcc 13.3, glibc 2.39, x86-64: int is 4 bytes, pointers are 8 bytes, the same command as the SyntaxPilot compiler) and run exactly as shown. Sizes can differ on other machines (a 32-bit system has 4-byte pointers); the reasons never change.
The problem: the interview room
Real-life example: interview day. The interviewer slides a printed snippet across the table. Ten lines. "What does it print?" It looks easy, so you answer in two seconds. "Wrong. Why?" Silence.
Knowing the output is not enough: the interviewer wants the reason, because the reason tells you the answer to every variant of the question. So today, for each question:
- Predict (say it out loud).
- Run it for real.
- When the obvious answer fails, find why in one line.
- Give the fix.
Round 1: sizes
Question 1: how big is this struct? (padding and alignment)
#include <stdio.h>
struct Ticket {
char coach;
int seat;
char berth;
};
int main() {
printf("char: %zu, int: %zu\n", sizeof(char), sizeof(int));
printf("struct Ticket: %zu bytes\n", sizeof(struct Ticket));
return 0;
}
The obvious answer: 1 + 4 + 1 = 6. Run it:
char: 1, int: 4
struct Ticket: 12 bytes
Why: alignment and padding. Real-life example: a steel lunch box with fixed compartments. The big roti slot always starts at the edge, not just anywhere, and small items leave gaps.
- Alignment: the CPU reads an
intfastest when it starts at an address that is a multiple of 4 (its alignment). The compiler makes sure it does. - Padding: unused bytes the compiler inserts to keep members aligned.
The layout of struct Ticket:
| Offset | Bytes | What |
|---|---|---|
| 0 | 1 | coach |
| 1-3 | 3 | padding, so seat starts at 4 |
| 4-7 | 4 | seat |
| 8 | 1 | berth |
| 9-11 | 3 | padding at the end, so the next Ticket in an array is aligned too |
Total: 12. The struct's size is always a multiple of its biggest member's alignment.
The fix: order members from big to small.
#include <stdio.h>
struct Ticket {
int seat;
char coach;
char berth;
};
int main() {
printf("char: %zu, int: %zu\n", sizeof(char), sizeof(int));
printf("struct Ticket: %zu bytes\n", sizeof(struct Ticket));
return 0;
}
char: 1, int: 4
struct Ticket: 8 bytes
4 for seat, 1, 1, and 2 bytes of padding at the end. Same data, a third smaller: in an array of a million tickets that is 4 MB saved.
Interview answer: "12, not 6, because of alignment: the compiler adds 3 padding bytes after coach so seat starts at a multiple of 4, and 3 at the end so arrays stay aligned. Reordering the members big to small makes it 8."
Question 2: sizeof an array inside a function (array decay)
#include <stdio.h>
void count(int marks[]) {
printf("Inside count: %zu bytes\n", sizeof(marks));
}
int main() {
int marks[10];
printf("In main: %zu bytes\n", sizeof(marks));
count(marks);
return 0;
}
The obvious answer: 40 and 40. gcc already warns:
main.c: In function 'count':
main.c:4:47: warning: 'sizeof' on array function parameter 'marks' will return size of 'int *' [-Wsizeof-array-argument]
4 | printf("Inside count: %zu bytes\n", sizeof(marks));
| ^
main.c:3:16: note: declared here
3 | void count(int marks[]) {
| ~~~~^~~~~~~
In main: 40 bytes
Inside count: 8 bytes
Why: array decay. Real-life example: you tell a friend "the train is at platform 4, coach S1". You hand over the location, not the train. C never passes a whole array to a function: the array decays into a pointer to its first element. int marks[] in a parameter list means exactly int *marks, and a pointer here is 8 bytes.
The fix: count the elements where the real array is, and pass the length.
#include <stdio.h>
void count(int marks[], int n) {
printf("Inside count: %d marks\n", n);
}
int main() {
int marks[10];
int n = sizeof(marks) / sizeof(marks[0]);
count(marks, n);
return 0;
}
Inside count: 10 marks
sizeof(marks) / sizeof(marks[0]) = total bytes / bytes per element = 40 / 4 = 10. This only works on a real array, never on a pointer. That's why fgets, qsort, memcpy and friends always take the size as a separate argument.
Round 2: types
Question 3: is -1 smaller than sizeof(int)? (signed vs unsigned)
#include <stdio.h>
int main() {
int x = -1;
if (x < sizeof(int))
printf("-1 is smaller\n");
else
printf("-1 is NOT smaller?!\n");
return 0;
}
The obvious answer: "-1 is smaller" (−1 < 4). Run it:
-1 is NOT smaller?!
No error, and no warning with the default options.
Why. sizeof gives a value of type size_t, which is unsigned: it has no minus sign. Real-life example: a lift panel with no basement buttons: it simply can't show −1. When you compare an int with an unsigned value of the same or bigger size, C converts the int to unsigned (the usual arithmetic conversions). −1 becomes the biggest possible size_t, 18446744073709551615 (about 18 quintillion), and that is not smaller than 4.
The fix: make both sides signed.
if (x < (int)sizeof(int))
-1 is smaller
-Wall -Wextra would have caught it:
w.c:5:11: warning: comparison of integer expressions of different signedness: 'int' and 'long unsigned int' [-Wsign-compare]
Question 4: is 0.1 equal to 0.1? (floating point)
#include <stdio.h>
int main() {
float price = 0.1;
if (price == 0.1)
printf("equal\n");
else
printf("not equal\n");
printf("%.20f\n", price);
printf("%.20f\n", 0.1);
return 0;
}
not equal
0.10000000149011611938
0.10000000000000000555
Why. Real-life example: 1 ÷ 3 on a calculator shows 0.3333333 and stops somewhere: never exact. A computer stores numbers in binary, and in binary 0.1 never ends either, so it is stored as the nearest value that fits. A float (32 bits) keeps fewer digits than a double (64 bits), so the float 0.1 and the double 0.1 are two different nearby numbers. In price == 0.1, price is converted to double and compared with the double 0.1: not equal.
The fix: never compare floating point numbers with ==. Check that the difference is tiny:
#include <stdio.h>
#include <math.h>
int main() {
float price = 0.1;
if (fabs(price - 0.1) < 1e-6)
printf("equal (close enough)\n");
else
printf("not equal\n");
return 0;
}
equal (close enough)
fabs (from math.h) is the absolute value of a double; 1e-6 is one millionth, a tolerance (often called epsilon). For money, many programs avoid floating point completely and store paise in an integer.
Question 5: 50,000 × 50,000 (integer overflow)
#include <stdio.h>
int main() {
int people = 50000;
int pairs = people * people;
printf("pairs = %d\n", pairs);
return 0;
}
pairs = -1794967296
Why. Real-life example: an old car odometer: drive past 99,999 and it shows 00000 again. A 32-bit int holds at most 2,147,483,647 (about 2.1 billion). 2,500,000,000 doesn't fit, so it wraps into the negative numbers. For signed integers this is called signed overflow, and the C standard says it is undefined behaviour: the compiler is allowed to assume it never happens, so you can't even rely on the wrap.
The fix: a bigger type, and cast before multiplying, because int * int is computed in int even if you store the result in a long long.
#include <stdio.h>
int main() {
int people = 50000;
long long pairs = (long long)people * people;
printf("pairs = %lld\n", pairs);
return 0;
}
pairs = 2500000000
long long is at least 64 bits (up to about 9.2 quintillion) and is printed with %lld.
Round 3: pointers and strings
Four real-life pictures for this round:
| Real life | In C |
|---|---|
| A printed menu card: you can read it, not rewrite it | char *s = "delhi"; points to a string literal in read-only memory |
| Your own notebook copy of the menu | char s[] = "delhi"; copies the letters into your own array |
| Two house addresses: different houses, same furniture | == on two strings compares addresses, not letters |
| A hotel room after checkout: you hold the key card, but the room isn't yours | a local array is gone when its function returns |
Question 6: change the first letter (string literals are read-only)
#include <stdio.h>
int main() {
char *city = "delhi";
city[0] = 'D';
printf("%s\n", city);
return 0;
}
The obvious answer: Delhi. Run it:
Segmentation fault
Why. "delhi" is a string literal: the compiler puts its letters in read-only memory, and city just points there. Writing to it is undefined behaviour; on Linux the operating system protects that memory and kills the program.
The fix: an array, so the letters are copied into memory you own.
char city[] = "delhi";
Delhi
Good habit: write const char *city = "delhi"; whenever you point at a literal; then city[0] = 'D' becomes a compile error instead of a crash.
Question 7: the login check (== on strings)
#include <stdio.h>
int main() {
char typed[20];
scanf("%19s", typed);
if (typed == "admin")
printf("Welcome!\n");
else
printf("Wrong password\n");
return 0;
}
Input (the stdin box):
admin
Wrong password
Why. typed decays to the address of our array; "admin" is the address of the literal. == compares the two addresses: two different houses, never equal, no matter what is inside. -Wall warns: warning: comparison with string literal results in unspecified behavior [-Waddress].
The fix: strcmp from string.h compares letter by letter and returns 0 when equal (negative if the first string comes first in dictionary order, positive otherwise).
#include <stdio.h>
#include <string.h>
int main() {
char typed[20];
scanf("%19s", typed);
if (strcmp(typed, "admin") == 0)
printf("Welcome!\n");
else
printf("Wrong password\n");
return 0;
}
Welcome!
Question 8: returning a local array (dangling pointer)
#include <stdio.h>
char *make_otp() {
char otp[7] = "482913";
return otp;
}
int main() {
char *code = make_otp();
printf("Your OTP: %s\n", code);
return 0;
}
main.c: In function 'make_otp':
main.c:5:12: warning: function returns address of local variable [-Wreturn-local-addr]
5 | return otp;
| ^~~
main.c: In function 'main':
main.c:10:23: warning: '%s' directive argument is null [-Wformat-overflow=]
10 | printf("Your OTP: %s\n", code);
| ^~
Your OTP: (null)
Why. otp is a local variable: it lives in make_otp's stack frame, which is released when the function returns (the hotel room after checkout). A pointer to memory that is gone is a dangling pointer. Using it is undefined behaviour; here gcc spotted it and returned a null pointer instead, so glibc's printf printed (null). On another compiler you might see garbage, or the right digits by luck.
Fixes (any one):
static: a static local lives for the whole program (episode 17).
char *make_otp() {
static char otp[7] = "482913";
return otp;
}
Your OTP: 482913
mallocthe array and let the callerfreeit.- Let the caller pass its own buffer:
void make_otp(char *out, size_t size).
Question 9: const int p vs int const q
#include <stdio.h>
int main() {
int limit = 100, other = 200;
const int *p = &limit;
*p = 500;
int *const q = &limit;
q = &other;
return 0;
}
main.c: In function 'main':
main.c:6:8: error: assignment of read-only location '*p'
6 | *p = 500;
| ^
main.c:8:7: error: assignment of read-only variable 'q'
8 | q = &other;
| ^
Read declarations right to left:
| Declaration | Read it as | Can change the value *x? |
Can point elsewhere? |
|---|---|---|---|
const int *p (same as int const *p) |
p is a pointer to an int that is const | no | yes |
int *const q |
q is a const pointer to an int | yes | no |
const int *const r |
r is a const pointer to a const int | no | no |
const before the * protects the value; const after the * protects the pointer.
Round 4: macros
Question 10: SQUARE(2 + 3)
#include <stdio.h>
#define SQUARE(x) x * x
int main() {
printf("%d\n", SQUARE(5));
printf("%d\n", SQUARE(2 + 3));
return 0;
}
The obvious answer: 25 and 25. Run it:
25
11
Why. Real-life example: a rubber stamp. A macro is text substitution done by the preprocessor before compiling (episode 16). SQUARE(2 + 3) becomes 2 + 3 * 2 + 3, and multiplication comes first: 2 + 6 + 3 = 11.
The fix: brackets around every parameter, and around the whole body.
#define SQUARE(x) ((x) * (x))
25
25
Even better, use a function (static inline int square(int x) { return x * x; }): it evaluates its argument once, so SQUARE(i++) can't increment i twice.
Coding round
Reverse a string in place
In place means no second array: you change the letters where they are, with O(1) extra memory. Real-life example: two people at both ends of a queue swap places, then both step inward, until they meet in the middle.
The obvious loop goes all the way:
#include <stdio.h>
#include <string.h>
void reverse(char s[]) {
int n = strlen(s);
for (int i = 0; i < n; i++) {
char t = s[i];
s[i] = s[n - 1 - i];
s[n - 1 - i] = t;
}
}
int main() {
char word[] = "chai";
reverse(word);
printf("%s\n", word);
return 0;
}
chai
Unchanged! The first half of the loop reversed the word; the second half swapped every pair back.
The fix: stop in the middle, i < n / 2. Then test the edge cases interviewers always ask about: an odd length, one letter, and the empty string.
#include <stdio.h>
#include <string.h>
void reverse(char s[]) {
int n = strlen(s);
for (int i = 0; i < n / 2; i++) {
char t = s[i];
s[i] = s[n - 1 - i];
s[n - 1 - i] = t;
}
}
int main() {
char a[] = "chai", b[] = "tea", c[] = "a", d[] = "";
reverse(a); reverse(b); reverse(c); reverse(d);
printf("[%s] [%s] [%s] [%s]\n", a, b, c, d);
return 0;
}
[iahc] [aet] [a] []
With an odd length the middle letter stays; with one letter or an empty string the loop never runs. Time O(n), extra space O(1).
Is n a power of two?
The obvious way: keep dividing by 2 in a loop. The one-line trick (episode 18): a power of two has exactly one 1 bit. Subtracting 1 turns that bit off and turns on every bit after it, so n and n - 1 share no bits.
8 = 1000 7 = 0111 8 & 7 = 0000 → power of two
12 = 1100 11 = 1011 12 & 11 = 1000 → not
#include <stdio.h>
int is_power_of_two(int n) {
return (n & (n - 1)) == 0;
}
int main() {
int tests[] = {1, 8, 12, 64, 0};
for (int i = 0; i < 5; i++)
printf("%d -> %s\n", tests[i], is_power_of_two(tests[i]) ? "yes" : "no");
return 0;
}
1 -> yes
8 -> yes
12 -> no
64 -> yes
0 -> yes
0 -> yes is wrong: zero is not a power of two, but 0 & -1 is 0. The fix:
return n > 0 && (n & (n - 1)) == 0;
1 -> yes
8 -> yes
12 -> no
64 -> yes
0 -> no
n > 0 also rejects negative numbers. O(1) time.
Quick-fire theory
| Question | One-line answer |
|---|---|
What does static do on a local variable? |
It keeps its value between calls; it lives for the whole program (episode 17). |
What does static do on a global or a function? |
It makes it visible only inside its own .c file. |
What is extern? |
"Declared here, defined in another file" (episode 20). |
What is volatile? |
The value can change outside the program (a hardware register, a signal handler), so the compiler must read it from memory every time. |
| Stack vs heap? | Stack: locals, freed automatically when the function returns. Heap: malloc, stays until you free it. |
malloc vs calloc? |
malloc gives uninitialised memory (garbage); calloc gives zero-filled memory and takes a count and a size (episode 14). |
i++ vs ++i? |
i++ gives the old value, then increments; ++i increments, then gives the new value. |
What's wrong with i++ + i++? |
It changes i twice without a sequence point: undefined behaviour. -Wall: operation on 'i' may be undefined [-Wsequence-point]. |
| What is a dangling pointer? | A pointer to memory that was freed or went out of scope. |
| What is a memory leak? | Heap memory that is never freed and can no longer be reached. |
NULL vs '\0'? |
NULL is a null pointer; '\0' is the character with value 0 that ends a string. |
What does return 0 from main mean? |
Success, reported to the operating system. |
How to answer like an engineer
- Say your prediction out loud, with a reason: "12, because of padding" beats "12".
- Give the reason in one line: alignment, decay, unsigned conversion, read-only literal, addresses, scope, text substitution.
- Give the fix: reorder, pass
n, cast,fabs,long long,char[],strcmp,static, brackets. - Mention edge cases: 0, negative, empty, one element, overflow.
- Mention the tool that would catch it:
-Wall -Wextra, sanitizers (episode 26),gdb(episode 21).
Common mistakes (and the real messages)
| Code | What you see | Why | Fix |
|---|---|---|---|
sizeof(marks) where marks is a parameter |
warning: 'sizeof' on array function parameter 'marks' will return size of 'int *' [-Wsizeof-array-argument] |
the array decayed to a pointer | compute sizeof(a) / sizeof(a[0]) where the array is, pass n |
if (x < sizeof(int)) with x = -1 |
no message (with -Wextra: comparison of integer expressions of different signedness) |
-1 converted to a huge unsigned value |
(int)sizeof(int) |
if (price == 0.1) |
no message, prints not equal |
float and double 0.1 are different | fabs(a - b) < 1e-6 |
int pairs = people * people; |
no message, pairs = -1794967296 |
signed overflow | (long long)people * people |
char *city = "delhi"; city[0] = 'D'; |
Segmentation fault |
string literals are read-only | char city[] = "delhi"; |
if (typed == "admin") |
no message by default, Wrong password (-Wall: comparison with string literal results in unspecified behavior) |
compares addresses | strcmp(typed, "admin") == 0 |
char otp[7] = ...; return otp; |
warning: function returns address of local variable [-Wreturn-local-addr], prints (null) |
the local is gone after return | static, malloc, or a caller's buffer |
const int *p; *p = 500; |
error: assignment of read-only location '*p' |
const before * protects the value |
remove const, or use int *const p if only the pointer is fixed |
int *const q; q = &other; |
error: assignment of read-only variable 'q' |
const after * protects the pointer |
declare q without the second const |
#define SQUARE(x) x * x |
no message, SQUARE(2 + 3) is 11 |
macros paste text | ((x) * (x)) or an inline function |
reverse with i < n |
no message, string unchanged | swapped twice | i < n / 2 |
(n & (n - 1)) == 0 alone |
no message, 0 -> yes |
0 has no 1 bits | n > 0 && ... |
Mini project: your own string functions
Interviewers love "write strlen / strcpy / strcmp yourself" and "swap two numbers with a function". All four, run:
#include <stdio.h>
size_t my_strlen(const char *s) {
const char *p = s;
while (*p) p++;
return p - s;
}
char *my_strcpy(char *dest, const char *src) {
char *d = dest;
while ((*d++ = *src++))
;
return dest;
}
int my_strcmp(const char *a, const char *b) {
while (*a && *a == *b) { a++; b++; }
return (unsigned char)*a - (unsigned char)*b;
}
void swap(int *a, int *b) {
int t = *a; *a = *b; *b = t;
}
int main() {
char buf[20];
my_strcpy(buf, "SyntaxPilot");
printf("%s has %zu letters\n", buf, my_strlen(buf));
printf("cmp(admin, admin) = %d\n", my_strcmp("admin", "admin"));
printf("cmp(apple, mango) < 0: %d\n", my_strcmp("apple", "mango") < 0);
int x = 3, y = 7;
swap(&x, &y);
printf("x = %d, y = %d\n", x, y);
return 0;
}
SyntaxPilot has 11 letters
cmp(admin, admin) = 0
cmp(apple, mango) < 0: 1
x = 7, y = 3
Things to say while writing them: my_strlen walks to the '\0' and subtracts two pointers; my_strcpy copies the '\0' too (the loop stops after copying it), and the caller must make sure dest is big enough; my_strcmp compares as unsigned char so letters above 127 sort correctly; swap needs pointers because C passes arguments by value (episode 10).
Practice
- Predict
sizeofofstruct { char a; double b; char c; }, then reorder it to make it smaller. - Count the 1 bits of a number with the
n & (n - 1)trick. - Check whether a string is a palindrome, in place (no second array).
- Explain
const char *const sin one line.
Solutions
Questions 1 to 3, run together:
#include <stdio.h>
#include <string.h>
struct A { char a; double b; char c; };
struct B { double b; char a; char c; };
int count_bits(unsigned n) {
int count = 0;
while (n) {
n &= n - 1; // clears the lowest 1 bit
count++;
}
return count;
}
int is_palindrome(const char *s) {
int i = 0, j = strlen(s) - 1;
while (i < j) {
if (s[i] != s[j]) return 0;
i++; j--;
}
return 1;
}
int main() {
printf("struct A: %zu, struct B: %zu\n", sizeof(struct A), sizeof(struct B));
printf("bits in 13: %d, in 255: %d, in 0: %d\n", count_bits(13), count_bits(255), count_bits(0));
printf("madam: %d, chai: %d, a: %d, empty: %d\n", is_palindrome("madam"), is_palindrome("chai"), is_palindrome("a"), is_palindrome(""));
return 0;
}
struct A: 24, struct B: 16
bits in 13: 3, in 255: 8, in 0: 0
madam: 1, chai: 0, a: 1, empty: 1
struct Ais 24:a(1) + 7 padding,b(8),c(1) + 7 padding. Big first (struct B): 8 + 1 + 1 + 6 padding = 16.- Each
n &= n - 1clears the lowest 1 bit, so the loop runs once per 1 bit (13 = 1101 has three). This is Brian Kernighan's trick. - Two indexes walk inward and compare; the empty string gives
j = -1, the loop never runs, and it counts as a palindrome. const char *const s: s is a const pointer to const chars: you can change neither the letters nor wherespoints.
Quick quiz
#define DOUBLE(x) x + x
int main() {
printf("%d\n", DOUBLE(3) * 2);
return 0;
}
- A.
12 - B.
9 - C.
6 - D.
18
Answer
B: 9. The macro pastes text: 3 + 3 * 2 = 3 + 6 = 9. With #define DOUBLE(x) ((x) + (x)) it prints 12.
More quick checks:
- What is
sizeof(struct { char c; int i; })on our machine?Answer
8: 1 + 3 padding + 4. - Inside
void f(char s[]), what issizeof(s)?Answer
The size of a pointer (8 here), not the array. - What does
strcmp("abc", "abd")return: zero, negative or positive?Answer
Negative:'c'comes before'd'. - Is
unsigned u = 0; u - 1 > 0true?Answer
Yes: unsigned arithmetic wraps,u - 1is 4294967295.
Interview questions
- Why is a struct often bigger than the sum of its members? How do you reduce padding? What does
#pragma packdo, and what does it cost? - What is array decay? Name the three cases where an array does not decay (
sizeof,&array, a string literal initialising achararray). - What happens when you compare a signed and an unsigned integer? How would you catch it?
- Why should you never compare floats with
==? How do you compare them? Why is money often stored in integers? - What is integer overflow? Why is signed overflow undefined but unsigned overflow well defined?
char *s = "hi"vschar s[] = "hi": where is each stored, and which can you modify?- Why does
==not compare strings? How doesstrcmpwork, and what does it return? - What is a dangling pointer? Give three ways to return a string from a function safely.
- Explain
const int *p,int *const pandconst int *const p. - Why are function-like macros dangerous? Give two macro traps and their fixes.
- Reverse a string in place. What are the edge cases? What are the time and space complexities?
- Check if a number is a power of two in O(1). Why does the trick fail for 0?
- What do
static,externandvolatilemean? - Stack vs heap: what lives where, and who frees it?
Glossary
| Term | Meaning | Example |
|---|---|---|
| Alignment | The CPU's rule that a type should start at an address that is a multiple of its size (or a smaller power of two) | an int starts at a multiple of 4 |
| Padding | Unused bytes the compiler adds inside or at the end of a struct to keep members aligned | 3 bytes after char coach |
sizeof |
An operator giving the size in bytes of a type or variable, worked out at compile time | sizeof(int) is 4 |
size_t |
The unsigned type that sizeof returns; printed with %zu |
size_t n = sizeof(marks); |
| Array decay | An array used as a value (passed to a function, assigned to a pointer) turns into a pointer to its first element | int marks[] parameter = int *marks |
| Signed / unsigned | Signed types can hold negative values; unsigned types can't | int vs unsigned int |
| Usual arithmetic conversions | C's rules for converting both sides of an operator to a common type | -1 < sizeof(int) converts -1 to size_t |
| Floating point | Numbers with a fractional part stored in binary (float 32-bit, double 64-bit) |
0.1 is stored as 0.1000000014… |
| Tolerance (epsilon) | A tiny allowed difference when comparing floats | fabs(a - b) < 1e-6 |
fabs |
Absolute value of a double, from math.h |
fabs(-0.5) is 0.5 |
| Integer overflow | A result too big for its type | 50000 * 50000 in an int |
| Undefined behaviour | Code the C standard gives no meaning to; anything may happen | signed overflow, writing to a literal |
long long |
An integer type of at least 64 bits, printed with %lld |
long long pairs |
| String literal | Text in double quotes, stored in read-only memory | "delhi" |
strcmp |
Compares two strings letter by letter; 0 when equal | strcmp(typed, "admin") == 0 |
| Stack frame | The memory a function call uses for its locals; released at return | otp inside make_otp |
| Dangling pointer | A pointer to memory that was freed or went out of scope | the pointer make_otp returned |
static local |
A local variable that lives for the whole program | static char otp[7] |
const |
Marks something read-only | const int *p, int *const q |
| Macro | A name the preprocessor replaces with text before compiling | #define SQUARE(x) ((x) * (x)) |
| Inline function | A small function the compiler may paste in place of the call; evaluates its arguments once | static inline int square(int x) |
| In place | Changing data where it is, without a second copy | reversing word itself |
| Edge case | An input at the extremes: 0, empty, one element, the maximum | "" for reverse |
volatile |
Tells the compiler a value can change outside the program, so it must be re-read every time | a hardware status register |
extern |
Declares a variable defined in another file | extern int count; |
| Sequence point | A point where all earlier side effects are complete; changing a variable twice between two is undefined | i++ + i++ |
Summary
- Sizes: a struct has padding for alignment (
struct Ticketis 12, reordered 8); an array parameter is a pointer, so pass the length. - Types:
-1 < sizeof(int)is false becausesize_tis unsigned; compare floats withfabs(a - b) < eps; avoid overflow withlong long, casting before you multiply. - Pointers and strings: string literals are read-only (use
char s[]);==compares addresses (usestrcmp, 0 means equal); never return the address of a local (usestatic,mallocor a caller's buffer); readconstdeclarations right to left. - Macros paste text: bracket every parameter and the whole body, or use an inline function.
- Coding round: reverse in place with
i < n / 2; a power of two isn > 0 && (n & (n - 1)) == 0; always test the edge cases. - How to answer: prediction, reason, fix, edge cases.
Next episode: the final episode of the C course, What next after C: C++, embedded and systems programming.