473,698 Members | 2,134 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

A tired question?

mdh
I have looked this up in the FAQ, and still do not have a nice
understanding of it.

If I have defined a function in foo.c, and if, for the sake of
argument have foo.h contain that function's declaration, and if , as
one of the answers to a similar question was "All definitions are also
declarations!" then does one need to use "# include foo.h" in foo.c?

Thanks in advance.

Jun 27 '08 #1
25 1346
mdh wrote:
I have looked this up in the FAQ, and still do not have a nice
understanding of it.

If I have defined a function in foo.c, and if, for the sake of
argument have foo.h contain that function's declaration, and if , as
one of the answers to a similar question was "All definitions are also
declarations!" then does one need to use "# include foo.h" in foo.c?
You can usually get away without it,
but "getting away with something" isn't good coding.

If you # include foo.h" in foo.c,
then the compiler will check that the function types match.

--
pete
Jun 27 '08 #2
In article <72************ *************** *******@f1g2000 prb.googlegroup s.com>,
mdh <md**@comcast.n etwrote:
>I have looked this up in the FAQ, and still do not have a nice
understandin g of it.

If I have defined a function in foo.c, and if, for the sake of
argument have foo.h contain that function's declaration, and if , as
one of the answers to a similar question was "All definitions are also
declarations !" then does one need to use "# include foo.h" in foo.c?
It's not strictly necessary (since the definition of the function does
serve as the declaration there), but it's still a good idea to do so,
because that lets the compiler make sure that the declaration in foo.h
matches what's in foo.c.
The compiler can (and in many common cases is required to) complain if
it sees two incompatible declarations of the same function or variable
in the same translation unit, but if you don't #include "foo.h" from
foo.c, it won't ever see both versions at the same time[1], so it won't
have the opportunity to do that.

Diagnosable errors are ALWAYS better than silent undefined behavior,
and silent undefined behavior is what you end up getting if you call a
function that was declared (to the calling code) differently than it
was defined.
dave

[1] It's still quite possible for a sufficiently clever compiler to
arrange for this to be checked, and there are also code checking
tools (lint is the canonical example) that specifically look for
incompatible declarations in places where the compiler is unlikely
to see them, but it does take rather more effort.

--
Dave Vandervies dj3vande at eskimo dot com

Insult your lusers, not random swaths of your fellow sysadmins.
--Alan J Rosenthal in the scary devil monastery
Jun 27 '08 #3

"mdh" <md**@comcast.n etwrote in message
news:dd******** *************** ***********@w4g 2000prd.googleg roups.com...
Not sure if this is straying to the philosophical.. .. It is clearly a
good check, almost having the compiler ask "Are you really sure that
the function definition is correctly written?" if it can check the
format against a declaration. Other than that...is there something in
the C language that sets it up this way? ( For instance, as far as
variables are concerned, K&R say (Page 40) "All variables must be
declared before use,...). I have not found any language in K&R that
make such a specific statement about functions. Sorry is this sounds a
little dumb, if so, it's me not getting it.
See p.217-218 (section A.8.6.3, especially the smaller-print paragraph
near the end of p 218.)

..

-Mike
Jun 27 '08 #4
dj3va...@csclub .uwaterloo.ca.i nvalid wrote:
mdh <md**@comcast.n etwrote:
If I have defined a function in foo.c, and if, for the sake
of argument have foo.h contain that function's declaration,
and if , as one of the answers to a similar question was
"All definitions are also declarations!" then does one
need to use "# include foo.h" in foo.c?

It's not strictly necessary (since the definition of the function
does serve as the declaration there), but it's still a good idea
to do so, because that lets the compiler make sure that the
declaration in foo.h matches what's in foo.c.
For full value, make your declarations prototypes.

--
Peter
Jun 27 '08 #5
mdh
On Jun 25, 8:35*pm, Peter Nilsson <ai...@acay.com .auwrote:
dj3va...@csclub .uwaterloo.ca.i nvalid wrote:
mdh *<m...@comcast. netwrote:
If I have defined a function in foo.c, and if, for the sake
of argument have foo.h contain that *function's declaration,
and if , as one of the answers to a similar question was
"All definitions are also declarations!" then does one
need to use "# include foo.h" in foo.c?
It's not strictly necessary (since the definition of the function
does serve as the declaration there), but it's still a good idea
to do so, because that lets the compiler make sure that the
declaration in foo.h matches what's in foo.c.

