C Programming, Control Flow and Array Algorithms Notes for GATE CS
C Programming, Control Flow and Array Algorithms notes for GATE CS: 50 study cards covering concepts, formulas, shortcuts and exam traps, plus solved practice
c programming control flow and array algorithms notes
Chapter Roadmap: C Programming, Control Flow and Array Algorithms
Chapter Journey
A vertical path through the five topics of this chapter, in the order you will master them.
1. Loop Tracing and Iterative Computation
Highest weightage in this chapter. The foundation. You will learn to trace any loop by hand.
2. Function Calls, Evaluation Order and Static State
What happens across function boundaries. Static variables and argument evaluation order.
3. Bitwise and Character Expressions
Small topic. Tricky on the day of the exam. Shifts, masks, and ASCII arithmetic.
4. Array Processing and Iterative Algorithms
Loops meet arrays. Polynomial evaluation, subarrays. The bridge to Data Structures.
5. Variable Scope, Shadowing and Compilation
Which variable wins when names collide. Block scope, file scope, shadowing rules.
What you will be able to do by the end: read any C snippet the exam throws at you, trace it in your head, and write the exact output in under two minutes.
What Loop Tracing Actually Is
The Core Idea
A loop is just a repeated action. Tracing it is just being a careful accountant.
You have a small set of variables.
The loop changes them, one iteration at a time.
Your job: write down every variable, at every step.
At the end, read off the answer.
There is no trick. The only way students lose marks here is by trying to do it in their head and skipping a step. Do not skip a step.
First principle: a C program is a deterministic state machine. If you know the state before an instruction, you know the state after. Tracing is just applying this, one line at a time.
The Trace Table: Your Main Weapon
Build a Table, Every Time
iter
i
j
count
notes
init
0
-3
0
before loop
1
2
...
Columns: every variable in the program, plus an output column if there is a printf.
Rows:
Top row: values before the loop starts.
One row per iteration: values after the body of that iteration finishes.
Rule: one variable change per cell. Never combine two updates into one row. This single habit, done on paper, solves nearly every loop question in this exam.
47 more cards in this chapter
Try a question
Answer it here to see how it works. Nothing is recorded until you sign in.
Question 1
Level 1: Warm-up
Consider the C expression (x > 0) && (y++). If x is -1 and y is 5 before the expression is evaluated, what is the value of y immediately after?
Question 2
Level 1: Warm-up
When an expression like i++ is used inside a loop condition, it performs two distinct actions. What are these two actions in the correct order?
Question 3
Level 1: Warm-up
Consider the C expression (x < 0) && (y++). If x is 5 and y is 10 before evaluation, which statement correctly describes the outcome?
Question 4
Level 1: Warm-up
Which of the following is IMPOSSIBLE for a static local variable declared inside a C function?
Question 5
Level 1: Warm-up
According to the C standard, what is the behavior of the expression printf("%d %d", i++, i++) if i is initially 5?
Question 6
Level 1: Warm-up
Assertion (A): A static local variable in C retains its value between function calls.
Reason (R): The static keyword changes the scope of the variable to the entire program, making it visible everywhere.
Question 7
Level 1: Warm-up
Match the following C programming concepts with their correct descriptions:
P. static local variable
Q. auto local variable
R. Function argument evaluation order (unspecified)
Destroyed when the function returns
Undefined behavior if the same variable is modified multiple times
Initialized only once, retains value across calls
Question 8
Level 1: Warm-up
Rank the following steps of a C function call in the correct chronological order of execution:
P. Arguments are evaluated.
Q. Activation record is pushed onto the stack.
R. Function body executes.
S. Activation record is popped.
Question 9
Level 1: Warm-up
According to the Function Calls and Static State Checklist, rank the following tracing priorities in the exact sequential order they are presented as rules/tips for manual evaluation:
P. Maintain a separate Static Memory column to track values surviving returns.
Q. Resolve the innermost function call first.
R. Recognize that all recursive calls share the same static variable without resetting.
Question 10
Level 1: Warm-up
Horner's method evaluates a polynomial of degree n in a single forward pass. What is the maximum number of multiplications required to evaluate a polynomial of degree 3 using this method?
Free preview ends here
Login to view the complete notes
Creating an account is free. You get the rest of this chapter, step-by-step solutions, and a study plan built around the topics you are actually weak at.
Most platforms hand everyone the same content. Here the content moves with your performance, topic by topic.
Built around you, not around a syllabus PDF
Every answer you give moves your topic-level intelligence rate. The next question, the next revision card and tomorrow's plan all change with it.
Revision that hits your weak spots
We only revise topics you have actually attempted and are still below the safe bar on — never the same chapter on repeat.
Questions calibrated to the real exam
Each question carries a measured toughness. You are served a rung above your current level, so practice keeps stretching you.
Notes written for recall, not for volume
Full lesson cards for first study, curated short-note cards for the last mile — with derivations, traps and exam patterns marked.
One place for everything
Notes, chapter practice, previous-year questions, test series and full-length papers — all feeding one picture of your preparation.
Honest progress
No vanity streaks. Progress here means chapters mastered and accuracy that held up on harder questions.
Unlock the whole course
Full notes and short notes, the complete question bank with worked solutions, mock tests, full-length papers, and an adaptive plan that rebuilds itself as you improve.
C Programming, Control Flow and Array Algorithms Notes for GATE CS
C Programming, Control Flow and Array Algorithms notes for GATE CS: 50 study cards covering concepts, formulas, shortcuts and exam traps, plus solved practice questions.
Chapter Roadmap: C Programming, Control Flow and Array Algorithms
Chapter Journey
A vertical path through the five topics of this chapter, in the order you will master them.
1. Loop Tracing and Iterative Computation
Highest weightage in this chapter. The foundation. You will learn to trace any loop by hand.
2. Function Calls, Evaluation Order and Static State
What happens across function boundaries. Static variables and argument evaluation order.
3. Bitwise and Character Expressions
Small topic. Tricky on the day of the exam. Shifts, masks, and ASCII arithmetic.
4. Array Processing and Iterative Algorithms
Loops meet arrays. Polynomial evaluation, subarrays. The bridge to Data Structures.
5. Variable Scope, Shadowing and Compilation
Which variable wins when names collide. Block scope, file scope, shadowing rules.
What you will be able to do by the end: read any C snippet the exam throws at you, trace it in your head, and write the exact output in under two minutes.
What Loop Tracing Actually Is
The Core Idea
A loop is just a repeated action. Tracing it is just being a careful accountant.
You have a small set of variables.
The loop changes them, one iteration at a time.
Your job: write down every variable, at every step.
At the end, read off the answer.
There is no trick. The only way students lose marks here is by trying to do it in their head and skipping a step. Do not skip a step.
First principle: a C program is a deterministic state machine. If you know the state before an instruction, you know the state after. Tracing is just applying this, one line at a time.
The Trace Table: Your Main Weapon
Build a Table, Every Time
iter
i
j
count
notes
init
0
-3
0
before loop
1
2
...
Columns: every variable in the program, plus an output column if there is a printf.
Rows:
Top row: values before the loop starts.
One row per iteration: values after the body of that iteration finishes.
Rule: one variable change per cell. Never combine two updates into one row. This single habit, done on paper, solves nearly every loop question in this exam.
For Loop: The Exact Order of Execution
The Four Beats of a for Loop
for ( init ; condition ; update )
body
The exact firing order:
1. init runs once, before anything else.
2. condition is checked. (If false, loop ends. If true, continue.)
3. body runs.
4. update runs.
5. Go back to step 2.
The update is not part of the body. It runs after the body, before the next condition check.
C Programming, Control Flow and Array Algorithms: Solved Questions with Step-by-Step Explanations (10 Problems)
Question 1 · Programming and Data StructuresMCQ
Consider the C expression (x > 0) && (y++). If x is -1 and y is 5 before the expression is evaluated, what is the value of y immediately after?
A.
y becomes 6 because y++ is always evaluated
B.
y remains 5 because short-circuit evaluation skips y++
C.
y becomes 4 because the && operator decrements the right operand
D.
The expression results in a compilation error due to side effects
Correct Answer:
B
Step-by-Step Solution
Key idea: Logical AND (&&) short-circuits if the left operand is false.
Step 1: Evaluate the left operand: x > 0. Since x = -1, this is false.
Step 2: Because the left operand is false, the entire && expression is false regardless of the right side.
Step 3: C short-circuits, meaning the right operand (y++) is never evaluated.
Step 4: y is not incremented and remains 5.
Answer: y remains 5.
Question 2 · Programming and Data StructuresMCQ
When an expression like i++ is used inside a loop condition, it performs two distinct actions. What are these two actions in the correct order?
A.
Increments i, then returns the new value for the test
B.
Returns the current value of i for the test, then increments i
C.
Returns the current value of i for the test, then decrements i
D.
Increments i, then returns the old value for the test
Correct Answer:
B
Step-by-Step Solution
Key idea: The post-increment operator i++ returns the old value and then increments.
Step 1: Recall the behavior of the post-increment operator in C.
Step 2: It first returns the current value of i to be used in the expression (the test).
Step 3: Then, as a side effect, it increments i by 1.
Answer: Returns the current value of i for the test, then increments i.
Question 3 · Programming and Data StructuresMCQ
Consider the C expression (x < 0) && (y++). If x is 5 and y is 10 before evaluation, which statement correctly describes the outcome?
A.
y becomes 11 because the && operator evaluates both operands.
B.
y remains 10 because short-circuit evaluation skips the y++ operand.
C.
y becomes 11 because the left operand x < 0 is true.
D.
The expression causes a compilation error due to the side effect.
Correct Answer:
B
Step-by-Step Solution
Insight: The && operator short-circuits if the left operand is false.
Exam route: x < 0 is false, so y++ is skipped. y remains 10.
Learning route: Step 1: Evaluate the left operand of &&: x < 0. Since x = 5, this is false. Step 2: Apply the short-circuit rule for &&: if the left side is false, the right side is never evaluated. Step 3: Conclude that y++ is skipped, and y retains its original value of 10.
Answer: y remains 10 because short-circuit evaluation skips the y++ operand.
Question 4 · Programming and Data StructuresMCQ
Which of the following is IMPOSSIBLE for a static local variable declared inside a C function?
A.
Retaining its value between two consecutive function calls
B.
Being initialized with a constant expression
C.
Having its scope extend outside the function in which it is declared
D.
Being shared across all recursive calls of the same function
Correct Answer:
C
Step-by-Step Solution
Key idea: This is a boundary-case question testing the strict distinction between scope and lifetime.
Step 1: Recall that the static keyword changes the lifetime of a local variable to the entire program execution.
Step 2: Recall that the static keyword does not change the scope of a local variable. Its scope remains strictly confined to the block (function) in which it is declared.
Step 3: Evaluate the options. Retaining value (A), constant initialization (B), and sharing across recursion (D) are all valid properties of static local variables.
Step 4: Extending scope outside the function (C) is impossible; that would require a global variable.
Answer: Option C is impossible.
Question 5 · Programming and Data StructuresMCQ
According to the C standard, what is the behavior of the expression printf("%d %d", i++, i++) if i is initially 5?
A.
It always prints 5 6
B.
It always prints 6 5
C.
It is undefined behavior
D.
It results in a compilation error
Correct Answer:
C
Step-by-Step Solution
Insight: Modifying the same variable multiple times in a single expression without a sequence point is undefined in C.
Exam route: Recognize the multiple unsequenced modifications of i. This is a classic undefined behavior trap.
Learning route: Step 1: Analyze the expression printf("%d %d", i++, i++). Step 2: Notice that the variable i is modified twice (via i++) within the same function call. Step 3: Recall that the C standard does not specify the order of evaluation of function arguments. Step 4: Modifying a variable multiple times between sequence points without a defined order leads to undefined behavior.
Answer: It is undefined behavior.
Question 6 · Programming and Data StructuresMCQ
Assertion (A): A static local variable in C retains its value between function calls.
Reason (R): The static keyword changes the scope of the variable to the entire program, making it visible everywhere.
A.
Both A and R are true and R is the correct explanation of A
B.
Both A and R are true but R is NOT the correct explanation of A
C.
A is true but R is false
D.
A is false but R is true
Correct Answer:
C
Step-by-Step Solution
Insight: The static keyword affects the lifetime of a local variable, not its scope.
Exam route: A is true (static variables retain value). R is false (scope remains local to the block).
Learning route: Step 1: Evaluate Assertion (A). A static local variable is initialized once and retains its value across calls. This is true. Step 2: Evaluate Reason (R). The static keyword changes the lifetime to the entire program execution, but the scope remains strictly within the block or function where it is declared. It is not visible everywhere. This is false. Step 3: Conclude that A is true, but R is false.
Answer: A is true but R is false.
Question 7 · Programming and Data StructuresMCQ
Match the following C programming concepts with their correct descriptions:
P. static local variable
Q. auto local variable
R. Function argument evaluation order (unspecified)
Destroyed when the function returns
Undefined behavior if the same variable is modified multiple times
Initialized only once, retains value across calls
A.
P-3, Q-1, R-2
B.
P-1, Q-3, R-2
C.
P-3, Q-2, R-1
D.
P-2, Q-1, R-3
Correct Answer:
A
Step-by-Step Solution
Key idea: This is a matching question testing fundamental definitions of variable lifetime and evaluation rules.
Step 1: Analyze P. A static local variable is initialized only once and retains its value across function calls. This matches description 3.
Step 2: Analyze Q. An auto (default) local variable is created on the stack and destroyed when the function returns. This matches description 1.
Step 3: Analyze R. If function arguments have side effects on the same variable and the evaluation order is unspecified, the C standard defines this as undefined behavior. This matches description 2.
Step 4: Combine the matches: P-3, Q-1, R-2.
Answer: Option A is the correct match.
Question 8 · Programming and Data StructuresMCQ
Rank the following steps of a C function call in the correct chronological order of execution:
P. Arguments are evaluated.
Q. Activation record is pushed onto the stack.
R. Function body executes.
S. Activation record is popped.
A.
P, Q, R, S
B.
Q, P, R, S
C.
P, R, Q, S
D.
Q, R, P, S
Correct Answer:
A
Step-by-Step Solution
Key idea: This is a sequencing question testing the mechanical lifecycle of a function call.
Step 1: Before a function can be called, its arguments must be evaluated to know what values to pass. (P is first).
Step 2: Once arguments are ready, the call is made, and an activation record (stack frame) is pushed onto the call stack to hold parameters and local variables. (Q is second).
Step 3: The function body then executes using the prepared activation record. (R is third).
Step 4: Upon return, the activation record is popped and destroyed. (S is last).
Answer: The correct order is P, Q, R, S.
Question 9 · Programming and Data StructuresMCQ
According to the Function Calls and Static State Checklist, rank the following tracing priorities in the exact sequential order they are presented as rules/tips for manual evaluation:
P. Maintain a separate Static Memory column to track values surviving returns.
Q. Resolve the innermost function call first.
R. Recognize that all recursive calls share the same static variable without resetting.
A.
Q, R, P
B.
P, Q, R
C.
R, Q, P
D.
Q, P, R
Correct Answer:
A
Step-by-Step Solution
Key idea: This is an order-ranking question testing recall of the specific sequence of the evaluation checklist.
Step 1: Recall the Function Calls and Static State Checklist order.
Step 2: Item 1 is "Nested Calls": Always resolve the innermost function call first. (This matches Q).
Step 3: Item 5 is "Static in Recursion": All recursive calls share the same static variable. (This matches R).
Step 4: Item 6 (Final Tip) is to maintain a separate Static Memory column. (This matches P).
Step 5: The sequential order presented in the checklist is Q, then R, then P.
Answer: The correct order is Q, R, P.
Question 10 · Programming and Data StructuresMCQ
Horner's method evaluates a polynomial of degree n in a single forward pass. What is the maximum number of multiplications required to evaluate a polynomial of degree 3 using this method?
A.
2
B.
3
C.
4
D.
6
Correct Answer:
B
Step-by-Step Solution
Key idea: This is an observation question testing the operational count of Horner's method.
Step 1: Recall the structure of Horner's method update: total = x * total + next_coefficient.
Step 2: Observe that each iteration of the loop performs exactly one multiplication.
Step 3: For a polynomial of degree n, there are exactly n iterations (processing the remaining n coefficients after the first).
Step 4: For degree n=3, the number of multiplications is exactly 3.