C structure declaration in separate header file

Status
Not open for further replies.

praveenkumar450

Newbie level 5
Joined
Jul 2, 2014
Messages
9
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Location
Hyderabad, Andhra Pradesh, India
Activity points
55
C program for to store and access the student id, name and percentage using structures
Code:
#include <stdio.h>
#include <string.h>
struct student
{
	int id;
	char name[20];
	float percentage;
};
int main()
{
	struct student record={0};

	record.id = 450;
	strcpy(record.name,"praveen");
	record.percentage = 75.13;

	printf("ID of the Student is		: %d\n",record.id);
	printf("\nName of the student is		: %s\n",record.name);
	printf("\nPercentage of the studnt is	: %.2f",record.percentage);
	return 0;
}

NOW THE QUESTION IS WE CAN OR WE CAN'T DECLARE THE STRUCTURE.C LIKE STRUCTURE HEADER FILE LIKE #INCLUDE "STRUCTURE.H"

Code:
#include <stdio.h>
#include <string.h>
#include "structure.h"
int main()
{
	record.id = 450;
	strcpy(record.name,"praveen");
	record.percentage = 75.13;

	printf("ID of the Student is		: %d\n",record.id);
	printf("\nName of the student is		: %s\n",record.name);
	printf("\nPercentage of the studnt is	: %.2f",record.percentage);
	return 0;
}

IS IT EXECUTE OR NOT
 
Last edited by a moderator:

Yes you can because the contents of a header file are simple inserted verbatim where the #include statement appears in the main program file.

Bear in mind that you only have a single entry structure available to you. If you want to add other people to the structure you should dimension it, something like:
struct student record[10];
to allocate 10 copies of the structure.

Brian.
 
but problem is it's not executing using #include "structure" can you give and example by a program struct student record[5] members at output i want to see the optional member what i entered out of 5 members details
 
Last edited by a moderator:

you structure.h should look like
Code:
struct student
{
	int id;
	char name[20];
	float percentage;
};
and mian.c
Code:
#include <stdio.h>
#include <string.h>
#include "structure.h"
int main()
{
    	struct student record={0};
	record.id = 450;
	strcpy(record.name,"praveen");
	record.percentage = 75.13;

	printf("ID of the Student is		: %d\n",record.id);
	printf("\nName of the student is		: %s\n",record.name);
	printf("\nPercentage of the studnt is	: %.2f",record.percentage);
	return 0;
}
and it will include stucture.h when compiling main.c
 

In the file structure.h if you are having a
Code:
struct student
{
	int id;
	char name[20];
	float percentage;
}st;
in main.c do this
Code:
#include"structure.h"
st studentfile;   // call and rename the sturcture as you like 

int main()
{
      studentfile.id = 1; // apply as you like
}
 

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…