Solved

is this a legal C program?

Posted on 2008-10-23
14
199 Views
Last Modified: 2012-05-05
I mean would it cause an error as the function is declared but not defined?
void a(void);

Open in new window

0
Comment
Question by:kuntilanak
  • 4
  • 4
  • 3
  • +2
14 Comments
 
LVL 53

Expert Comment

by:Infinity08
Comment Utility
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
 
LVL 16

Expert Comment

by:HooKooDooKu
Comment Utility
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
 

Author Comment

by:kuntilanak
Comment Utility
I still don't get it why it's not legal? so each function declaration must have it's corresponding function definitions?
0
 
LVL 45

Expert Comment

by:Kdo
Comment Utility
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
 

Author Comment

by:kuntilanak
Comment Utility
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
 
LVL 45

Expert Comment

by:Kdo
Comment Utility

>> 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
 
LVL 53

Expert Comment

by:Infinity08
Comment Utility
>> 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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 45

Expert Comment

by:Kdo
Comment Utility
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
 
LVL 12

Expert Comment

by:trinitrotoluene
Comment Utility
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
 

Author Comment

by:kuntilanak
Comment Utility
>>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
 
LVL 53

Accepted Solution

by:
Infinity08 earned 500 total points
Comment Utility
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
 

Author Comment

by:kuntilanak
Comment Utility
gotcha! thanks guys
0
 
LVL 45

Expert Comment

by:Kdo
Comment Utility

>> 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
 
LVL 12

Expert Comment

by:trinitrotoluene
Comment Utility
>>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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

There is an easy way, in .NET, to centralize the treatment of all unexpected errors. First of all, instead of launching the application directly in a Form, you need first to write a Sub called Main, in a module. Then, set the Startup Object to th…
Windows Script Host (WSH) has been part of Windows since Windows NT4. Windows Script Host provides architecture for building dynamic scripts that consist of a core object model, scripting hosts, and scripting engines. The key components of Window…
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now