I know alot of similar questions were asked before but i couldn't find something that would fix this warning i get:
MyIntFunctions.c:19:2: warning: implicit declaration of function ‘printf’ [-Wimplicit-function-declaration]
Occurs here:
void IntPrint (const void *key)
{
printf("%d", *(int*)key); // line 19
printf("\t-->\t");
}
and a similar warning:
MyStringFunctions.c:22:2: warning: implicit declaration of function ‘printf’ [-Wimplicit-function-declaration]
void StringPrint (const void *key)
{
printf("%s",(char*)key); //line 22
printf("\t-->\t");
}
I really want to understand what is wrong so i won't do that again in the future.
You need to include the appropriate header
#include <stdio.h>
If you're not sure which header a standard function is defined in, the function's man page will state this.
You need to include a declaration of the printf() function.
#include <stdio.h>
the warning or error of kind IMPLICIT DECLARATION is that the compiler is expecting a Function Declaration/Prototype..
It might either be a header file or your own function Declaration..
warning: incompatible implicit declaration of built-in function 'printf'
warning: incompatible implicit declaration of built-in function 'scanf'
the above warnings of compiler says that there is need to include declaration of printf and scanf i.e. include appropriate header
#include <stdio.h>
Related
Here's my code:
int main(){
printf("Hi");
int i=10;
printf("Hi %d",i);
return 0;
}
Now as C can implicitly declare a function this program will compile correctly (As it does with gcc).
But my question is, wasn't the 1st printf declared to return an int with 1 parameter of type char * ?
That makes the 2nd printf an error.
Yet the program compiles with no errors, just warnings (gcc). Why ?
Strictly speaking, implicit declaration is standard violation. It is removed from the standard.
Quoting C11, Foreword
Major changes in the second edition included:
— remove implicit function declaration
That said, in earlier version of C, for a function which is considered declared implicitly (i.e., used before the compiler has knowledge about the function prototype) was supposed to
return an int
accepts any number and type of parameter.
So, as long as the function declaration and the definition does not collide (say, return type mismatch), you'll not get any error. However, a strictly conforming compiler MUST produce a diagnostic.
In your case printf is implicitly defined to be int printf(...), not int printf(char *), thus compiler detects no errors when you call it with different arguments.
Your code is not portable C since you're missing the requisite #include which brings in the function prototype for printf: any sort of implicit declaration was removed in C99.
If you want to write non-portable C, then the best thing you can do is to consult your compiler documentation. In this instance it looks like your friendly compiler is defaulting to int printf(...).
gcc unfortunately has some built in notion of printf even though a header file has not been used.
unsigned int fun ( unsigned int x )
{
printf("%s\n");
printf("%u\n",x);
more_fun("%s\n");
more_fun("%u\n",x);
return(x+1);
}
so.c: In function ‘fun’:
so.c:5:5: warning: implicit declaration of function ‘printf’ [-Wimplicit-function-declaration]
printf("%s\n");
^
so.c:5:5: warning: incompatible implicit declaration of built-in function ‘printf’
so.c:5:5: note: include ‘<stdio.h>’ or provide a declaration of ‘printf’
so.c:5:12: warning: format ‘%s’ expects a matching ‘char *’ argument [-Wformat=]
printf("%s\n");
^
so.c:7:5: warning: implicit declaration of function ‘more_fun’ [-Wimplicit-function-declaration]
more_fun("%s\n");
^
fortunately if we make our own it does forget at least a little.
void printf(char *, unsigned int );
void more_fun(char *, unsigned int );
unsigned int fun ( unsigned int x )
{
printf("%s\n");
printf("%u\n",x);
more_fun("%s\n");
more_fun("%u\n",x);
return(x+1);
}
so.c:3:6: warning: conflicting types for built-in function ‘printf’
void printf(char *, unsigned int );
^
so.c: In function ‘fun’:
so.c:7:5: error: too few arguments to function ‘printf’
printf("%s\n");
^
so.c:3:6: note: declared here
void printf(char *, unsigned int );
^
so.c:9:5: error: too few arguments to function ‘more_fun’
more_fun("%s\n");
^
so.c:4:6: note: declared here
void more_fun(char *, unsigned int );
^
but we are talking about one compiler, one compiler does not cover it you have to try many/all. Standard or not a compiler could still notice your changing the use of the function and let you know about it, this compiler chooses not to. The error here is not that the compiler didnt notice the function being used differently from one undeclared instance to another but that there was no declaration, and then it does notice the difference as one would hope.
I am trying to pass an entire array into a my function, but I am getting currently getting the error:
test.c: In function 'main':
test.c:4:18: error: expected expression before ']' token
method(myArray[]);
^
test.c: At top level:
test.c:8:6: warning: conflicting types for 'method' [enabled by default]
void method(int arr[]){
^
test.c:4:3: note: previous implicit declaration of 'method' was here
method(myArray[]);
^
test.c: In function 'method':
test.c:9:3: warning: incompatible implicit declaration of built-in function 'printf' [enabled by default]
printf("DATA: %d",arr[2]);
^
This is my code (a simplified version of what I'm trying to do that throws up the same error:
int main(){
int myArray[3];
myArray[2]=12;
method(myArray[]);
}
void method(int arr[]){
printf("DATA: %d",arr[2]);
}
When passing an array to a function, you don't need the [] after it. Just using the name of the array is sufficient.
Also, you need to either define or declare your functions before they're used, and you need to #include <stdio.h> so the compiler knows the definition of printf.
#include <stdio.h>
void method(int arr[]);
int main(){
int myArray[3];
myArray[2]=12;
method(myArray);
}
void method(int arr[]){
printf("DATA: %d",arr[2]);
}
More than one point to be mentioned here,
Firstly, include the required header files which contains the function signature of the library functions you're going to use.
Secondly, either forward declare the function prototype or define the function before usage. Be aware, the ancient implicit declaration rule has been officially dropped from the C standards.
Thirdly, change
method(myArray[]);
to
method(myArray);
as the array name itself gives you the base address of the array, which is basically what you need to pass.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I need someone to tell me what I am doing wrong over here.
void main(void) {
int *arr,n,i;
printf("Enter the number of elements you want to enter=");
scanf("%d",&n);
arr = (int*)calloc(n,sizeof(int));
if(!arr)
return;
for(i=0;i<n;i++)
scanf("%d",&arr[i]);
for(i=0;i<n;i++)
printf("%d ",arr[i]);
free(arr);
}
This is nothing but a simple program to input the number of items in an array then take the input and print the inputted values. But whenever I go to input the values in the array after taking the number of items from the user, my program CRASHES. I don't understand what I am doing wrong over here. Need some help immediately. Please run the program and see if it also crashes on other systems. I executed the program using an online compiler and strangely there it was running fine. I am using the Visual Studio 2013 command line compiler to execute my program.
Compiling this piece of code with gcc I get:
calloc.c: In function ‘main’:
calloc.c:4:5: warning: incompatible implicit declaration of built-in function ‘printf’ [enabled by default]
calloc.c:5:5: warning: incompatible implicit declaration of built-in function ‘scanf’ [enabled by default]
calloc.c:7:17: warning: incompatible implicit declaration of built-in function ‘calloc’ [enabled by default]
calloc.c:18:5: warning: incompatible implicit declaration of built-in function ‘free’ [enabled by default]
using clang:
calloc.c:1:1: error: 'main' must return 'int'
void main(void) {
^
calloc.c:4:5: warning: implicitly declaring C library function 'printf' with type 'int (const char *, ...)'
printf("Enter the number of elements you want to enter=");
^
calloc.c:4:5: note: please include the header <stdio.h> or explicitly provide a declaration for 'printf'
calloc.c:5:5: warning: implicitly declaring C library function 'scanf' with type 'int (const char *, ...)'
scanf("%d",&n);
^
calloc.c:5:5: note: please include the header <stdio.h> or explicitly provide a declaration for 'scanf'
calloc.c:7:17: warning: implicitly declaring C library function 'calloc' with type 'void *(unsigned long, unsigned long)'
arr = (int*)calloc(n,sizeof(int));
^
calloc.c:7:17: note: please include the header <stdlib.h> or explicitly provide a declaration for 'calloc'
calloc.c:18:5: warning: implicit declaration of function 'free' is invalid in C99 [-Wimplicit-function-declaration]
free(arr);
^
4 warnings and 1 error generated.
As clang kindly pointed out, you need to add
#include <stdlib.h>
#include <stdio.h>
to the top of your code, as you are using functions that are declared within them. The functions you are using are in the standard C library, so the code will still compile but you should expect the unexpected. The reason your program crashes is likely to be that the compiler has made an incorrect assumption about the return type of calloc, as it doesn't have the correct declaration. Many systems will let you get away with this but at the very least they should warn you.
On a side note, please see this discussion on casting the return value of malloc (don't cast it). This advice also applies to calloc.
edit
Here's a minimal example which might shed some light on the issue:
a.c
#include <stdio.h>
int main()
{
printf("%f\n", f());
return 0;
}
b.c
float f()
{
return 4.2;
}
These two files can be compiled separately into object files:
gcc -c a.c # creates a.o
gcc -c b.c # creates b.o
No warnings or errors are generated. Switching to C99 mode causes an "implicit function declaration" warning. Note that at compile-time, the function definition isn't needed. These two object files can be linked:
gcc a.o b.o
Now, the linker finds the definitions of the functions so there is no error but it is already too late. The implicit declaration of f() when a.c was compiled causes the output to be incorrect when the program is run (I get 0.000000).
The same thing has happened here as in your question. The function calloc can be found, so there is no error at link-time. However, an incorrect assumption is made about the type that it returns, so it doesn't behave as expected on your system.
#include <stdio.h>
void main()
{
m();
}
void m()
{
printf("hi");
}
Output
hi
Warnings
main.c:11:10: warning: conflicting types for 'm' [enabled by default]
void m()
^
main.c:7:9: note: previous implicit declaration of 'm' was here
m();
^
Why this program runs successfully even though m() is called before it is defined?
And what is the meaning of the warning?
C89 allow this by implicitly converting the return type of function and parameter passed to it to int. See here.
But, this is not valid in C99 and later. This has been omitted from the standard. Either you have to declare a prototype for your function or define it before main. See the result here. There is a compile time error in this case.
If you don't declare a function in K&RC/C89, it's implicitly declared as returning int. Since yours returns void there's a mismatch.
If you add a prototype:
void m(void);
...before it's called, it'll fix things.
No, but you can declare it:
#include <stdio.h>
// declare m()
void m();
void main()
{
// use m()
m();
}
// define m()
void m()
{
printf("hi");
}
function declaration needs to be add before the first call of the function.
A full declaration includes the return type and the number and type of the arguments. This is also called the function prototype.
So you are Missing function prototype.
Add function declaration as void m(); to the code.
Edit:
C program allow to use forward declaration
.
In your case void m(); represents forward declaration of a function and is the function's prototype. After processing this declaration, the compiler would allow the user to refer to the entity m in the rest of the program.
Definition for a function must be provided somewhere (same file or other, where it would be responsibility of the linker to correctly match references to particular function in one or several object files with its definition, which must be unique, in another): (From wikipedia page)
That is why defining function after main work in your program.
Yes - It is called using prototypes.
I.e. put
void m();
At the start of the file
No, you cant normally.
The code would print the error something like this if the function is not defined before invoking it:
s.c: In function 'main':
s.c:4:1: warning: implicit declaration of function 'm' [-Wimplicit-function-declaration]
4 | m();
| ^
s.c: At top level:
s.c:8:6: warning: conflicting types for 'm'
8 | void m(){
| ^
s.c:4:1: note: previous implicit declaration of 'm' was here
4 | m();
| ^
So, it is always a good practice to declare method before or inside of main method especially if you are defining a method outside the scope of main function.
The code snippet for best practice has been provided below:
#include <stdio.h>
void m();
void main()
{
m();
}
void m()
{
printf("hi");
}
For this code:
int i=0; char **mainp;
for(i=0;i<2;++i)
{
mainp[i]=malloc(sizeof(char)*200);
if(!scanf("%[^#],#",mainp[i]))
break;
if(i<2)
scanf("%[^#],#",mainp[i]);
}
GCC emits the warnings:
warning: implicit declaration of function ‘scanf’
warning: incompatible implicit declaration of built-in function ‘scanf’
warning: ‘mainp’ may be used uninitialized in this function
And I get a segmentation fault at runtime
input:(P>Q),(Q>R),-R#-P
output:
(P>Q),(Q>R),-R
(empt slot)
i expected to give me
(P>Q),(Q>R),-R
-P //where should i fix in my code such that it gives me expected
//output
Problem #1:
warning: ‘mainp’ may be used uninitialized in this function
You need to allocate memory for the array of arrays first.
char **mainp = malloc(sizeof(char*)*2);
Problem #2:
warning: implicit declaration of function ‘scanf’
warning: incompatible implicit declaration of built-in function ‘scanf’
You need to include stdio.h at the top of your file:
#include <stdio.h>
Problem #3: (Not included in your compiling warnings)
Remember to free both the allocated array members and also the array of array address.
gcc expects this line at the beginning of your file:
#include <stdio.h>
and a declaration of mainp like this one:
char *mainp[2];
You shouldn't use functions without declaring them; you used scanf, but at no point in your code is scanf declared. Since it's a standard library function it's declared in one of the standard headers, stdio.h, so you just need to include it:
#include <stdio.h>
Brian's answer is good for the other part