Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

is this a legal C program?

Posted on 2008-10-23
14
Medium Priority
?
206 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
  • 3
  • +2
14 Comments
 
LVL 53

Expert Comment

by:Infinity08
ID: 22788213
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
ID: 22788242
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
ID: 22788284
I still don't get it why it's not legal? so each function declaration must have it's corresponding function definitions?
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 46

Expert Comment

by:Kent Olsen
ID: 22788423
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
ID: 22788493
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 46

Expert Comment

by:Kent Olsen
ID: 22788575

>> 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
ID: 22788601
>> 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
 
LVL 46

Expert Comment

by:Kent Olsen
ID: 22788737
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
ID: 22788901
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
ID: 22789424
>>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 2000 total points
ID: 22789455
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
ID: 22789512
gotcha! thanks guys
0
 
LVL 46

Expert Comment

by:Kent Olsen
ID: 22789533

>> 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
ID: 22794556
>>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

Industry Leaders: 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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Navigation is an important part of web design from a usability perspective. But it is often a pain when it comes to a developer’s perspective. By navigation, it often means menuing. This is less theory and more practical of how to get a specific gro…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The viewer will learn how to implement Singleton Design Pattern in Java.
This video teaches viewers about errors in exception handling.

715 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