Results 1 to 9 of 9
Hi,
I'm not sure whether this is the right forum for this question, and if not I hope someone could help me to find the right one.
I have a ...
- 03-22-2010 #1Just Joined!
- Join Date
- Mar 2010
- Posts
- 3
GCC C programming question
Hi,
I'm not sure whether this is the right forum for this question, and if not I hope someone could help me to find the right one.
I have a simple C program, 2 source files and 1 header file (I'm so sorry about the syntax, but this forum did for some reason see an URL among my code...):
---
#include "car.h"
int main()
{
create_car("BMW");
return 0;
}
---
#ifndef CAR_H
#define CAR_H
void create_car(/*const char* s*/);
#endif
---
#include "car.h"
#include <stdio.h>
void create_car(/*const char* s*/)
{
// printf("S=%s\n",s);
printf("S\n");
}
---
Now, I'm sure that you can see that the create car function in the car.h file takes no arguments, and that the call in the main function is passing a constant string "BMW". Now, this should not compile I think. But apparently it does:
jockeATubuntu:~/src$ gcc -Wall car.c simple_test.c -o jtest
jockeATubuntu:~/src$
If I uncomment the comments and remove the "BMW" string I get a compilation error though (as it should of course):
---
#include "car.h"
int main()
{
create_car();
return 0;
}
---
#ifndef CAR_H
#define CAR_H
void create_car(const char* s);
#endif
---
#include "car.h"
#include <stdio.h>
void create_car(const char* s)
{
printf("S=%s\n",s);
}
---
Compilation:
jockeATubuntu:~/src$ gcc -Wall car.c simple_test.c -o jtest
simple_test.c: In function ‘main’:
simple_test.c:5: error: too few arguments to function ‘create_car’
jockeATubuntu:~/src$
My question is then, why can GCC only detect too few (and not too many) arguments?
- 03-23-2010 #2Linux Guru
- Join Date
- Apr 2009
- Location
- I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
- Posts
- 8,974
First, you can use the
block to display code without conversion of special character sequences, such as smiley-faces.Code:...
Anyway, you don't show what the function declaration is in "car.h". Please post that inside a set of code/endcode blocks.Sometimes, real fast is almost as good as real time.
Just remember, Semper Gumbi - always be flexible!
- 03-23-2010 #3Just Joined!
- Join Date
- Mar 2010
- Posts
- 1
In case of function definitions, the absence of parameters is sufficient to indicate that there are none. However this is not true in case of declarations.
void create_car();
says create_car takes an indeterminate number of parameters. This is K&R-style non prototyped function declaration in which there was no compile-time checking for the right number and types of parameters in a function call. With the introduction of prototypes, this form of function declaration has become obsolete and should not be used anymore as it provides no protection, even though compilers still support it.
So, in order for a declaration to serve as a prototype, the parameter list must be complete. In the case where there are no parameters, you must specify this condition with void as :
void create_car(void);
- 03-23-2010 #4Just Joined!
- Join Date
- Mar 2010
- Posts
- 3
Thank you Meenakshi,
Is there no way to tell the compiler to understand no arguments as it should be no arguments? Is there another style than the K&R that you can use? I don't like to write anything in an argument list where there should be no arguments. This only feels confusing.
Regards,
Joachim
- 03-23-2010 #5
I don't know whether I understood your problem-but here is my questions ,
What are you trying to achieve with this program?Why do you want to avoid K&R style? If you know that function is not receiving any arguments?
why can't you simply use void? As Rubberman said,posting source code inside code block will help readability and what's your car.h contents?- Lakshmipathi.G
-------------------
FOSS India Award winning ext3fs Undelete tool and tutorials www.giis.co.in
First they criticize you,Then they laugh at you,Then they fight with you,Then you win. - M.K.Gandhi
-------------------
- 03-23-2010 #6Just Joined!
- Join Date
- Mar 2010
- Posts
- 3
Ok, I will keep that thing with code blocks in mind in the future.
But as I said, I don't want to clutter my code with unnecessary things if I can avoid it. Hence, I would expect
void create_car();
to have exactly the same meaning as
void create_car(void);
I do not want to write "void" if I really don't need it, and I don't see why I really should have to put it there.
- 03-23-2010 #7Linux Guru
- Join Date
- Apr 2009
- Location
- I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
- Posts
- 8,974
It's is just one of the requirements of the language. Get over it. No arguments, the arg-list is 'void'.
Sometimes, real fast is almost as good as real time.
Just remember, Semper Gumbi - always be flexible!
- 03-25-2010 #8
I would like to reiterate what others have said.
If you have a function that truly has NO parameters, it is part of programming best practices to be explicit about the fact that it has no parameters.
Hence the declaration
void create_car(void);
is the correct way to write the function declaration. The "void" tells anyone else reading the code "hey, I have no parameters". But if you simply leave them out, all you have is a declaration that create_car is a function that may or may not have parameters. THAT is confusing and would not be beneficial to anyone.
Use the void in function declarations that have no parameters. That is the way it is done by professional programmers. Or at least all the one I have ever worked with over the years.
- 03-25-2010 #9Linux Guru
- Join Date
- Apr 2009
- Location
- I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
- Posts
- 8,974
In any case, in case you are still wondering why your examples worked the way they did, it is really quite simple. As I understand it (things change with newer standards, such as C99, usually tightening up the rules):
1. Declaring a function with no return value means it can return an int (current compilers should give you a warning about this).
2. An empty argument list in the declaration means that an integer argument will be accepted, and since it can convert your pointer to an int quietly, it will take it. However, the compiler should emit a warning about this usage also.Sometimes, real fast is almost as good as real time.
Just remember, Semper Gumbi - always be flexible!


Reply With Quote
