579 views
1 1 vote

What will be the output of the following program? 

#include <stdio.h>

int main() {
    int i[] = {1,2,3}, j[] = {1,2,3};
    int *p=i,*q=j;
    while ((*p++ = *q++) != 3) {  
        printf("%d", *p);
    }
    return 0;
}

 

1 Answer

1 1 vote

*p++ = *q++ first copies the value from j to i, then both pointers move to the next element.
The while condition checks the assigned value, not the next one.

After each assignment, p already points to the next element, so printf("%d", *p) prints that next value.

  • Assign 1 → p moves → prints 2

  • Assign 2 → p moves → prints 3

  • Assign 3 → condition fails → loop stops

Output: 23

Position:
Show:

Related questions

0 0 votes
0 0 answers
449
449 views
Sahil_Lather asked Jan 28, 2023
449 views
Consider the following C function executed in an OS with paging where the page size is 4 kilobytes. Further, assume that the system employs a 32-entry direct mapped TLB.i...
0 0 votes
0 0 answers
359
359 views
0 0 votes
2 2 answers
1.1k
1.1k views
Shantnu Gupta asked Dec 24, 2017
1,051 views
Consider the following program segmentint main ( ){char ∗ str = “GATECS”;printf (“%d”, madeeasy (str));return 0;}int madeeasy (int ∗ p1){int ∗ p2 = p1;while (∗++p1);retur...
1 1 vote
2 2 answers
2.5k
2.5k views
Shubhanshu asked Jun 10, 2017
2,545 views
#include <stdio.h void my_toUpper(char* str, int index){ *(str + index) &= ~32;} int main(){ char* arr = "gatesquiz"; my_toUpper(arr, 0); my_toUpper(arr,...