C Programming - Typedef - Discussion

Discussion Forum : Typedef - Find Output of Program (Q.No. 2)
2.
What will be the output of the program?
#include<stdio.h>

int main()
{
    typedef int arr[5];
    arr iarr = {1, 2, 3, 4, 5};
    int i;
    for(i=0; i<4; i++)
        printf("%d,", iarr[i]);
    return 0;
}
1, 2, 3, 4
1, 2, 3, 4, 5
No output
Error: Cannot use typedef with an array
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
20 comments Page 1 of 2.

Ashok said:   7 years ago
How can it's possible because we provide just alias to the array right from 0 to 5?

EauDeSource said:   10 years ago
a ', ' has been forgot on the printf display. So I choosed option with the typedef error.

Sam said:   1 decade ago
int main()
{
typedef int arr[5];/*it only creates alias name to array name */
arr iarr = {1, 2, 3, 4, 5};
int i;
for(i=0; i<4; i++)
printf("%d,", iarr[i]);
return 0;
}

Sana said:   1 decade ago
"typedef int arr[5];" can be written as:

typdef int[5] arr;

So, "arr iarr = {1, 2, 3, 4, 5};" can be thought of as:

int[5] iarr = {1, 2, 3, 4, 5};
(1)

Chandra vijay said:   1 decade ago
I am asking that int is typedefed in arr[5] then how can we use only arr in place of int?

Pushpak said:   1 decade ago
The typedef keyword allows the programmer to create new names for types such as int or, more commonly in C++, templated types--it literally stands for "type definition". Typedefs can be used both to provide more clarity to your code and to make it easier to make changes to the underlying data types that you use.

Hate raho said:   1 decade ago
I think "iarr" means the previously created datatype arr's variable. Here iarr is working as a variable of iarr datatype, which is actually integer type of data type.
(1)

Sagar hudge said:   1 decade ago
How it is 1, 2, 3, 4 ?

Tanmay said:   1 decade ago
How it is ending at 3?

Aaa said:   1 decade ago
At for loop it starts from 0 and ends at 3.
So from index 0-3, The no's available are 1, 2, 3, 4.


Post your comments here:

Your comments will be displayed after verification.