I'm still learning C and I understand that to get rid of most implicit declaration warnings, you add the prototype header at the beginning. But I'm confused as to what you do when you have outside methods being used in your code.
This is my code when I'm using the outside methods
#include <stdio.h>
#include <string.h>
int main(void)
{
int arrayCapacity = 10;
int maxCmdLength = 20;
int A[arrayCapacity];
int count = 0; /* how many ints stored in array A */
char command[maxCmdLength + 1];
int n;
while (scanf("%s", command) != EOF)
{
if (strcmp(command, "insert") == 0)
{
scanf("%d", &n);
insert (n, A, arrayCapacity, &count);
printArray(A, arrayCapacity, count);
}
else if (strcmp(command, "delete") == 0)
{
scanf("%d", &n);
delete(n,A,&count);
printArray(A, arrayCapacity, count);
}
else
{
scanf("%d", &n);
printArray(A, arrayCapacity, count);
}
}
return 0;
}
The methods printArray, insert, and delete are all in the form of: printArray.o, insert.o, delete.o
This is how I compiled my program: gcc -Wall insert.o delete.o printArray.o q1.c
and I get these warnings:
q1.c: In function âmainâ:
q1.c:20: warning: implicit declaration of function `insert'
q1.c:21: warning: implicit declaration of function `printArray'
q1.c:30: warning: implicit declaration of function `delete'
I've tried including this in headers but I get errors saying file or directory not found.
Any help appreciated.
Put them in a header file foo.h like so:
extern void printArray(int *A, int capacity, int count);
...
then include that file in your source
#include "foo.h"
You need to include the correct headers to get rid of such warnings.
If you get a "file not found" error, try to include them as
#include "myheader.h"
and put your header files in the same directory as your source code.
Generally speaking, #include "file" is for programmer-defined headers while #include <file> is or standard headers.
You should be able to just put in the function prototype at the top of the file like you do for other functions in the same file. The linker should take care of the rest.
Where did you get those .o files from? If you have written them yourself, then you should create the corresponding .h files. If you got these files from somewhere else, then you should search for the headers in the same place.
If all called functions are written before the main() function the compiler will know their name, return type and parameter signature and can match all three of these properties with each following function invocation.
Some programmers like to write a function signature first, and do the implementation at a later time.
The only time a function declaration is essential is when using co-routines: functionA invokes functionB which in turn invokes functionA.
Done as follows:
type a(...signatureOfA...)
/* compiler now knows about a() */
type b(...signatureOfB...)
{…
// implementation of b
a(…arguments for a…);
/* compiler knows about above */
…}
type a(...signatureOfA...)i
{…
// implementation of a
b(…arguments for b…);
/* compiler knows about above */
…}
int main()
{
a(… arguments for a…);
/* compiler knows */
return(code);
}
Related
This is a header file
#include <stdio.h>
int m = 18;
int x = 4;
int singles (n) {
if (n == 1)
return 0;
return doubles(n-1);
}
int doubles (n) {
if (n == 1)
return 0;
return triples(n-1);
}
int triples (n) {
if (n == 1)
return m;
return (singles(n-1) + doubles (n-1) + triples (n-1))*(m-1);
}
and this is the main file
#include <stdio.h>
#include "test.h"
int main () {
printf("%d",singles (x));
}
So this is pretty complicated for me at-least.The idea is that in the main function i will call singles(x) where x =4 so its more like singles (4),it will call doubles (3),that will call triples (2),that will call all of singles(1) which will return 0,doubles (1) that returns 0 and triples (1) that will return m.
So the error i am getting is
./test.h:13:12: warning: implicit declaration of function 'doubles' is invalid
in C99 [-Wimplicit-function-declaration]
return doubles(n-1);
^
./test.h:20:12: warning: implicit declaration of function 'triples' is invalid
in C99 [-Wimplicit-function-declaration]
return triples(n-1);
^
2 warnings generated.
I tried to create a header file .h with the first script and then made a second .c script that i try to compile that won't work.I tried importing the header to try to avoid this error but it doesn't seem to work.
Thanks a lot
Inside of singles, you're using doubles before it's defined. Similarly in doubles, you're using triples before it's defined. That's why you're getting the implicit declaration errors.
Also, you're not defining the type of the n parameter to any of these functions.
You need to specify function prototypes, which declare the function without defining it:
int singles(int n);
int doubles(int n);
int triples(int n);
Also, you shouldn't define functions in a header file. If you include this header in multiple .c files and then link them together, you'll get an error because you'll have multiple definitions of those functions.
Take all of the function definitions and put them in test.c. Then in test.h, put only the prototypes above. Then you can compile everything as follows:
gcc -c test.c
gcc -c main.c
gcc -o main main.o test.o
Or in a single line:
gcc -o main test.c main.c
The term "implicit declaration" in an error message is usually generated when the compiler sees the implementation of a call to a function before the declaration (prototype).
For example, you should have:
header file
int singles(int x);
int doubles(int x);
int triples(int x);
In your source file:
#include "header_file.h"
Also, in your function definitions (implementations) you need to specify the type of the argument:
int singles (/* data type */ parameter_name)
{
//...
}
Edit 1: Changed implementation to function call per #John Bollinger.
Scenario :
A C application created in Netbeans IDE with below two files:
some_function.c
#include <stdio.h>
int function_1(int a, int b){
printf("Entered Value is = %d & %d\n",a,b);
return 0;
}
newmain.c
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char** argv) {
//function_2(); //Error //function name not allowed
function_1();
function_1(1);
function_1(1,2);
return (EXIT_SUCCESS);
}
When learning the need of the header file in a C program, I tried the above application (as it is). It got compiled and gave the output as below
Entered Value is = 4200800 & 102
Entered Value is = 1 & 102
Entered Value is = 1 & 2
Question 1 : (I realize, in starting stage, to understand the process of the linker program is tough, hence i ask this question.) Is my assumption correct, that when linking, "the linker will check for the function name and not the arguments" in a condition the header file not used?
Regarding the header file usage, I came across this link and there it said as, we can include the C file itself using the #include. So, I used the below line in the file newmain.c
#include "some_function.c"
As expected it shown the below error
error: too few arguments to function 'function_1()'
error: too few arguments to function 'function_1(1)'
And also, I got the below (unexpected) error:
some_function.c:8: multiple definition of `function_1'
some_function.c:8: first defined here
Question 2: What error I did when including the 'c' file itself, as it gives the above said (unexpected) error?
You are probably using a pre-C99 dialect of C, which has "implicit function declarations". This means that functions without declarations are taken to have this kind of signature:
int function_1();
i.e. returning an int and accepting any number of arguments of any type. When you pass an argument list that is incompatible with your function definition, you invoke undefined behaviour at runtime.
Concerning the multiple definition errors, think of it. Each translation unit you include some_function.c will have its own definition of the function. It is as if you had written that definition in every other .c file. C doesn't allow multiple definitions in a program/library.
I (the questioner) post this answer for a quick understanding for some C programming starters. This answer inspired from the answers by #juanchopanza & #Sam Protsenko which are in this post.
Question 1 :Implicit function declarations in C
Question 2:
When using the below line
#include "some_function.c"
The result application will change like below after the preprocessor activity
some_function.c
#include <stdio.h>
int function_1(int a, int b){
printf("Entered Value is = %d & %d\n",a,b);
return 0;
}
newmain.c
#include <stdio.h>
#include <stdlib.h>
/*"#include "some_function.c"" replace begin by preprocessor*/
#include <stdio.h>
int function_1(int a, int b){
printf("Entered Value is = %d & %d\n",a,b);
return 0;
}
/*"#include "some_function.c"" replace end by preprocessor*/
int main(int argc, char** argv) {
//function_2(); //Error //function name not allowed
//function_1(); //Error
//function_1(1); //Error
function_1(1,2);
return (EXIT_SUCCESS);
}
Note : In the above two file, the function function_1 is there in two places
So now the below error is meaningful
some_function.c:8: multiple definition of `function_1'
some_function.c:8: first defined here
I am a rookie programmer here. I am trying to learn C. I am trying to have a main program run another file. However, I am getting compile time errors. My IDE says: Error. Implicit declaration of function print and also it says ROW and COL are not defined here. Of course I have 3 files and I don't see why I am getting these errors. I think I defined ROW and COL properly in the header file like you're supposed to. Can someone see a problem with this simple code? Right now I have the following code:
//p750_eightqueens.h
#ifndef P750_EIGHTQUEENS_H_INCLUDED
#define P750_EIGHTQUEENS_H_INCLUDED
#define ROW 8
#define COL 8
void go(int row, int col);
void print(int array[ROW][COL]);
#endif // P750_EIGHTQUEENS_H_INCLUDED
//p750_eightqueens.c
void go(int row, int col) {
int a[ROW][COL],i,j;
for(i=0;i<ROW;i++) {
for(j=0;j<COL;j++){
a[i][j]=(i==row&&j==col?1:0);
}
}
print(a);
}
void print(int array[ROW][COL]) {
int i,j;
for(i=0;i<ROW;i++) {
for(j=0;j<COL;j++){
printf("%i",a[i][j]);
if (j!=COL-1) printf(" ");
}
printf("\n");
}
}
//now in main.c
#include<stdio.h>
#include<stdlib.h>
#include "p750_eightqueens.h"
int main(){
go(4,4);
return 0;
}
You need to include your header file in p750_eightqueens.c, too.
Your .c files are compiled independently of each other, so your p750_eightqueens.c doesn't know about your defines and function declarations.
If you compile a C-program each compilation unit (.c-file) is compiled on it's own and needs all includes and declarations that are used in that file. After that you have object files (.o) which are then combined to a single executable by the linker. The linker searchs for the implementations of the used functions and puts it together so that different compilation units can call functions in others.
I'm getting an error when I compile my main.c
The error says:
functions.c:27:6: warning: conflicting types for 'secondfunction' [enabled by default]
functions.c:23:2: note previous implicit declaration of 'secondfunction' was here
(My program has a file called main.c, header.h, and functions.c)
main.c:
#include "header.h" /*Includes the header file*/
int main(void) /*Starts the main function.*/
{
course();
firstfunction(filename,fileextension,fp,MAX_SIZE);
/*
thirdfunction();
fourthfunction();
fifthfunction();
sixthfunction();
*/
return 0;
} /*Ends the main function.*/
header.h:
#include <stdio.h>
#include <string.h>
#include "functions.c"
#define MAX_SIZE 50
struct records
{
double euid;
char firstname[MAX_SIZE];
char lastname[MAX_SIZE];
float gpa;
};
void course(void);
void firstfunction(char filename[],char fileextension[],FILE* fp,int SIZE);
void secondfunction(FILE* fp);
struct records people;
FILE* fp;
char filename[MAX_SIZE],fileextension[MAX_SIZE];
functions.c:
void course(void)
{
printf("\n\n\n\n\nDepartment: CSCE\nCourse number: 1030\nProgram number: Homework 3\nName: xxxxxxxxxxxxxx\nEUID: xxxxxxxxxxxxx\nEmail: xxxxxxxxx\n\n\n\n\n");
}
void firstfunction(char filename[],char fileextension[],FILE* fp,int SIZE)
{
printf("Please enter the file name (ex:filename)\n"); /*Asks for the name and the extension*/
scanf("%s",filename);
printf("Please enter the file extension (ex:.txt)\n");
scanf("%s",fileextension);
strcat(filename,fileextension); /*Puts them together with strcat*/
printf("%s\n",filename);
fp=fopen(filename,"r");
secondfunction(fp);
fclose(fp);
} /*End of the first function*/
void secondfunction(FILE* fp)
{
} /*End of the second function.*/
Thanks for the help guys!!
I don't think it's the variables being passed to the function I think it's the fact that I'm calling a function from within a function. How do I fix this?
remove this line #include "functions.c" from header.h and include header.h in functions.c
You make use of secondfunction() in functions.c before you declare or define this function.
So compiler treats her as it usually does with unknown functions: assumes it takes variable number of int arguments and returns int.
When it next finds your definition - it yells about differences because your function returns void and takes FILE* as an argument.
Move your secondfunction() definition before firstfunction() and it will work. But what you should really do is read on what to put in header files. In your case put all functions declarations in header file, and problem will be gone, if you include header file on top of your functions.c
You declare both functions in header.h, which is great.
But in functions.c, when you call secondfunction from within firstfunction, no declaration or definition of secondfunction is yet visible.
You should add
#include "header.h"
to the top of functions.c. You should also add include guards to header.h so that you don't run into problems if it's included more than once:
#ifndef H_HEADER
#define H_HEADER
/* contents of header.h */
#endif
And as Mario's answer points out, you shouldn't include functions.c from anywhere. Except in unusual cases, only .h files should be #included.
Each function should be declared in a .h file, and defined in a .c file.
Each file that needs to refer to a given function should have a #include directive for the header that declares it.
(main needn't be declared in a header, since it's not normally called.)
As for the confusing "conflicting types" message, that's because of an obsolescent feature of C. In older versions of the language (prior to the 1999 standard), a call to an undeclared function would result in an implicit declaration of that function, and the compiler would assume that it returns a result of type int. Even if you're using a compiler that still does this (many still do by default), you should ensure that each function you call has a visible declaration.
For example: If I have two .h files
process1.h and process2.h
and they contain two function with different output variables.
process1.h:
function(int var)
{
return 2*var;
}
process2.h:
function(int var)
{
return 10*var;
}
Can this be done in main.c:
int main()
{
int a = 2;
#include "process1.h"
printf("%d",function(a)); //output is 4
EXCLUDE #INCLUDE "process1.h" ????? <----can this be done in any way??
#include "process2.h"
printf("%d",function(a)); //output is 20
}
No, you cannot "un-include" a file. Think of all the preprocessor directives (lines starting with #) as happening before the actual C compiler even sees the source file. They just operate on the text of the file, and the preprocessor could be implemented as a separate step that just feeds new text into the actual compiler.
The best way to modify the actions of an include depending on the caller is to use further macros inside the included files, that you can #define before including them.
Still, your overall syntax is off, you can't (typically) nest functions in C.
No, and you should not try to write a program with two functions of the same name.
In the special case that the functions are actually defined in the header file (instead of just prototypes), you can do this:
#define function function_file1
#include "file1.h"
#undef function
#define function function_file2
#include "file2.h"
#undef function
int
main (void)
{
int a = 2;
printf ("%d\n", function_file1 (a));
printf ("%d\n", function_file2 (a));
}
BUT if you rename a function prototype then you haven't actually renamed the real function, so you'll get undefined symbol error when you link.
In any case, if you have two functions defined with the same name then it won't link anyway, not matter what else you do in the sources. (In C++, it is sometimes possible to define two things with the same name, but the One-Definition-Rule means the linker is allowed to assume they are both the same thing really and just pick one.)
This is why libraries are supposed to use names that won't be used elsewhere - usually by adding a common prefix to all symbol names (e.g. my_unique_lib_initialize()).
Why not use array of function pointers. Sure you need to initialize it at the start but I think it probably solves what you want to do.
int process1_function(int var);
int process2_function(int var);
int main(void)
{
int i, a = 10;
int (* functions[2])(int);
functions[0] = process1_function;
functions[1] = process2_function;
for(i=0; i < 2; i++)
{
printf("%d", (functions[i])(a));
}
return 0;
}
If you do not need to dynamically change which function you're going to call you can also just prefix the functions:
int process1_function(int var);
int process2_function(int var);
int main(void)
{
printf("%d",process1_function(a));
printf("%d",process2_function(a));
return 0;
}