Structs in C
You,•C Programming
/* Online C Compiler and Editor */
#include <stdio.h>
#include <string.h>
//a struct holds multiple data togather
struct twothings
{
char name[32];
int id;
};
//using a typedef, we can stop using the word struct
//everytime we declare a struct
typedef struct twothings tt;
//notice that this function returns 'one' variable of type struct
//but within the struct , there are two variables (the name and int)
//struct thus solves the problem of a function returning multiple values
//there are more things that structs can solve
tt getfavouritestudent()
{
tt rv;
strcpy(rv.name, "varu gal");
rv.id = 777;
return rv;
}
int main()
{
printf("Hello, World!\n");
tt x = getfavouritestudent();
printf("name: %s id : %d ", x.name,x.id);
return 0;
}
Output: