CrazyEngineers
  • Try these C aptitude

    moonfalsh

    Member

    Updated: Oct 26, 2024
    Views: 919
    Go for these C questions and tel the answer and errors if any:

    1. void main()
    {

    int const * p=5;
    printf("%d",++(*p));
    }

    2. main()
    {
    int c[ ]={2.8,3.4,4,6.7,5};
    int j,*p=c,*q=c;
    for(j=0;j<5;j++) {
    printf(" %d ",*c);
    ++q; }
    for(j=0;j<5;j++){
    printf(" %d ",*p);
    ++p; }
    }

    3. main()
    {
    extern int i;
    i=20;
    printf("%d",i);
    }


    4. main()
    {
    int i=-1,j=-1,k=0,l=2,m;
    m=i++&&j++&&k++||l++;
    printf("%d %d %d %d %d",i,j,k,l,m);
    }

    5. main()
    {
    printf("%x",-1<<4);
    }

    0
    Replies
Howdy guest!
Dear guest, you must be logged-in to participate on CrazyEngineers. We would love to have you as a member of our community. Consider creating an account or login.
Replies
  • Neha Kochhar

    MemberJan 20, 2010

    moonfalsh
    Go for these C questions and tel the answer and errors if any:

    1. void main()
    {
    int const * p=5;
    printf("%d",++(*p));
    }

    Answer of 1st question
    According to me it will result in an error as we cannot change the value the pointer is pointing as pointer is pointing to constant integer .
    Tell me where I am wrong?
    Are you sure? This action cannot be undone.
    Cancel
  • gaurav.bhorkar

    MemberJan 22, 2010

    moonfalsh
    3. main()
    {
    extern int i;
    i=20;
    printf("%d",i);
    }
    Output: 20

    Reason: A local variable is always preferred over global variable if their names are same.
    Are you sure? This action cannot be undone.
    Cancel
  • Manish Goyal

    MemberJan 22, 2010

    @ Gaurav which compiler are you using? Actually it is giving error in my system
    Are you sure? This action cannot be undone.
    Cancel
  • gaurav.bhorkar

    MemberJan 22, 2010

    goyal420
    @ Gaurav which compiler are you using? Actually it is giving error in my system
    I didn't compile that.

    You are getting error because the variable 'i' is not defined outside main ().
    Maybe my answer was wrong.
    Are you sure? This action cannot be undone.
    Cancel
  • Mangesh6688

    MemberJan 23, 2010

    moonfalsh

    3. main()
    {
    extern int i;
    i=20;
    printf("%d",i);
    }
    Its giving error as:

    "Undefined symbol _i in module"
    Are you sure? This action cannot be undone.
    Cancel
  • Mangesh6688

    MemberJan 23, 2010

    moonfalsh

    5. main()
    {
    printf("%x",-1<<4);
    }
    The %x format specifier represents the integer value in a hexadecimal form.
    -1 is represented as all 1's and when it is four times left shifted the least significant 4 bits are filled with 0's.

    Output: fff0
    Are you sure? This action cannot be undone.
    Cancel
  • gaurav.bhorkar

    MemberJan 23, 2010

    Mangesh6688
    Its giving error as:

    "Undefined symbol _i in module"
    Thats what I said. The variable should also be defined outside main ().
    Are you sure? This action cannot be undone.
    Cancel
  • Mangesh6688

    MemberJan 23, 2010

    moonfalsh

    2. main()
    {
    int c[ ]={2.8,3.4,4,6.7,5};
    int j,*p=c,*q=c;
    for(j=0;j<5;j++) {
    printf(" %d ",*c);
    ++q; }
    for(j=0;j<5;j++){
    printf(" %d ",*p);
    ++p; }
    }

    As c[] is declared as integer, c will contain {2,3,4,6,5}.
    In first 'for' loop we are printing *c, as *c contains 2 it will be printed 5 times.
    In second 'for' loop we are printing *p, initially *p contains 2 then it is incremented.
    So, 2,3,4,6,5 will be printed.

    Final Output: 2 2 2 2 2 2 3 4 6 5
    Are you sure? This action cannot be undone.
    Cancel
  • moonfalsh

    MemberJan 25, 2010

    Sorry for late reply guys.

    Answer for the questions are:

    1. Compilation error - cannot modify a constant value.

    2. 2222223465

    3. linker error- undefined symbol i

    4. 00131

    5. fffo
    Are you sure? This action cannot be undone.
    Cancel
  • sherya mathur

    MemberFeb 18, 2010

    answers are
    ans1. compilation error
    explanation
    because p is a constant integer but here we are going to change the value of a constant integer
    ans2: the out will be
    2 2 2 2 2 2 3 4 6 5
    Explanation:
    Initially pointer c is assigned to both p and q. In the first loop,
    since
    only q is incremented and not c , the value 2 will be printed 5 times.
    In
    second loop p itself is incremented. So the values 2 3 4 6 5 will be
    printed.
    ans3: Linker Error
    explaination: Undefined symbol '_i'
    Explanation:
    extern storage class in the following declaration,
    extern int i;
    specifies to the compiler that the memory for i is allocated in some
    other
    program and that address will be given to the current program at the
    time
    of linking. But linker finds that no other variable of name i is
    available
    in any other program with memory space allocated for it. Hence a linker
    error has occurred
    ans 4:00131
    ans5: fff0

    Are you sure? This action cannot be undone.
    Cancel
Home Channels Search Login Register