chapter
    C Programming, Control Flow and Array Algorithms Short Notes for GATE CS

    C Programming, Control Flow and Array Algorithms short notes for GATE CS: 5 study cards covering concepts, formulas, shortcuts and exam traps, plus solved pra

    c programming control flow and array algorithms short notes

    Loop Tracing: The One-Page Checklist

    The Checklist

    1. Write a trace table. One column per variable. One row per iteration.
    2. For loop order: init, condition, body, update, condition, ...
    3. Side effects in conditions: track the value used for the test AND the value after the increment, separately.
    4. Short-circuit: for every && or || in a condition, ask "does the left side kill the right side on this iteration?"
    5. Integer division in while: check for fixed points. If the variable cannot change, the loop does not terminate.
    6. do-while: execute the body once BEFORE checking the condition.
    7. Boundary: add one extra row to your table where the condition fails, just to confirm.
    8. Symbolic question: trace 3-4 small inputs, guess the closed form, verify on one more.
    9. Recognize recipes: digit extraction, subtraction GCD, polynomial evaluation.
    10. Verify: always check your guessed formula on a value you did not use to derive it.
    If you follow these ten steps, you will not lose marks on loop tracing.

    Function Calls and Static State: The Checklist

    The Checklist

    1
    Nested Calls Always resolve the innermost function call first. Pass its return value to the outer function.
    2
    Evaluation Order If "right-to-left" is specified, evaluate the rightmost argument first, apply its side effects, and then move left.
    3
    Unspecified Order If no order is specified and arguments have side effects on the same variable, the behavior is undefined.
    4
    Static Lifetime A static local variable is initialized only once and retains its value across all function calls.
    5
    Static in Recursion All recursive calls share the same static variable. It does not reset or duplicate per call.
    6
    Auto Variables Default local variables are destroyed when the function returns. They do not remember past values.
    Final Tip: When tracing, maintain a separate "Static Memory" column in your trace table to track values that survive function returns.

    Bitwise Character Evaluation Checklist

    The Evaluation Checklist

    1. Translate: Convert all character literals to their decimal ASCII values immediately.
    2. Parenthesize: Respect operator precedence. Bitwise operators evaluate after arithmetic unless grouped by parentheses.
    3. Compute Bitwise: Perform AND, OR, or XOR on the 8-bit binary representations of the numbers.
    4. Compute Arithmetic: Add or subtract the resulting decimal values.
    5. Check Bounds: Ensure the final value fits within the signed char range ( to ) to avoid silent overflow.
    6. Translate Back: Convert the final decimal result to its corresponding ASCII character.
    Final Tip: When in doubt, write out the 8-bit binary representation of the numbers. It prevents mental math errors on bitwise operations and is the safest path to the correct answer.

    2 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)

    1. Destroyed when the function returns
    2. Undefined behavior if the same variable is modified multiple times
    3. 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 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 short 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.

    Why MastersUp

    Personalised first. High quality throughout.

    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 Short Notes for GATE CS

    C Programming, Control Flow and Array Algorithms short notes for GATE CS: 5 study cards covering concepts, formulas, shortcuts and exam traps, plus solved practice questions.

    Loop Tracing: The One-Page Checklist

    The Checklist

    1. Write a trace table. One column per variable. One row per iteration.
    2. For loop order: init, condition, body, update, condition, ...
    3. Side effects in conditions: track the value used for the test AND the value after the increment, separately.
    4. Short-circuit: for every && or || in a condition, ask "does the left side kill the right side on this iteration?"
    5. Integer division in while: check for fixed points. If the variable cannot change, the loop does not terminate.
    6. do-while: execute the body once BEFORE checking the condition.
    7. Boundary: add one extra row to your table where the condition fails, just to confirm.
    8. Symbolic question: trace 3-4 small inputs, guess the closed form, verify on one more.
    9. Recognize recipes: digit extraction, subtraction GCD, polynomial evaluation.
    10. Verify: always check your guessed formula on a value you did not use to derive it.
    If you follow these ten steps, you will not lose marks on loop tracing.

    Function Calls and Static State: The Checklist

    The Checklist

    1
    Nested Calls Always resolve the innermost function call first. Pass its return value to the outer function.
    2
    Evaluation Order If "right-to-left" is specified, evaluate the rightmost argument first, apply its side effects, and then move left.
    3
    Unspecified Order If no order is specified and arguments have side effects on the same variable, the behavior is undefined.
    4
    Static Lifetime A static local variable is initialized only once and retains its value across all function calls.
    5
    Static in Recursion All recursive calls share the same static variable. It does not reset or duplicate per call.
    6
    Auto Variables Default local variables are destroyed when the function returns. They do not remember past values.
    Final Tip: When tracing, maintain a separate "Static Memory" column in your trace table to track values that survive function returns.

    Bitwise Character Evaluation Checklist

    The Evaluation Checklist

    1. Translate: Convert all character literals to their decimal ASCII values immediately.
    2. Parenthesize: Respect operator precedence. Bitwise operators evaluate after arithmetic unless grouped by parentheses.
    3. Compute Bitwise: Perform AND, OR, or XOR on the 8-bit binary representations of the numbers.
    4. Compute Arithmetic: Add or subtract the resulting decimal values.
    5. Check Bounds: Ensure the final value fits within the signed char range ( to ) to avoid silent overflow.
    6. Translate Back: Convert the final decimal result to its corresponding ASCII character.
    Final Tip: When in doubt, write out the 8-bit binary representation of the numbers. It prevents mental math errors on bitwise operations and is the safest path to the correct answer.

    Array Iteration Checklist

    The Array Iteration Checklist

    1. Identify the Pattern: Does the problem require a running total (Accumulator) or a contiguous subsegment (Sliding Window)?
    2. Polynomial Evaluation: Default to Horner's method: total = x * total + next_coefficient.
    3. State Tracking: For "at most distinct elements" in space, maintain the count of the most recent contiguous sequence (trail_len) to shrink the window instantly.
    4. Boundary Check: Verify loop conditions. For an array of size , the valid index range is to .
    5. Complexity: Ensure the solution achieves time and auxiliary space where possible.
    Final Tip: When tracing iterative code manually, create a small table with columns for the loop variable, the array element, and all state variables. Update the table row by row to prevent mental math errors.

    C Programming, Control Flow and Array Algorithms: Solved Questions with Step-by-Step Explanations (10 Problems)

    Question 1 · Programming and Data Structures MCQ

    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?

    1. A.

      y becomes 6 because y++ is always evaluated

    2. B.

      y remains 5 because short-circuit evaluation skips y++

    3. C.

      y becomes 4 because the && operator decrements the right operand

    4. 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 Structures MCQ

    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?

    1. A.

      Increments i, then returns the new value for the test

    2. B.

      Returns the current value of i for the test, then increments i

    3. C.

      Returns the current value of i for the test, then decrements i

    4. 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 Structures MCQ

    Consider the C expression (x < 0) && (y++). If x is 5 and y is 10 before evaluation, which statement correctly describes the outcome?

    1. A.

      y becomes 11 because the && operator evaluates both operands.

    2. B.

      y remains 10 because short-circuit evaluation skips the y++ operand.

    3. C.

      y becomes 11 because the left operand x < 0 is true.

    4. 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 Structures MCQ

    Which of the following is IMPOSSIBLE for a static local variable declared inside a C function?

    1. A.

      Retaining its value between two consecutive function calls

    2. B.

      Being initialized with a constant expression

    3. C.

      Having its scope extend outside the function in which it is declared

    4. 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 Structures MCQ

    According to the C standard, what is the behavior of the expression printf("%d %d", i++, i++) if i is initially 5?

    1. A.

      It always prints 5 6

    2. B.

      It always prints 6 5

    3. C.

      It is undefined behavior

    4. 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 Structures MCQ

    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.

    1. A.

      Both A and R are true and R is the correct explanation of A

    2. B.

      Both A and R are true but R is NOT the correct explanation of A

    3. C.

      A is true but R is false

    4. 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 Structures MCQ

    Match the following C programming concepts with their correct descriptions:

    P. static local variable

    Q. auto local variable

    R. Function argument evaluation order (unspecified)

    1. Destroyed when the function returns
    2. Undefined behavior if the same variable is modified multiple times
    3. Initialized only once, retains value across calls
    1. A.

      P-3, Q-1, R-2

    2. B.

      P-1, Q-3, R-2

    3. C.

      P-3, Q-2, R-1

    4. 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 Structures MCQ

    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.

    1. A.

      P, Q, R, S

    2. B.

      Q, P, R, S

    3. C.

      P, R, Q, S

    4. 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 Structures MCQ

    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.

    1. A.

      Q, R, P

    2. B.

      P, Q, R

    3. C.

      R, Q, P

    4. 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 Structures MCQ

    Horner's method evaluates a polynomial of degree in a single forward pass. What is the maximum number of multiplications required to evaluate a polynomial of degree 3 using this method?

    1. A.

      2

    2. B.

      3

    3. C.

      4

    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 , there are exactly iterations (processing the remaining coefficients after the first).

    Step 4: For degree , the number of multiplications is exactly 3.

    Answer: 3.

    More short notes in this unit