Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Calling C Function without declaring prototype

 
Jeffry Kristianto Yanuar
Ranch Hand
Posts: 759
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 15438
41
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jeffry Kristianto Yanuar
Ranch Hand
Posts: 759
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 15438
41
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic