C Programming - Expressions - Discussion

Discussion Forum : Expressions - Find Output of Program (Q.No. 5)
5.
What will be the output of the program?
#include<stdio.h>
int main()
{
    static int a[20];
    int i = 0;
    a[i] = i  ;
    printf("%d, %d, %d\n", a[0], a[1], i);
    return 0;
}
1, 0, 1
1, 1, 1
0, 0, 0
0, 1, 0
Answer: Option
Explanation:

Step 1: static int a[20]; here variable a is declared as an integer type and static. If a variable is declared as static and it will be automatically initialized to value '0'(zero).

Step 2: int i = 0; here vaiable i is declared as an integer type and initialized to '0'(zero).
Step 3: a[i] = i ; becomes a[0] = 0;
Step 4: printf("%d, %d, %d\n", a[0], a[1], i);
Here a[0] = 0, a[1] = 0(because all staic variables are initialized to '0') and i = 0.
Step 4: Hence the output is "0, 0, 0".

Discussion:
10 comments Page 1 of 1.

Vardhan said:   2 decades ago
It could be explained some what clearly.
(1)

Abhioxic said:   9 years ago
I think, we don't need to declare it as static, static or not, it will still be initialized to 0.
(1)

Annn said:   1 decade ago
Arrays cannot be initialized dynamically ?

Anumita singha roy said:   1 decade ago
Arrays can be initialized dynamically, we can use scanf to initialize an array dynamically.

SUSHMA said:   9 years ago
Can anyone explain about the static variable declaration from all another type of explanation in brief?

Raj said:   9 years ago
#include<stdio.h>
int main()
{
int i=-3, j=2, k=0, m;
m = ++i && ++j && ++k;
printf("%d, %d, %d, %d\n", i, j, k, m);
return 0;
}
Can anyone explain it?

Suraj Jaiswal said:   9 years ago
@Raj,

OUTPUT: 4, 3, 1, 1.

Here i = 4, j = 3, k = 1 because of pre-increment operator in i, j, k(++i, ++j, ++k). It changes the value before execution and increment by 1.
So, m = 4 && 3 && ++1;
Here, 431 are not equal to zero. So it becomes TRUE means 1, In AND operation if all TRUES Then result would be TRUE.
m = TRUE && TRUE && TRUE.
m = 1 && 1 && 1.
m = 1.

Harry_hari said:   8 years ago
#include <stdio.h>
int main() {
int i_ = 2, *j_, k_;
j_ = &i_;
printf("%d\n", i_**j_*i_+*j_);
return 0;
}

What will be the output?

Saila said:   6 years ago
@Suraj.

The output is -2,3,1,1 because given int i value is -3 not only 3.

Parmeshw89591 said:   6 years ago
@Raj.

2, 3, 1, 1.

Post your comments here:

Your comments will be displayed after verification.