What will be the output of the following C code?void main() { int a[] = {1,2,3,4,5}, *p; p = a; ++*p; printf("%d ", *p);p += 2; printf("%d ", *p);}a.24b.22c.23d.34
Question
What will be the output of the following C code?void main() { int a[] = {1,2,3,4,5}, *p; p = a; ++*p; printf("%d ", *p);p += 2; printf("%d ", *p);}a.24b.22c.23d.34
Solution 1
To determine the output of the given code, let's analyze it step by step:
- Declare an integer array
awith values {1, 2, 3, 4, 5}. - Declare a pointer variable
p. - Assign the address of the first element of array
ato pointerp. - Increment the value pointed by
pby 1. This means the value ata[0]will become 2. - Print the value pointed by
p, which is 2. - Increment the pointer
pby 2 positions. This meanspwill now point toa[2]. - Print the value pointed by
p, which is 3.
Therefore, the output of the given C code will be "2 3".
Answer: c. 23
Solution 2
To determine the output of the given code, let's analyze it step by step:
- Declare an integer array
awith values {1, 2, 3, 4, 5}. - Declare a pointer variable
p. - Assign the address of the first element of array
ato pointerp. - Increment the value pointed by
pby 1. This means the value ata[0]will become 2. - Print the value pointed by
p, which is 2. - Increment the pointer
pby 2 positions. This meanspwill now point toa[2]. - Print the value pointed by
p, which is 3.
Therefore, the output of the given C code will be "2 3".
Answer: c. 23
Similar Questions
What will be the output of the following code?int main() { int a = 10; int *p = &a; *p = 20; printf("%d\n", a); return 0;}
What will be the output of the following C code?#include <stdio.h>void foo(int*);int main(){int i = 10;foo((&i)++);}void foo(int *p){printf("%d\n", *p);}
What will the following code output?#include <stdio.h>int main() { int arr[3] = {1, 2, 3}; int *p = arr; printf("%d\n", *(p + 2)); return 0;}
.What will be the output of the following C code?int main() {int arr[] = {1, 2, 3, 4, 5};printf("%d", arr[3]);return 0;}*1 point1234
What will be the output of the following C program?#include<stdio.h>int main(){ int a[5] = {5, 3, 15, 20, 25}; int i, j, m; i = ++a[1]; j = a[1]++; m = a[i++]; printf("%d, %d, %d", i, j, m); return 0;}
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.