Skip to content
Related Articles
Get the best out of our app
GFG App
Open App
geeksforgeeks
Browser
Continue

Related Articles

Output of C programs | Set 39 (Pre Increment and Post Increment)

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

Prerequisite : Pre-increment and Post increment 
Question 1 
 

C




#include <stdio.h>
 
int main()
{
    char* p = "mayhem";
    char c;
    int i;
    for (i = 0; i < 3; i++) {
        c = *p++;
    }
    printf("%c", c);
 
    return 0;
}


  1. y
  2. h
  3. e
  4. a
Answer : y 

Explanation: The pointer ‘p’ points at the third location of the character array. The reason is that in the ‘for’ loop iteration, the value of the character pointer ‘p’ has been incremented thrice.
Question 2 
 

CPP




#include<stdio.h>
void test(char c[])
{
    c=c+2;
    c--;
    printf("%c",*c);
}
int main()
{
    char ch[5]={'p','o','u','r'};
    test(ch);
     
    return 0;
}


What is the output of the above program? 
 

  1. p
  2. o
  3. u
  4. r

 

Answer: o 

Explanation: When the character array ‘ch’ is passed as an argument to the function ‘test()’, the base address of the array ‘ch[]’ is passed. The variable ‘c’ in the function ‘test()’ points at the second location of the array. And then it decrements by 1 pointing to ‘o’.
Question 3 
 

C




#include<stdio.h>
int main()
{
    int i;
    char ch[] = {'x', 'y', 'z'};
    char *ptr, *str1;
    ptr = ch;
    str1 = ch;
    i = (*ptr-- + ++*str1) - 10;
    printf("%d", i);
     
    return 0;
}


What is the output of the above program if the ASCII values of characters ‘x’=120, ‘y’=121, ‘z’=122? 
 

  1. 231
  2. 233
  3. 232
  4. 363

 

Answer : 231

Explanation: The pointer ptr points to ‘x’. 
Step1: Since, it is a post-decrement operation, hence the value remains 120 and is decremented later. 
Step2 :The pointer str1 points at ‘x’. Since ++ and * have same precedence level evaluation starts from Right to Left. For the expression ++*str1, Firstly *str1 is evaluated which gives 120 i.e. ASCII Equivalent of x. Next we evaluate the prefix increment ++120 = 121. Hence the final result is (120+121)-10=131

Question 4 – What will be the output of following Program? 
 

CPP




#include<stdio.h>
 
int main(void)
{
    char *ptr = "Linux";
    printf("\n [%c] \n", *ptr++);
    printf("\n [%c] \n", *ptr);
 
    return 0;
}


Output : 
 

    [L] 

    [i]

Explanation : Since the priority of both ‘++’ and ‘*’ are same so processing of ‘*ptr++’ takes place from right to left. Going by this logic, ptr++ is evaluated first and then *ptr. So both these operations result in ‘L’. Now since a postfix ‘++’ was applied on ptr so the next printf() would print ‘i’.
Question 5 – What will be the output of following Program? 
 

CPP




#include <stdio.h>
int main()
{
        int num1 = 5;
        int num2 = 3;
        int num3 = 2;
        num1 = num2++;
        num2 = --num3;
        printf("%d %d %d", num1, num2, num3);
        return 0;
}


  1. 231
  2. 311
  3. 327
  4. 321
Answer:311 

This article is contributed by Brahmani Sai. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
 


My Personal Notes arrow_drop_up
Last Updated : 24 Sep, 2021
Like Article
Save Article
Similar Reads