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.

Jayadev( dilu ) said:   1 decade ago
Typedef names can be used to improve code readability.
You can declare any type with typedef, including pointer, function, and array types. You can declare a typedef name for a pointer to a structure or union type before you define the structure or union type, as long as the definition has the same visibility as the declaration.

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.

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;
}

Sathya said:   1 decade ago
Here "arr" is alias to "int" in first line.

Declaration of an array is int iarr[5] = {1, 2, 3, 4, 5};.

But here arr is alias to int so arr iarr[5] = {1, 2, 3, 4, 5};.

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)

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)

Cherry said:   1 decade ago
typedef is used to create new data type. But it is commonly used to change existing data type with another name.

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.

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

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?


Post your comments here:

Your comments will be displayed after verification.