For full value, make your declarations prototypes.

--
Peter
Thank you all....I am sure with the clarity of time, it will be more
apparent to me.
Jun 27 '08 #6
mdh wrote:
I have looked this up in the FAQ, and still do not have a nice
understanding of it.

If I have defined a function in foo.c, and if, for the sake of
argument have foo.h contain that function's declaration, and if , as
one of the answers to a similar question was "All definitions are also
declarations!" then does one need to use "# include foo.h" in foo.c?

Thanks in advance.
Besides what the others have already said, note that you will have to
have a declaration at the top of foo.c, if the some function or
functions call the function in question and appear before that function
in the translation unit. And one way to place a declaration at the top
of foo.c is to include foo.h of course.

Jun 27 '08 #7
mdh
On Jun 25, 9:09*pm, santosh <santosh....@gm ail.comwrote:
mdh wrote:
I have looked this up in the FAQ, and still do not have a nice
understanding of it.
Besides what the others have already said, note that you will have to
have a declaration at the top of foo.c, if the some function or
functions call the function in question and appear before that function
in the translation unit. And one way to place a declaration at the top
of foo.c is to include foo.h of course.
So, is this then correct.
If I am calling, lets say f(), which is defined in foo.c, from main,
then I only need to #include foo.h in main? and not in foo.c? Or,
only include foo.h in foo.c and not in main? I have been including
them in both ( ie main and foo.c) and it seems duplicative and
unnecessary?

Jun 27 '08 #8
mdh said:
On Jun 25, 9:09 pm, santosh <santosh....@gm ail.comwrote:
>mdh wrote:
I have looked this up in the FAQ, and still do not have a nice
understanding of it.
>
Besides what the others have already said, note that you will have to
have a declaration at the top of foo.c, if the some function or
functions call the function in question and appear before that function
in the translation unit. And one way to place a declaration at the top
of foo.c is to include foo.h of course.

So, is this then correct.
If I am calling, lets say f(), which is defined in foo.c, from main,
then I only need to #include foo.h in main? and not in foo.c? Or,
only include foo.h in foo.c and not in main? I have been including
them in both ( ie main and foo.c) and it seems duplicative and
unnecessary?
Because you need to call foo() from main.c, you need a declaration for
foo() in main.c's scope, right?

Because you might need, one day (perhaps tomorrow or later on this
afternoon), to call foo() from bar.c, it is convenient to have the foo()
declaration in a header, so that main.c and bar.c can just #include it
rather than you have to type a separate declaration for each. Bear in mind
that the minimal work saving for this becomes rather more significant when
you realise that foo() is really just a shorthand for foo_create(),
foo_destroy(), foo_insert(), foo_delete(), foo_sort(), foo_enquire(),
foo_test(), foo_process(), foo_print(), and whatever other fooish notions
one might entertain. Suddenly, the header starts to look attractive,
right?

Because you might, one day Real Soon Now, modify the implementation of
foo(), it makes sense to make sure that main.c has the right declarations,
right?

Because #including foo.h into foo.c will have the effect of forcing the
compiler to check that the declarations in foo.h match the declarations in
foo.c, #including foo.h into foo.c suddenly starts to look attractive
here, too, right?

--
Richard Heathfield <http://www.cpax.org.uk >
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Jun 27 '08 #9
mdh
On Jun 25, 9:52*pm, Richard Heathfield <r...@see.sig.i nvalidwrote:
mdh said:
On Jun 25, 9:09 pm, santosh <santosh....@gm ail.comwrote:
mdh wrote:
I have looked this up in the FAQ, and still do not have a nice
understanding of it.
Besides what the others have already said, note that you will have to
have a declaration at the top of foo.c, if the some function or
functions call the function in question and appear before that function
in the translation unit. And one way to place a declaration at the top
of foo.c is to include foo.h of course.
So, is this then correct.
If I am calling, lets say f(), which is defined in foo.c, from main,
then I only need to #include foo.h in main? and not in foo.c? *Or,
only include foo.h in foo.c and not in main? I have been including
them in both ( ie main and foo.c) and it seems duplicative and
unnecessary?

Because you need to call foo() from main.c, you need a declaration for
foo() in main.c's scope, right?

