Kunal Sharma
Kunal Sharma

Reputation: 426

How to fix warning occured during compiling of C program on fedora

I am trying to learn how to split a c program in more than one file but during compilation it throws warning as given below:

$ gcc ./p1.c ./p2.c -o ./p1
./p2.c:2:1: warning: data definition has no type or storage class
    2 | b = 6;
      | ^
./p2.c:2:1: warning: type defaults to ‘int’ in declaration of ‘b’ [-Wimplicit-int]

where, p1.c

#include <stdio.h>
#include "p2.h"
int a = 5;

int main(void) {
    printf("%d\n", a);
    printf("%d\n", b);
    printf("%d\n", square(b));
    return 0;
}

p2.c

#include "./p2.h"
b = 6;
int square(int x) {
    return x * x;
}

and p2.h

#ifndef P2_H
#define P2_H

extern int b;
int square(int);

#endif

all of these files are present in same directory but i am still getting the warning, after trying several searching on the internet i can't find how to fix it.

Thank You In Advance.

Upvotes: 0

Views: 63

Answers (1)

hyde
hyde

Reputation: 62838

p2.c should have a normal definition of the global variable:

#include "./p2.h"
int b = 6;
int square(int x) {
    return x * x;
}

Upvotes: 1

Related Questions