Varun U
Varun U

Reputation: 157

Accessing structures from different files in C

I have the following C code

File Name: abc.c

#include<stdio.h>

struct abc{
int xxx;
float yyy;
};

I need to access the above struct in another file called def.c

Could anybody explain me how i can achieve this?

Thanks!

Upvotes: 2

Views: 2014

Answers (2)

ganesh borkar
ganesh borkar

Reputation: 3

//myheader.h

struct abc{

int xxx;

float yyy;

}obj;
//demo.c
# include"myheader.h"

# include"stdio.h"

int main(){

printf("\nENTER TWO VALUES : ");

scanf("%d %f",&obj.xxx,&obj.yyy);

printf("\nVALUES YOU ENTERED ARE : %d %f\n",obj.xxx,obj.yyy);

return 0;

}

Upvotes: -1

Kishore
Kishore

Reputation: 839

// mystructs.h
struct abc{
    int xxx;
    float yyy;
};


//abc.c
#include "mystructs.h"
struct abc var1;


//another_file.c
#include "mystructs.h"
struct abc var2;

Upvotes: 7

Related Questions