define structure in c code example
Example 1: struct in C
#include<stdio.h>
#include<string.h>
struct Student{
long int rollNo;
int age;
char name[50];
int totalMarks;
};
int main(){
struct Student s1;
s1.rollNo = 1232234643;
s1.age = 10;
char s[]="Jonathan";
strcpy(s1.name,s);
s1.totalMarks = 450;
printf("RollNumber: %d \nName: %s \nTotal Marks: %d",s1.rollNo,s1.name,s1.totalMarks);
return 0;
}
Example 2: structs in c
#include <stdio.h>
#include <stdlib.h>
struct book{
char name[50];
char author[50];
char ISBN[11];
};
int main(){
struct book book1;
strcpy(book1.name, "james and the giant tatti");
strcpy(book1.author, "Krishan Grewal");
strcpy(book1.ISBN, "12345678987");
printf("book name: %s\n", book1.name);
printf("book author: %s\n", book1.author);
printf("book ISBN: %s\n", book1.ISBN);
return 0;
}
Example 3: C structure
struct struct1 {
int a;
char b;
}
typedef struct struct2 {
int a;
char b;
} name;
void main()
{
struct struct1 var1;
name var2;
var1.a = 10;
var2.b = 'c';
}
Example 4: declare structure in c
struct num{
int a ;
int b;
};
int main()
{
struct num n;
n.a=10;
n.b=20;
}
Example 5: Structure of Structs in c
The basic structure
Example 6: structure and function in c
#include <stdio.h>
#include <string.h>
struct student
{
int id;
char name[20];
float percentage;
};
struct student record;
void structure_demo();
int main()
{
record.id=1;
strcpy(record.name, "Raju");
record.percentage = 86.5;
structure_demo();
return 0;
}
void structure_demo()
{
printf(" Id is: %d \n", record.id);
printf(" Name is: %s \n", record.name);
printf(" Percentage is: %f \n", record.percentage);
}