fork download
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<string.h>
  4.  
  5. enum days {mon, tue, wed, thu, fri, sat, sun};
  6.  
  7. typedef enum days days_t;
  8.  
  9. struct personal_info {
  10. char name[32];
  11. int age;
  12. char *addr;
  13. };
  14.  
  15. // typedef a previously defined struct.
  16. typedef struct personal_info person;
  17.  
  18. // declare a nameless struct and typedef it right away.
  19. typedef struct {
  20. int x;
  21. int y;
  22. } pair;
  23.  
  24.  
  25. // can typedef primitive types too
  26. typedef int my_int;
  27.  
  28.  
  29.  
  30. int main()
  31. {
  32. days_t d; // just a shorter way of declaring "enum days d"
  33. person david; // just a shorter way of declaring "struct personal_info david"
  34. pair p;
  35. my_int x;
  36.  
  37. d = mon;
  38.  
  39. strcpy(david.name, "john");
  40. david.age = 20;
  41.  
  42. p.x = 5;
  43. p.y = 7;
  44.  
  45. x = 42;
  46.  
  47. printf("Size of 'person' struct is %ld\n", sizeof(person));
  48. printf("David's name is %s\n", david.name);
  49. printf("The pair 'p' is (%d, %d)\n", p.x, p.y);
  50. printf("'x' is %d\n", x);
  51.  
  52. return 0;
  53. }
  54.  
Success #stdin #stdout 0s 5300KB
stdin
Standard input is empty
stdout
Size of 'person' struct is 48
David's name is john
The pair 'p' is (5, 7)
'x' is 42