Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 208
  • Last Modified:

is this a legal C program?

I mean would it cause an error as the function is declared but not defined?
void a(void);

Open in new window

0
kuntilanak
Asked:
kuntilanak
  • 4
  • 4
  • 3
  • +2
1 Solution
 
Infinity08Commented:
No, function declarations only serve to the compiler to indicate the return type, name and parameter type(s) of a function. The definition does not have to be in the same compilation unit. It can be in some other compilation unit, or it can be absent entirely.

Note that if an undefined function is called, then the linker will complain, not the compiler.
0
 
HooKooDooKuCommented:
In addition to everything Infinity08 said, it's stil not a legal C program because there is no 'main()' defining the entry point to the program.
0
 
kuntilanakAuthor Commented:
I still don't get it why it's not legal? so each function declaration must have it's corresponding function definitions?
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Kent OlsenData Warehouse Architect / DBACommented:
Hi kuntilanak,

While it is legal C (it should compile just fine) it is not a program.

A program needs an execution path.  A function header doesn't provide for execution.  It simply says that IF there is a function (in your case called *a*) it will have the characteristics shown in the header.

HooKooDooKu's point about there being no function called 'main' is pretty close.  By convention, the starting point of the user code begins with a function called *main*.  Without it, you can have legal C and create loadable modules (the items that could be placed into a library) but you can't really have a "program".


Good Luck,
Kent
0
 
kuntilanakAuthor Commented:
well, I understand all that..assuming that it has all the neccesary main, etc.. will this program compile.. that's my problem. One of my colleagues told me that it's a:

 function is declared but not defined error

0
 
Kent OlsenData Warehouse Architect / DBACommented:

>> function is declared but not defined error

That would probably depend on the compiler AND the warning/error level that you've chosen for compilation.

0
 
Infinity08Commented:
>> I still don't get it why it's not legal?

It IS legal. See my post ;)


>> so each function declaration must have it's corresponding function definitions?

No, it doesn't. You only need a function definition when you actually call the function, and the function definition does not have to be in the same compilation unit as the place it's called.

Note also, that the compiler will not complain about a missing function definition, but the linker will.
0
 
Kent OlsenData Warehouse Architect / DBACommented:
A function prototype (as in your example) tells the C compiler that IF there is a function by the name in the prototype, it will match the definition in the prototype.  It doesn't declare the function, nor does it declare that there MUST be one.  It simply says that IF the program declares one, this is what it will look like.

If it helps to think of it this way, the C compiler knows all about integers, floats, doubles, chars, pointers, etc.  The program doesn't have to use them, but IF they are used they will have specific characteristics.


Kent
0
 
trinitrotolueneCommented:
a simple way to illustrate this is :

we include header files such as:

iostream
stdio
fstream
conio
iomanip

Now if you open these headers we find that several function declarations are present.
If you call any of these functions then the linker would try to link the definition of the function, so in this case a definition is needed.
If you do not call the function, then the declaration is still there but the linker does not need to resolve a definition since you've not called the function.

However take care to link the library/object code where the function is defined if you call the function anywhere in your code.

So the bottom line is : declaring a function and not calling it does not need a definition and is not an error.
0
 
kuntilanakAuthor Commented:
>>So the bottom line is : declaring a function and not calling it does not need a definition and is not an error.

even gcc won't complaint about it?
0
 
Infinity08Commented:
The compiler will NEVER complain about a missing definition.

The linker will only complain if the function is called, and the definition is missing.

A function with a missing definition that is never called will not generate an error of any kind.
0
 
kuntilanakAuthor Commented:
gotcha! thanks guys
0
 
Kent OlsenData Warehouse Architect / DBACommented:

>> So the bottom line is : declaring a function and not calling it does not need a definition and is not an error

That's a bit ambiguous.

A C function is an identifier, just as is an integer, float, double, char, etc.  It is a user created name to be used within the rules of C. ALL identifiers must be declared in C.

int b,c,d,e;
double f,g,h,i,j;
float k,l,m,n,o,p;
void a(void);
void z(void)
{
  return
};

In the statements above, ALL of them declare identifiers.  The big difference is that no code, and no storage is reserved for a.  The declaration for a is a function prototype.

Functions can be declared explicitly or implicitly.  The function prototype is an explicit declaration of what the function will look like.  Declaring a function without explicitly using a prototype is implicitly declaring the function.  Both are perfectly valid C, though some compilers allow you to flag implicit declarations as warnings.

The bottom line is, you can have as many function prototypes as you want within your source code.  They only come into play if you actually define a function with the same name as a prototype, or call a function that is declared by the prototype.

Good Luck,
Kent
0
 
trinitrotolueneCommented:
>>The bottom line is, you can have as many function prototypes as you want within your source code.  They only come into play if you actually define a function with the same name as a prototype, or call a function that is declared by the prototype.

I think that is what I said in fewer words...;-)

0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

  • 4
  • 4
  • 3
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now