Because you might need, one day (perhaps tomorrow or later on this
afternoon), to call foo() from bar.c, it is convenient to have the foo()
declaration in a header, so that main.c and bar.c can just #include it
rather than you have to type a separate declaration for each. Bear in mind
that the minimal work saving for this becomes rather more significant when
you realise that foo() is really just a shorthand for foo_create(),
foo_destroy(), foo_insert(), foo_delete(), foo_sort(), foo_enquire(),
foo_test(), foo_process(), foo_print(), and whatever other fooish notions
one might entertain. Suddenly, the header starts to look attractive,
right?

Because you might, one day Real Soon Now, modify the implementation of
foo(), it makes sense to make sure that main.c has the right declarations,
right?

Because #including foo.h into foo.c will have the effect of forcing the
compiler to check that the declarations in foo.h match the declarations in
foo.c, #including foo.h into foo.c suddenly starts to look attractive
here, too, right?

--
Richard Heathfield <http://www.cpax.org.uk >
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Thank you Richard.
Jun 27 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
2346
by: Thomas Heller | last post by:
I'm slowly getting tired maintaining py2exe. It is far from perfect, although it has interesting features (I would say). The problem, apart from the work, is that it is good enough for me - I can do everything that I need with it. But I assume I use far less libaries than other Python programmers, so a lot of bugs will never bite me. It is also interesting that the recently introduced bundle-files option, which allows to build...
14
1409
by: Thomas Andersson | last post by:
Hi, I am so so tired of getting this error message when I try to open a web project or create a new one: The web server reported the following error when attempting to create ot open the web project located at the following URL: 'http://localhost/...' A connection with the server could not be established I usually fix it somehow (see my collection of solutions at the bottom of
1
1196
by: Roy | last post by:
Hey all, Yeah, I'm tired of the same old, boring datagrids. Does anyone out there have links on how to "spruce up" a datagrid using style sheets or something else so that they aren't as blocky and well... "gridlike" as they typically are? Thanks, Roy
9
1197
by: Mat | last post by:
i reinstalled everything but the problem still persists. Really, i am tired to restart the IDE every 10 mn. -classes doesn't have constructors but i have 5 constructors -Variable xxx is not declared but variable xxx is declared -Component is private.....etc
1
1638
by: Burganovsky | last post by:
Hi! I'm new in aspnet, and i'm already tired to write MapPath("") for every System.IO operation, where (in config maybe) i can set once, that cwd = server root path, not winnt\system32\? I mean f.e. this: File.OpenRead ("logo.gif") <- this file is in application server root directory i.e. c:\inetpub\wwwroot\aspnetproject\logo.gif Thanks a lot!
1
1603
by: nickjaffe | last post by:
Hi all, I've spent literally hours trying to figure this one out. I'm re-developing an app from vb6 to vb.net, and I cannot get my XML code working. I've just started using .NET. I'm tired, highly annoyed and this is my problem (I know it is very easy for someone who has done this before): My entire XML file is here - The '<CALL_12260>' tag name is dynamic (the XML file is generated by something else), everything else stays
1
1575
by: irresistibleangela2 | last post by:
Are you tired of being a single? Do you feel lonely and boredom in your life. Worry no more! Because we have great news for you!! We have the right site that waits just for you, Aware.Baycouples.com will give you the chance to meet people and experience the excitement of dating someone. Feel the thrill and pleasure here at Aware.Baycouples.com. We guarantee you that once you meet your perfect match; you will surely enjoy and might have...
5
1657
by: BurnTard | last post by:
Hi guys. It's been ages since I tried making anything halfway useful in python, and back when I did, I was almost never successful... Anyways, the thing is: I'm finally finished moving my mp3's from a great heap in a folder called "miscellany", and I'm dead tired of organizing stuff that should have been organized ages ago, and have already been organized on my laptop... What is the matter at the moment is that most mp3's, ripped from cd's...
1
3094
by: ccon67 | last post by:
In a win32 project I have several compile errors for the resource *.rc. The first fews errors look like this error RC2144 : PRIMARY LANGUAGE ID not a number error RC2135 : file not found: MAIN_MENU error RC2164 : unexpected value in RCDATA To overcome all of these errors, I include <windows.hat the top of "resource.h". But verytime the resource editor is touch, a new resource.h is generated and I have to add that line again...I'm...
0
8671
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8598
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9152
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9016
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8887
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8856
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7709
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6515
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
2
2321
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.