C Programming by Yash Sir Formula Sheet: Rules, Traces and Revision Notes

Revise C through compact rules, use cues and checked traces. The sheet covers expressions, loops, arrays, functions, storage and dynamic memory.

KnowledgeGate Team

Exam prep & CS education

Updated 13 Nov 20266 min read
C Programming by Yash Sir Formula Sheet: Rules, Traces and Revision Notes

C syntax can look familiar during revision, yet one missed rule about integer division, short-circuiting, array decay, pointer bounds or object lifetime can change an entire trace. These rules sit within CS Fundamentals and the worked checks are 17 / 5 = 3, a four-iteration loop ending at sum = 13, an array-pointer trace returning 40, and sum_to(4) = 10.

Types, declarations and conversions for the formula sheet

Rule

When to use

Exact check

Integer division discards the fraction; for positive operands, a = (a / b) * b + a % b.

Trace quotient and remainder.

With a=17, b=5: a/b=3, a%b=2, 3*5+2=17.

Cast before division to retain a fraction. Mixed arithmetic converts the int for that operation.

Require floating point.

(double)a/b=3.4, but (double)(a/b)=3.0.

sizeof reports bytes as size_t, with no universal int byte count.

Measure an object.

For int marks[6]={11,13,17,19,23,29};, sizeof marks / sizeof marks[0] = 6.

In int *p, q;, only p is a pointer; in int *p, *q;, both are. When to use: parse each name outward. The length expression fails after an array parameter adjusts to a pointer. Use C Programming by Yash Sir: Worked Examples & Traces for the full concept sequence and worked examples. Use these formula checks for retrieval across operators, control flow, recursion, storage and allocation.

Operators, precedence, short-circuiting and sequencing

Precedence groups operators, but does not by itself specify operand evaluation order. With int x=3, y=4, z=2, x + y*z = 3 + 8 = 11, while (x+y)*z = 7*2 = 14. When to use: build the grouping before calculating. Assignment is right-associative, so a=b=6 assigns 6 to b and then to a; this does not make every repeated side effect safe.

For separate statements starting with int seen=0, 0 && ++seen is false and leaves seen=0; 1 || ++seen is true and again leaves seen=0. When to use: prove whether a guarded call, increment or dereference is reached.

Red flag: i++ + ++i and printf("%d %d", i++, i++) contain multiple unsequenced modifications of i. Their behaviour is undefined, so do not guess a left-to-right result. Signed integer overflow is also undefined, not guaranteed wraparound.

Diagram compares operator grouping for 3 + 4 * 2 and (3 + 4) * 2, short-circuit results, and undefined i++ + ++i.

Conditions and loops: calculate transitions instead of guessing

Zero is false and nonzero is true. When to use: reduce a condition before choosing a branch. if/else selects a route; switch selects a case; break exits the nearest loop or switch; continue advances the loop. A switch falls through unless control exits.

Trace int i, sum=0; for (i=1; i<=8; i*=2) { if (i==2) continue; sum += i; }:

Entry i

Branch

sum after body

Updated i

1

add 1

1

2

2

continue

1

4

4

add 4

5

8

8

add 8

13

16

The update runs after continue. Contributions 1,0,4,8 give sum=13; then 16<=8 is false. The geometric count is floor(log2(8/1))+1 = 3+1 = 4. When to use: only for a positive multiplicative progression with matching inclusive bounds. By contrast, for (i=2; i<=10; i+=2) visits 2,4,6,8,10, so (10-2)/2+1=5

Arrays, strings and pointers on one memory map

When array expression a becomes the address of its first element, a[i] == *(a+i). Pointer subtraction counts elements and is defined only within one array. When to use: verify that shared array first.

For int a[5]={10,20,30,40,50}; int *p=&a[1];, *p=20, p+2=&a[3], *(p+2)=40, and &a[4]-&a[1]=3. One-past pointer a+5 may be compared, not dereferenced.

For int m[3][4], m[2][1] is 2*4+1=9 elements from &m[0][0]. When to use: first confirm row length 4 and zero-based indices.

