#include <stdio.h>
#define main() main(){printf("hi");}int i()
main()
{//empty main
}
what is the use of int i()
That's a pretty silly program, but the purpose of the int i() is so that it will compile - the braces at the end:
{//empty main
}
will cause an error if there isn't a function declaration included in the #define statement. If I delete it, gcc gives the error:
testfile.c:4: error: expected identifier or ‘(’ before ‘{’ token
You can use the -E flag to gcc to see why the int i() is necessary - it will show you the output of the preprocessor without compiling.
Think about what you get if you expand the macro main() in the program:
#include <stdio.h>
main(){printf("hi");}int i()
{//empty main
}
The int i() is needed there to make the remaining { ... } part of a syntactically valid function definition.
As for intention, I can only guess that the point of the macro is to replace the existing main with a stub one. It's a bit icky IMO.
In that code main() will be expanded and the result will end with
int i()
{//empty main
}
what is the use of int i()
It makes the output of a very strange and broken macro compilable
Related
My compiler (GCC) is giving me the warning:
warning: implicit declaration of function
Why is it coming?
You are using a function for which the compiler has not seen a declaration ("prototype") yet.
For example:
int main()
{
fun(2, "21"); /* The compiler has not seen the declaration. */
return 0;
}
int fun(int x, char *p)
{
/* ... */
}
You need to declare your function before main, like this, either directly or in a header:
int fun(int x, char *p);
The right way is to declare function prototype in header.
Example
main.h
#ifndef MAIN_H
#define MAIN_H
int some_main(const char *name);
#endif
main.c
#include "main.h"
int main()
{
some_main("Hello, World\n");
}
int some_main(const char *name)
{
printf("%s", name);
}
Alternative with one file (main.c)
static int some_main(const char *name);
int some_main(const char *name)
{
// do something
}
When you do your #includes in main.c, put the #include reference to the file that contains the referenced function at the top of the include list.
e.g. Say this is main.c and your referenced function is in "SSD1306_LCD.h"
#include "SSD1306_LCD.h"
#include "system.h" #include <stdio.h>
#include <stdlib.h>
#include <xc.h>
#include <string.h>
#include <math.h>
#include <libpic30.h> // http://microchip.wikidot.com/faq:74
#include <stdint.h>
#include <stdbool.h>
#include "GenericTypeDefs.h" // This has the 'BYTE' type definition
The above will not generate the "implicit declaration of function" error, but below will-
#include "system.h"
#include <stdio.h>
#include <stdlib.h>
#include <xc.h>
#include <string.h>
#include <math.h>
#include <libpic30.h> // http://microchip.wikidot.com/faq:74
#include <stdint.h>
#include <stdbool.h>
#include "GenericTypeDefs.h" // This has the 'BYTE' type definition
#include "SSD1306_LCD.h"
Exactly the same #include list, just different order.
Well, it did for me.
You need to declare the desired function before your main function:
#include <stdio.h>
int yourfunc(void);
int main(void) {
yourfunc();
}
When you get the error: implicit declaration of function it should also list the offending function. Often this error happens because of a forgotten or missing header file, so at the shell prompt you can type man 2 functionname and look at the SYNOPSIS section at the top, as this section will list any header files that need to be included. Or try http://linux.die.net/man/ This is the online man pages they are hyperlinked and easy to search.
Functions are often defined in the header files, including any required header files is often the answer. Like cnicutar said,
You are using a function for which the compiler has not seen a
declaration ("prototype") yet.
If you have the correct headers defined & are using a non GlibC library (such as Musl C) gcc will also throw error: implicit declaration of function when GNU extensions such as malloc_trim are encountered.
The solution is to wrap the extension & the header:
#if defined (__GLIBC__)
malloc_trim(0);
#endif
This error occurs because you are trying to use a function that the compiler does not understand. If the function you are trying to use is predefined in C language, just include a header file associated with the implicit function.
If it's not a predefined function then it's always a good practice to declare the function before the main function.
Don't forget, if any functions are called in your function, their prototypes must be situated above your function in the code. Otherwise, the compiler might not find them before it attempts to compile your function. This will generate the error in question.
The GNU C compiler is telling you that it can find that particular function name in the program scope. Try defining it as a private prototype function in your header file, and then import it into your main file.
I think the question is not 100% answered. I was searching for issue with missing typeof(), which is compile time directive.
Following links will shine light on the situation:
https://gcc.gnu.org/onlinedocs/gcc-5.3.0/gcc/Typeof.html
https://gcc.gnu.org/onlinedocs/gcc-5.3.0/gcc/Alternate-Keywords.html#Alternate-Keywords
as of conculsion try to use __typeof__() instead. Also gcc ... -Dtypeof=__typeof__ ... can help.
Before, I did't read the generated code from yacc carefully. Now I saw one snippet of code like this:
#define yyparse ol_parser_parse
I know yyparse's definition as follows:
int yyparse (void)
so, this macro definition should be interpreted as: every "ol_parser_parse" in code will be replaced by "yyparse". And I wrote some code for testing that:
#include <stdio.h>
#define yyparse ol_parser_parse
void yyparse()
{
printf("hello world\n");
}
void main()
{
ol_parser_parse();
}
It worked!
But according to the definition about "macro definition":
#define <identifier>(<parameter list>) <replacement token list>
I am confused by this. Who can help me explain this? Thanks in advance!
Here's your code as seen by the compiler after the substitution is performed:
#include <stdio.h>
void ol_parser_parse()
{
printf("hello world\n");
}
void main()
{
ol_parser_parse();
}
There's nothing mysterious about it at that point. Every instance of "yyparse" becomes "ol_parser_parse" on every line following the #define.
In the documentation identifier means the thing being substituted and replacement token list is what you're substituting it with. You can also have parameters, like:
#define TIMES_TWO(n) ((n) * 2)
Where you can then do:
int x = TIMES_TWO(3);
Where that's equivalent to:
int x = ((3) * 2);
Where the extra brackets are so you can do this and not mess up order of operations:
int x = TIMES_TWO(1 - 5);
Without the brackets it'd show up as this:
int x = 1 - 5 * 2;
Which evaluates to 1 - 10 which is not what you want.
There's an art to using #define effectively to hide otherwise ugly implementation details. The name ol_parser_parse isn't something you need to worry about if you can use the yyparser macro. That gives the implementers the freedom to rename that function and the corresponding macro without breaking all your code.
Hello folks out there,
this is my code:
main.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "sqlite3.h"
#include "db_typedefs.h"
#include "operations.h"
int main(){
printf("Text\n");
int f = 3;
void add_mini(3);
}
operations.h
#ifndef ADD_OPERATIONS_H_INCLUDED
#define ADD_OPERATIONS_H_INCLUDED
void add_mini(int flag);
#endif // ADD_OPERATIONS_H_INCLUDED
operations.c
void add_mini(int flag)
{
int rc;
rc = flag;
printf("%i\n", rc);
}
Operations.c has also libraries included similar to main.c.
Compiler Error
error: expected declaration specifiers or '...' before numeric constant
regarding to void add_mini(3)
It seems like I'm unable to pass a simple integer value. While debugging it's even skipping the add_mini line.
Do you have any idea what's going on?
The whole code is embedded in a larger query to determine typed orders but this works fine. I just can't pass this simple integer value.
Thanks in advance.
When you use
void add_mini(3);
the compiler thinks it is a function declaration, not a function call. The argument 3 is not valid for a function declaration. Hence, the compiler complains.
Remove the void part to call the function.
int main(){
printf("Text\n");
int f = 3;
add_mini(3);
}
or, since you have initialized f to 3,
int main(){
printf("Text\n");
int f = 3;
add_mini(f);
}
Call the function like so: add_mini(3); rather than void add_mini(3);
Remove the word void for calling add_mini from main.c :
add_mini(3);
Or
(void)add_mini(3);
#include <stdio.h>
#define A 1
#if A
printf("Csau");
#endif
int main()
{
return 0;
}
I am trying to run this but my compiler is giving me the error of
main.c:4:9: error: expected declaration specifiers or '...' before
string constant printf("Csau");
Any suggestions why this isn't working ?
P.S. In main function it's working fine with some minor modification.
Edit : Anyway I can show the output outside the main function ?
You can output messages at compile time:
#include <stdio.h>
#define A 1
#if A
#warning "Csau"
#endif
int main()
{
return 0;
}
At runtime you can not print something outside another function body.
Once preprocessed, your code boils down more or less to this:
#include <stdio.h>
printf("Csau");
int main()
{
return 0;
}
And this is not correct C. You cannot call a function outside functions. It doesn't make sense. When you run the program, the system calls your main function and that's it.
You are getting this error because you are trying to call printf from outside of main (or outside of another function)
Edit : Anyway I can show the output outside the main function ?
No. The program starts at the beginning of the "main" function. Putting code outside of functions is syntactically incorrect. Even if the compiler let you do it, the code would never be executed.
The preprocessor doesn't run before main() (aka: before execution) but before compilation. The actual compilation step is fed the results of preprocessing as input, so, in your case, an invalid C program, as you can't have statements outside of functions.
At runtime of your program, there is nothing before entering main(). Of course, your runtime will probably setup a few things, but anything happening before main() is called is not part of your C program.
My compiler (GCC) is giving me the warning:
warning: implicit declaration of function
Why is it coming?
You are using a function for which the compiler has not seen a declaration ("prototype") yet.
For example:
int main()
{
fun(2, "21"); /* The compiler has not seen the declaration. */
return 0;
}
int fun(int x, char *p)
{
/* ... */
}
You need to declare your function before main, like this, either directly or in a header:
int fun(int x, char *p);
The right way is to declare function prototype in header.
Example
main.h
#ifndef MAIN_H
#define MAIN_H
int some_main(const char *name);
#endif
main.c
#include "main.h"
int main()
{
some_main("Hello, World\n");
}
int some_main(const char *name)
{
printf("%s", name);
}
Alternative with one file (main.c)
static int some_main(const char *name);
int some_main(const char *name)
{
// do something
}
When you do your #includes in main.c, put the #include reference to the file that contains the referenced function at the top of the include list.
e.g. Say this is main.c and your referenced function is in "SSD1306_LCD.h"
#include "SSD1306_LCD.h"
#include "system.h" #include <stdio.h>
#include <stdlib.h>
#include <xc.h>
#include <string.h>
#include <math.h>
#include <libpic30.h> // http://microchip.wikidot.com/faq:74
#include <stdint.h>
#include <stdbool.h>
#include "GenericTypeDefs.h" // This has the 'BYTE' type definition
The above will not generate the "implicit declaration of function" error, but below will-
#include "system.h"
#include <stdio.h>
#include <stdlib.h>
#include <xc.h>
#include <string.h>
#include <math.h>
#include <libpic30.h> // http://microchip.wikidot.com/faq:74
#include <stdint.h>
#include <stdbool.h>
#include "GenericTypeDefs.h" // This has the 'BYTE' type definition
#include "SSD1306_LCD.h"
Exactly the same #include list, just different order.
Well, it did for me.
You need to declare the desired function before your main function:
#include <stdio.h>
int yourfunc(void);
int main(void) {
yourfunc();
}
When you get the error: implicit declaration of function it should also list the offending function. Often this error happens because of a forgotten or missing header file, so at the shell prompt you can type man 2 functionname and look at the SYNOPSIS section at the top, as this section will list any header files that need to be included. Or try http://linux.die.net/man/ This is the online man pages they are hyperlinked and easy to search.
Functions are often defined in the header files, including any required header files is often the answer. Like cnicutar said,
You are using a function for which the compiler has not seen a
declaration ("prototype") yet.
If you have the correct headers defined & are using a non GlibC library (such as Musl C) gcc will also throw error: implicit declaration of function when GNU extensions such as malloc_trim are encountered.
The solution is to wrap the extension & the header:
#if defined (__GLIBC__)
malloc_trim(0);
#endif
This error occurs because you are trying to use a function that the compiler does not understand. If the function you are trying to use is predefined in C language, just include a header file associated with the implicit function.
If it's not a predefined function then it's always a good practice to declare the function before the main function.
Don't forget, if any functions are called in your function, their prototypes must be situated above your function in the code. Otherwise, the compiler might not find them before it attempts to compile your function. This will generate the error in question.
The GNU C compiler is telling you that it can find that particular function name in the program scope. Try defining it as a private prototype function in your header file, and then import it into your main file.
I think the question is not 100% answered. I was searching for issue with missing typeof(), which is compile time directive.
Following links will shine light on the situation:
https://gcc.gnu.org/onlinedocs/gcc-5.3.0/gcc/Typeof.html
https://gcc.gnu.org/onlinedocs/gcc-5.3.0/gcc/Alternate-Keywords.html#Alternate-Keywords
as of conculsion try to use __typeof__() instead. Also gcc ... -Dtypeof=__typeof__ ... can help.