fork download
  1. /******************************************************************************
  2.  
  3. Welcome to GDB Online.
  4. GDB online is an online compiler and debugger tool for C/C++.
  5. Code, Compile, Run and Debug online from anywhere in world.
  6.  
  7. *******************************************************************************/
  8. #define SUCCESS 0
  9. #define BUF_SIZE 64
  10. #define HPY_BTH_MSG "I, %s, wish you a happy birthday, %s!!!\n"
  11. #define SPECIAL_PERSON "Tage"
  12. #define GUESTS { "Eduardo", "Marcos", "Matheus", "Breno", "Romoff", "Rafa", "Barizon" }
  13.  
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <string.h>
  17.  
  18. typedef struct guest Guest;
  19. Guest* invite(char* guest_name, Guest* guest_list);
  20. void wish_happy_birthday(Guest* guest_list, char* special_person);
  21. void cleanup_mess(Guest* guest_list);
  22.  
  23. int main(void)
  24. {
  25. char special_person[BUF_SIZE] = SPECIAL_PERSON;
  26. char* guests[] = GUESTS;
  27. int guest_num = sizeof(guests) / sizeof(guests[0]);
  28. Guest* guest_list = NULL;
  29.  
  30. for (int i = 0; i < guest_num; i++)
  31. guest_list = invite(guests[i], guest_list);
  32.  
  33. wish_happy_birthday(guest_list, special_person);
  34. cleanup_mess(guest_list);
  35.  
  36. return SUCCESS;
  37. }
  38.  
  39. struct guest
  40. {
  41. char name[BUF_SIZE];
  42. struct guest* next;
  43. };
  44.  
  45. Guest* invite(char* guest_name, Guest* guest_list)
  46. {
  47. Guest* guest = (Guest*)malloc(sizeof(Guest));
  48. strcpy(guest->name, guest_name);
  49. guest->next = guest_list;
  50. return guest;
  51. }
  52.  
  53. void wish_happy_birthday(Guest* guest_list, char* special_person)
  54. {
  55. while (guest_list != NULL)
  56. {
  57. printf(HPY_BTH_MSG, guest_list->name, special_person);
  58. guest_list = guest_list->next;
  59. }
  60. }
  61.  
  62. void cleanup_mess(Guest* guest_list)
  63. {
  64. Guest* temp;
  65. while (guest_list != NULL)
  66. {
  67. temp = guest_list;
  68. guest_list = guest_list->next;
  69. free(temp);
  70. }
  71. }
  72.  
Success #stdin #stdout 0.01s 5320KB
stdin
45
stdout
I, Barizon, wish you a happy birthday, Tage!!!
I, Rafa, wish you a happy birthday, Tage!!!
I, Romoff, wish you a happy birthday, Tage!!!
I, Breno, wish you a happy birthday, Tage!!!
I, Matheus, wish you a happy birthday, Tage!!!
I, Marcos, wish you a happy birthday, Tage!!!
I, Eduardo, wish you a happy birthday, Tage!!!