For char s[]="GATE", the elements are 'G','A','T','E','\0', so strlen(s)=4 and sizeof s=5 here. When to use: strlen counts characters before the terminator; sizeof counts this whole array. Arrays and Pointers in C: Memory, Arithmetic, Traps owns the dedicated arrays-and-pointers treatment, including its eight-result trace; use these notes for the shorter cross-topic checkpoint connecting pointers to loops, recursion, storage and allocation.

Diagram maps an int array and char string by index, pointer offsets, the one-past boundary, and strlen versus sizeof values.

Functions, recursion, scope and storage duration

C passes argument values. Given void bump(int n, int *m) { n++; (*m)++; } and int a=7, b=7; bump(a,&b);, caller a stays 7, while b becomes 8. When to use: pass an address to modify the caller's object, and validate it if null is possible.

For int sum_to(int n) { return n==0 ? 0 : n + sum_to(n-1); }, the base case is sum_to(0)=0, and each call progresses by reducing n by one:

sum_to(4)=4+sum_to(3)=4+3+sum_to(2)=4+3+2+sum_to(1)=4+3+2+1+sum_to(0)=10.

When to use: accept a recursive trace only after naming its base case and progress toward it.

Scope controls where a name is visible, linkage connects declarations, and storage duration controls how long an object exists. For local static int calls=0; return ++calls;, three calls in separate statements return 1, 2, then 3. The name remains block-scoped, while the object retains its value. extern declares a reference to an object or function with external linkage, not a new copy per use. When to use: classify name visibility and object lifetime separately.

Structures, dynamic memory and macros without shortcuts

A struct gives each member a distinct object; a union overlaps its members in the same storage. For struct Point { int x; int y; }; struct Point pt={3,4};, pt.x*pt.x + pt.y*pt.y = 9+16=25. When to use: access the selected member, but never assume a universal structure size because padding is implementation-dependent.

Allocate with int *v = malloc(6 * sizeof *v);. After checking that v is not null, set v[i]=(i+1)*(i+1) for i=0..5. The values are [1,4,9,16,25,36], and their sum is 1+4+9+16+25+36=91. Call free(v) and do not dereference v afterwards. When to use: choose dynamic storage when an automatic array cannot suitably handle the required size or lifetime. calloc zero-initialises allocated bytes; do not turn that into a universal claim about every semantic value representation.

Define the macro as #define SQUARE(x) ((x)*(x)). Then SQUARE(3+1)=((3+1)*(3+1))=4*4=16. When to use: parenthesise macro parameters and the complete replacement. Never use SQUARE(i++), because it expands to two unsequenced side effects. Prefer a function when an argument must be evaluated exactly once.

How C revision rules appear in practice questions

Practice asks you to trace expressions and loops, parse declarations, follow pointers, separate strlen from array size, trace parameters, find recursion bases, test static lifetime, or reject undefined behaviour. For the broader preparation map, use C Programming for GATE: Tested Areas and Prep Order gives the broader preparation map without fixing a weightage.

Check

Exact question to ask

Division

Was the cast applied before /?

Operators

Is this precedence or evaluation order?

Loop

What are the entry and post-update values?

Pointer

Is the target within the same array and safe to dereference?

Object

What are its scope, linkage and storage duration?

Three traps matter. (double)(17/5) is 3.0, not 3.4. sizeof on an array parameter measures its adjusted pointer type. Modifying a string literal through char * is undefined, so use char s[]="GATE". Check the conducting body's latest official syllabus or instructions for current scope.

C Programming by Yash Sir in the short version

Retrieve ten lines: cast before division; parenthesise grouping; short-circuit may skip effects; trace loop entry and update; use the length formula only on arrays; keep pointers within one array; strings end at \0; C passes values; static locals retain state; check and free allocations.

In fifteen minutes, recompute 17/5, rebuild the loop to sum=13, recover *(p+2)=40, and expand sum_to(4)=10. The C Programming by Yash Sir question set adds practice. Next, use the Free Learning & Guidance Program by Yash Jain Sir for free guidance, or the C Language course for structured study.