Solved

is this a legal C program?

Posted on 2008-10-23
14
200 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
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
 
LVL 45

Expert Comment

by:Kdo
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 45

Expert Comment

by:Kdo
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 45

Expert Comment

by:Kdo
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 500 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 45

Expert Comment

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Python 2.7 - French characters 6 58
mapShare challenge 13 88
How to install SVN Command Line Client? 5 41
outlook office 365 8 81
When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

919 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

17 Experts available now in Live!

Get 1:1 Help Now