Knowledge / Computer Science and Engineering / C programming fundamentals

C programming fundamentals

Computer Science and Engineering (B.E./B.Tech) · Programming · free study notes for exams, placements and interviews

In short

C is a small, statically typed, compiled procedural language created by Dennis Ritchie at Bell Labs (1972) and standardised as ANSI C (1989) / ISO C90, then C99, C11, C17 and C23. It gives direct control over memory and hardware with almost no runtime, which is why operating systems, embedded firmware, databases and interpreters (CPython included) are written in it. Every Indian engineering degree teaches it in the first year as "Programming for Problem Solving" (AICTE model curriculum), and it remains the base language for GATE CS programming questions, for aptitude-plus-coding rounds (TCS NQT, Infosys, Wipro, Cognizant, Accenture) and for embedded interviews (Bosch, Continental, Tata Elxsi, KPIT, Qualcomm, BEL, ISRO, DRDO, C-DAC). Interviewers use C precisely because it exposes types, sizes, overflow and evaluation order that higher-level languages hide.

Explained

The language, standards and the shape of a program

Data types, constants and type conversion

Operators, expressions and precedence

Control flow: decisions, loops and jumps

Functions, recursion and storage classes

Arrays, strings, structures, unions and enums

Input/output, files and the preprocessor

Key points

Formulas and examples

Unsigned n-bit range: 0 … 2n − 1 | Signed (two's complement) n-bit range: −2n−1 … 2n−1 − 1
Two's complement of x (n bits) = 2n − x = (~x) + 1 ; symbols: n = bits, ~ = bitwise NOT
Value of a signed 8-bit pattern p: if p > 127 then value = p − 256
Integer division identity: a == (a / b) * b + (a % b), for b ≠ 0
Address of a[i] = base + i × sizeof(element) (bytes)
Address of a[i][j] in T a[R][C] (row-major) = base + (i × C + j) × sizeof(T)
Total array bytes = number of elements × sizeof(element) ; bytes for an n-char string = n + 1
Iterations of for(i=a; i<b; i+=s) = ⌈(b − a) / s⌉ , s > 0
x << k == x × 2k ; x >> k == x / 2k (unsigned) ; x & (x−1) clears the lowest set bit
Recursive-call count of naive Fibonacci: C(0)=C(1)=1, C(n)=1+C(n−1)+C(n−2)

Example 1 — signed/unsigned comparison.

unsigned int u = 10; int i = -20;
printf("%d", i < u);   /* prints 0 */

Both operands have rank unsigned int, so i converts to unsigned: −20 + 232 = 4,294,967,276. Since 4,294,967,276 < 10 is false, the output is 0. Same reason sizeof(x) - 10 > 0 is true for every x except one of exactly 10 bytes: size_t is unsigned, so the subtraction wraps instead of going negative.

Example 2 — overflow on a narrow type. signed char c = 200; — signed char holds −128…127, and 200 > 127, so the value is reduced modulo 256: 200 − 256 = −56 (bit pattern 1100 1000). Printing it with %d gives −56; assigning it to unsigned char would give 200.

Example 3 — digits of a number (loop trace), n = 9875.

stepndigit = n % 10sumrev = rev*10 + digitn = n / 10
19875555987
29877125798
3988205789
4992957890

Sum of digits = 29, reverse = 5789, loop runs 4 times (= number of digits).

Example 4 — array addressing. int a[3][4]; with base address 2000 and sizeof(int) = 4:

**Example 5 — strlen vs sizeof and structure padding.**

char s[] = "ABLjobs";                 /* strlen(s) = 7, sizeof(s) = 8  */
struct A { char c; int i; char d; };  /* typical 4-byte int alignment  */

Layout: c at offset 0, 3 padding bytes, i at 4–7, d at 8, 3 trailing padding bytes → sizeof(struct A) = 12 bytes, against 6 bytes of real data. Reordering as { int i; char c; char d; } gives 8 bytes. Exact figures are compiler- and ABI-dependent.

Example 6 — bit tricks.

Example 7 — recursion cost. fib(5) by naive recursion: C(2)=3, C(3)=5, C(4)=9, C(5)=15 calls to return the value 5; the iterative version needs 4 additions. This is the standard "why is recursion expensive" answer.

ConstructTestMinimum runsTypical use
whilebefore body0unknown count, condition-driven
do...whileafter body1menus, "retry until valid input"
forbefore body0known count, index iteration
Featurestructunion
Memorysum of members + paddingsize of largest member
Members validall at onceonly the last one written
Userecords, nodes, configregister overlay, tagged value, memory saving

Where it is used

Interview questions

Common mistakes

Related topics

Pointers and dynamic memory · Arrays, linked lists, stacks and queues · Compilation, linking and memory layout · Exception handling and debugging · C++ essentials · Number systems and computer arithmetic

Open in the Knowledge reader →Find jobs →