Home / CSE MCQs / C-MCQs :: Discussion

Discussion :: C-MCQs

  1. Comment on the output of this C code?

    int main()
    {
    const int i = 10;
    int *ptr = &i;
    *ptr = 20;
    printf("%d\n", i);
    return 0;
    }
  2. A.
    Compile time error
    B.
    Compile time warning and printf displays 20
    C.
    Undefined behaviour
    D.
    10

    View Answer

    Workspace

    Answer : Option B

    Explanation :

    Changing const variable through non-constant pointers invokes compiler warning
    Output:
    $ cc pgm2.c
    pgm2.c: In function 'main':
    pgm2.c:5: warning: initialization discards qualifiers from pointer target type
    $ a.out
    20


Be The First To Comment