hamburger menu
All Coursesall course arrow
adda247
reward-icon
adda247
    arrow
    arrow
    arrow
    Consider the following C program: What is the output of the above program?
    Question



    Consider the following C program:

    What is the output of the above program?

    A.

    x = 0, y = 10

    B.

    x = 10, y = 10

    C.

    x = 10, y = some garbage value

    D.

    x = 0, y = some garbage value

    E.

    Compilation Error

    Correct option is C

    1. Explanation of the Code:
    · a is initialized to 10.
    · b is declared but not initialized. In C, local variables that are not explicitly initialized contain garbage values.
    · The function exampleFunction(int x, int y) is called with a and b as arguments.
    · x gets the value of a (which is 10), while y gets the value of b (which is uninitialized and hence contains a garbage value).
    2. Function Execution:
    · Inside exampleFunction, the printf statement outputs the values of x and y.
    · x = 10 (as it gets the value from a), and y is some garbage value due to the uninitialized variable b.
    3. Compilation:
    · The code compiles without any errors because there is no syntactical issue. However, the behavior of b is undefined due to its uninitialized state.
    Important Key Points:
    1. Uninitialized Variables: Local variables in C (like b in this case) are stored in stack memory and contain undefined values (garbage values).
    2. Function Arguments: The values of variables are passed to functions, and their state (initialized or uninitialized) is maintained.
    3. Garbage Value Behavior: Garbage values are unpredictable and can vary between program executions.
    Knowledge Booster:
    · Initialization Best Practice: Always initialize variables explicitly to avoid undefined behavior.
    · Global vs. Local Variables:
    · Global variables are initialized to 0 by default.
    · Local variables are not initialized and contain garbage values.
    · Passing Values to Functions: C uses pass-by-value for function arguments, so the values of variables are copied to the function parameters.
    The program will output something like: x = 10, y = some garbage value.

    test-prime-package

    Access ‘IBPS SO IT Officer’ Mock Tests with

    • 60000+ Mocks and Previous Year Papers
    • Unlimited Re-Attempts
    • Personalised Report Card
    • 500% Refund on Final Selection
    • Largest Community
    students-icon
    163k+ students have already unlocked exclusive benefits with Test Prime!

    Free Tests

    Free
    Must Attempt

    NICL AO Prelims Full Mock Test -01

    languageIcon English
    • pdpQsnIcon100 Questions
    • pdpsheetsIcon100 Marks
    • timerIcon60 Mins
    languageIcon English
    Free
    Must Attempt

    NICL AO Prelims Full Mock Test -02

    languageIcon English
    • pdpQsnIcon100 Questions
    • pdpsheetsIcon100 Marks
    • timerIcon60 Mins
    languageIcon English
    Free
    Must Attempt

    NICL AO Prelims Reasoning Section Test -1

    languageIcon English
    • pdpQsnIcon35 Questions
    • pdpsheetsIcon35 Marks
    • timerIcon20 Mins
    languageIcon English