wood burning stoves 2.0*
The moose likes C / C++ and the fly likes Calling C Function without declaring prototype Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Languages » C / C++
Bookmark "Calling C Function without declaring prototype" Watch "Calling C Function without declaring prototype" New topic
Author

Calling C Function without declaring prototype

Jeffry Kristianto Yanuar
Ranch Hand

Joined: Oct 01, 2007
Posts: 759
hi guys, I am in the middle of learning C. I thought that if we wanted to call C function, we had to declared a prototype first. Look at the code



It still works even though I commented the function declaration. I use NetBeans 6.5.1 as an IDE and WINGW compiler. Thanks !!!

Jeffry Kristianto Yanuar SCJP, SCJA, SCJD

Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14117
    
  16

My C is a bit rusty... You don't have to declare the function first, but if you don't, the C compiler will assume the function returns an int (even if the real function, defined later, doesn't). It's a bit strange, but this is how C works. It's a good idea to always declare functions before you call them.

Doesn't the compiler give you a warning if you do this? Most modern C compilers will give a warning if you do this.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Jeffry Kristianto Yanuar
Ranch Hand

Joined: Oct 01, 2007
Posts: 759
Surprisingly, when Compiling using NetBeans 6.8 RC1, I get the error message. The most possibility is NetBeans 6.5.1 and NetBeans 6.8 RC1 invoke gcc command using different parameter.

By The way, what books do you use when learning C ?

Jeffry Kristianto Yanuar SCJP, SCJA, SCJD
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14117
    
  16

Maybe you can change the settings that NetBeans uses to call gcc. I haven't programmed in C for a long time, so I don't know how to do that, but it's probably not hard to find in the preferences of NetBeans. When calling gcc from the command line, it's usually a good idea to use -Wall (which means: turn on all warnings), for example:

gcc -Wall hello.c -o hello

I'm sorry but I don't have a good book recommendation for C - I learned C a long time ago, on the Amiga when I was 16 or so.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Calling C Function without declaring prototype