structure in c declare 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;
    //accessing members of the struct
    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: structure and function in c

#include <stdio.h>
#include <string.h>
 
struct student 
{
           int id;
           char name[20];
           float percentage;
};
 
void func(struct student *record);
 
int main() 
{
          struct student record;
 
          record.id=1;
          strcpy(record.name, "Raju");
          record.percentage = 86.5;
 
          func(&record);
          return 0;
}
 
void func(struct student *record)
{
          printf(" Id is: %d \n", record->id);
          printf(" Name is: %s \n", record->name);
          printf(" Percentage is: %f \n", record->percentage);
}

Example 3: declaration in c

#include<stdio.h>
#include<conio.h>
void main()
{
//variable declaration
	int a;
  //assinging value to a variable;
    a=10;
}

Tags:

Misc Example