When a typedef is used in C, will it create a new type or just a type name?

#include <stdio.h>
#include <string.h>

typedef struct Books {
     char title[50];
     char author[50];
     char subject[100];
     int book_id;
} Book;

int main( ) {

  Book book;

  strcpy( book.title, "C Programming");
  strcpy( book.author, "Nuha Ali"); 
  strcpy( book.subject, "C Programming Tutorial");
  book.book_id = 6495407;

  printf( "Book title : %s\n", book.title);
  printf( "Book author : %s\n", book.author);
  printf( "Book subject : %s\n", book.subject);
  printf( "Book book_id : %d\n", book.book_id);

  return 0;
}

Is the Book a new data type here in this example, or is it just an alternative name for the structure?

or, in other words, if the code: typedef unsigned char newDType; newDTypeis a new data type or an alternative name unsigned char?

+4
source share
4 answers

From typedefthe msdn specifier page (click to learn more)

Declaration A typedefintroduces a name that, within its scope, becomes a synonym for the type indicated by the declaration part of the type declaration.


, , - () . , typedef , .

+1

- ISO C. N1570 - . 6.7.8 3 :

typedef , .

C , , Kernighan Ritchie (K & R1), 1978 .

+6

, typedef , .

cppreference:

typedef , , ,
[...]
typedef ,

, , , , .

+3

, typedef . .

+2

Source: https://habr.com/ru/post/1660540/


All Articles