Is typedef a user-defined data type?

Debojit Acharjee 455 Reputation points
2023-06-13T03:02:08.4733333+00:00

I came to know that enum, union and struct are user-defined data type but in some articles type-def is also mentioned as user-defined data type. So which is true?

Developer technologies | C++
Developer technologies | Visual Studio | Other
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Minxin Yu 13,506 Reputation points Microsoft External Staff
    2023-06-13T06:11:01.13+00:00

    Hi,

    typedef allows you to give a new name to user-defined data type.

    typedef is used to improve code readability.

    #include <stdio.h>
    typedef struct Sample {
        char name[50];
        int id;
    } typedef_Sample;
    int main()
    {
        typedef_Sample sp;
        strcpy(sp.name, "hello");
        sp.id = 10;
        printf("%s,%d", sp.name, sp.id);
        return 0;
    }
    

    Best regards,

    Minxin Yu


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.