Bird
0
0

Which of the following C code snippets correctly stores a user's age and then increments it by 1?

hard📝 Application Q8 of 15
C - Variables and Data Types
Which of the following C code snippets correctly stores a user's age and then increments it by 1?
A<pre>int age = 20; age = age + 1; printf("%d", age);</pre>
B<pre>int age; age = age + 1; printf("%d", age);</pre>
C<pre>int age = 20; printf("%d", age + 1);</pre>
D<pre>int age; printf("%d", age + 1);</pre>
Step-by-Step Solution
Solution:
  1. Step 1: Initialize variable before use

    int age = 20;
    age = age + 1;
    printf("%d", age);
    initializes age to 20 before incrementing.
  2. Step 2: Increment variable correctly

    int age = 20;
    age = age + 1;
    printf("%d", age);
    updates age by adding 1 and then prints it.
  3. Step 3: Analyze other options

    Options B and D use age uninitialized; C prints incremented value without updating variable.
  4. Final Answer:

    Option A correctly initializes and increments the variable -> Option A
  5. Quick Check:

    Initialize before incrementing [OK]
Quick Trick: Initialize variables before use [OK]
Common Mistakes:
  • Using uninitialized variables
  • Incrementing without assignment
  • Printing incremented value without updating variable

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More C Quizzes