All Exams Test series for 1 year @ ₹349 only
Question

What is the output the given C language code snippet? 

#include<stdio.h>

 int main()

{

 int x[] = {10,20,30}; 

int *p = x; 

p++; 

printf("%d",*p); 

return 0; 

}

The correct answer is

20

This snippet demonstrates pointer arithmetic on an array. In C, the name of an array (here x) decays to the address of its first element, and adding to a pointer moves it by whole elements, not by single bytes.

Tracing the execution:

  • int x[] = {10, 20, 30}; — three integers are stored contiguously, at indices 0, 1 and 2.
  • int *p = x;p is set to the address of x[0], so *p would be 10.
  • p++; — this is the key step. Incrementing an int pointer advances it by sizeof(int) bytes, i.e. by exactly one array element. So p now points to x[1].
  • printf("%d", *p); — dereferencing gives x[1], which is 20.

Why the other outputs are wrong: 10 would be printed only if p had not been incremented (still pointing at x[0]). 30 would need two increments to reach x[2], but p++ advances just one element. A garbage value would arise only if p were moved past the end of the array (out of bounds); here it still points to a valid element, so the result is the well-defined value 20.

Was this answer helpful?

Important Questions from Array

  1. Which of the following initialization statement store six integer values in array?

  2. Which of the following expression will delete the entire array pointed to by q?

  3. Consider the following C program segment.

    #include <stdio.h>

    int main()
    {
      char s1[7] = "1234";
        char *p;

        p = s1 + 2;
        *p = '0';

        printf("%s", s1);

        return 0;
    }

    What will be printed by the program?
  4. #include <stdio.h>
    
    void foo(int *p, int x){
        *p = x;
    }
    
    int main(){
        int *z;
        int a = 20, b = 25;
        z = &a;
        foo(z, b);
        printf("%d", a);
        return 0;
    }
    
    The output of the given C program is __________. (Answer in integer)
  5. Consider an array $A$ of integers of size $n$. The indices of $A$ run from 1 to $n$. An algorithm is to be designed to check whether $A$ satisfies the condition given below.

    $\forall i,j \in \{1, \dots, n-1\}$ such that $i > j$, $(A[i + 1] - A[i]) > (A[j + 1] - A[j])$

    Which one of the following gives the worst case time complexity of the fastest algorithm that can be designed for the problem?
Need Expert Advice?
Test Series
RRB JE img
Railways
RRB JE Prev. Yr. Paper (CBT 1 + CBT 2) Test Series
290 Tests 5 Tests Free
4474 Attempts
4.3(88)
English

Start Your Preparation with Prepp Mobile App

Download the app from Google Play & App Store
Download the app from Google Play & App Store
Prepp Mobile App