C examples for Structure:Structure Value
Read data from console and fill to a structure
#include <stdio.h> #include <string.h> char * s_gets(char * st, int n); #define MAXTITL 41 /* maximum length of title + 1 */ #define MAXAUTL 31 /* maximum length of author's name + 1 */ struct book { /* structure template: tag is book */ char title[MAXTITL]; char author[MAXAUTL]; float value;// ww w . ja v a 2s.c o m }; /* end of structure template */ int main(void){ struct book my; /* declare my as a book variable */ printf("book title:\n"); s_gets(my.title, MAXTITL); /* access to the title portion */ printf("author:\n"); s_gets(my.author, MAXAUTL); printf("Now enter the value.\n"); scanf("%f", &my.value); printf("%s by %s: $%.2f\n",my.title, my.author, my.value); printf("%s: \"%s\" ($%.2f)\n", my.author, my.title, my.value); printf("Done.\n"); return 0; } char * s_gets(char * st, int n){ char * ret_val; char * find; ret_val = fgets(st, n, stdin); if (ret_val){ find = strchr(st, '\n'); // look for newline if (find) // if the address is not NULL, *find = '\0'; // place a null character there else while (getchar() != '\n') continue; // dispose of rest of line } return ret_val; }