473,769 Members | 7,058 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

header files

Iam just trying to know "c".
And I have a small doubt about these header files.
The header files just contain the declaration part...Where is the
definition for these declarations written??And how does that get
linked to our program when we run it??I would appreciate any helpful
info..And I would like to thank you for that in advance
-ambika
Nov 13 '05 #1
11 5605
In article <bi************ *@news.t-online.com>,
Martin Dickopp <ex************ *@zero-based.org> wrote:
Your question is not about the C /language/, which is what comp.lang.c is
about, or the C /standard/, which is what comp.std.c is about. It is
therefore off-topic in both groups. Please ask in a newsgroup dedicated to
(software development on) your system.


I interpreted his question as generic curiosity, not specific to any
particular system. What's the group dedicated to "how computer systems
typically work"?

--
Barry Margolin, ba************@ level3.com
Level(3), Woburn, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.
Nov 13 '05 #2
Barry Margolin <ba************ @level3.com> writes:
In article <bi************ *@news.t-online.com>,
Martin Dickopp <ex************ *@zero-based.org> wrote:
Your question is not about the C /language/, which is what comp.lang.c is
about, or the C /standard/, which is what comp.std.c is about. It is
therefore off-topic in both groups. Please ask in a newsgroup dedicated to
(software development on) your system.
I interpreted his question as generic curiosity, not specific to any
particular system.


So did I. But the answer is different for different systems, therefore an
answer which is not specific to any particular system is impossible.
What's the group dedicated to "how computer systems typically work"?


It depends on the system. If the system is a variant of Unix,
comp.unix.progr ammer will likely answer the OP's question. Is the system is
Microsoft Windows, it is highly likely that a group dedicated to software
development on that platform exits, but I cannnot recommend any as I don't
have any experience or knowledge in that field (haven't used a Microsoft
product in the last ten years).

Of course, if the OP uses something obscure like a C interpreter on a cell
phone, it might in fact be the case that no newsgroup for that system
exists...

Martin
Nov 13 '05 #3
In article <bf************ *************@p osting.google.c om>, ambika
<in*******@yaho o.com> writes
Iam just trying to know "c".
And I have a small doubt about these header files.
The header files just contain the declaration part...Where is the
definition for these declarations written??And how does that get
linked to our program when we run it??I would appreciate any helpful
info..And I would like to thank you for that in advance


They are provided by implementation files, object files or libraries
which the linker accesses to create the executable.
--
Francis Glassborow ACCU
64 Southfield Rd
Oxford OX4 1PA +44(0)1865 246490
All opinions are mine and do not represent those of any organisation
Nov 13 '05 #4
ambika wrote:
The header files just contain the declaration part...Where is the
definition for these declarations written??And how does that get
linked to our program when we run it??


Usually the definitions for the external functions and
objects are contained in one or more "object libraries"
which must be linked with the object modules produced
by compiling your program's source code. The standard
headers correspond to the system's standard C library,
which is usually linked with your program by default,
so that you may be unaware of it. Other libraries are
provided by, and spcific to, the development platform
or a third-party support-software provider. You can
create your own libraries of reusable functions, too.

Nov 13 '05 #5
Thanks to all of you
..
But I have a small question.I tried to save a program in C with the
".h"extenti on.I did not include the header files.I just wrote a few
functions.Then i included the file as what we do include our header
files.It just worked fine.Here I have both declared and defined the
functions.
But when I saved the same program with ".C" extention and include that
file in the program it also works the same way...
Have the really succeeded in saving my program as a header file in
the first case???
Am asking this because both the defn and declaration is done there
itself..and no linking is required in this case and that does not make
any diff b/w my ".h" and ".c" files when included.
Nov 13 '05 #6
In article <bf************ **************@ posting.google. com>,
ambika <in*******@yaho o.com> wrote:
Thanks to all of you
.
But I have a small question.I tried to save a program in C with the
".h"extention. I did not include the header files.I just wrote a few
functions.Th en i included the file as what we do include our header
files.It just worked fine.Here I have both declared and defined the
functions.
But when I saved the same program with ".C" extention and include that
file in the program it also works the same way...
Have the really succeeded in saving my program as a header file in
the first case???
Am asking this because both the defn and declaration is done there
itself..and no linking is required in this case and that does not make
any diff b/w my ".h" and ".c" files when included.


The #include directive doesn't care whether you name the file .C or .h or
anything else. The file named in the directive is simply treated as if it
were in the file that contains that line (there are some small differences,
but they're not relevant to your question).

The normal procedure of putting declarations in .h files and definitions in
..c files is a software engineering convention, not something dictated by
the language. It's useful when different parts of an application are
developed by different organizations.

For instance, since commercial OS and library vendors often don't want to
supply source code of their programs, you can't use the technique of
including the source file. They provide a binary library and a header file
that contains only the declarations of their functions.

--
Barry Margolin, ba************@ level3.com
Level(3), Woburn, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.
Nov 13 '05 #7
Barry Margolin wrote:

In article <bf************ **************@ posting.google. com>,
ambika <in*******@yaho o.com> wrote:
Thanks to all of you
.
But I have a small question.I tried to save a program in C with the
".h"extention. I did not include the header files.I just wrote a few
functions.Th en i included the file as what we do include our header
files.It just worked fine.Here I have both declared and defined the
functions.
But when I saved the same program with ".C" extention and include that
file in the program it also works the same way...
Have the really succeeded in saving my program as a header file in
the first case???
Am asking this because both the defn and declaration is done there
itself..and no linking is required in this case and that does not make
any diff b/w my ".h" and ".c" files when included.


The #include directive doesn't care whether you name the file .C or .h or
anything else. The file named in the directive is simply treated as if it
were in the file that contains that line (there are some small differences,
but they're not relevant to your question).


That's correct, but are a great many tools out there which assume that a
*.c file is C source code, and that a *.h file is a C header file.
Violating that convention is certainly legal, but may prove
inconvenient.
Nov 13 '05 #8
in*******@yahoo .com (ambika) wrote in message news:<bf******* *************** ****@posting.go ogle.com>...
Thanks to all of you
.
But I have a small question.I tried to save a program in C with the
".h"extenti on.I did not include the header files.I just wrote a few
functions.Then i included the file as what we do include our header
files.It just worked fine.Here I have both declared and defined the
functions.
But when I saved the same program with ".C" extention and include that
file in the program it also works the same way...
Have the really succeeded in saving my program as a header file in
the first case???
Am asking this because both the defn and declaration is done there
itself..and no linking is required in this case and that does not make
any diff b/w my ".h" and ".c" files when included.


The whole matter of using .h to mark a file as a header file and .c to
mark a file as a source file is a matter of convention, not a
requirement of the C language itself. As far as the C language is
concerned, you can include a .h file, or a .c file, or anything else,
as long as the contents of that file are legal C code. You don't even
have to #include any files at all if you aren't using any functions
defined outside of your particular program.

It is true that many development tools make the distinction between .h
and .c files, but that's because the convention has become so common
over the years. Also, not all platforms even allow you to name a file
so that it has a .h or .c extension. HP's MPE file system uses the
naming convention file.account.us er, so if I created a header file
containing types and prototypes for a networking module, the include
directive would look something like

#include "netdfs.dev.jbo de"

The standard library headers (stdio.h, stdlib.h, math.h, etc.) are
treated as special cases, so that you can use

#include <stdio.h>

on any system, including systems like MPE where stdio.h is not a legal
filename, and the right file contents will be loaded.

It is the generally accepted practice that #include files only contain
macro definitions, type definitions, and function prototypes. In
other words, these files define the *interface* to a module or
library, with the implementation of that module or library appearing
in separate source files. The primary reasons for doing this are that
it makes large projects easier to manage, and permits the same code to
be used easily in multiple projects. You break the project down into
a number of modules, implement each module in one or more source
files, compile each module into separate object files or libraries,
and define an interface to each module so other modules may use it.
Each module can be compiled and tested separately, and changes made to
any one module will have little or no effect on other modules if no
changes to the interface are necessary. If you write a
general-purpose module, it can be linked into a number of projects
(just as almost every C program uses printf() or fopen()).

There's no language requirement saying you *can't* put function
definitions in an included file, but there are several good reasons
for not doing so. First of all, function names are exported to the
linker unless you declare them as "static", so if you link two files
together that include the same function definition, you may get a
linker error.

For example, say you have the following files:

/* foo.h */
int foo (void)
{
return 1;
}

/* bar.c */
#include <stdio.h>
#include "foo.h"

int bar (void)
{
int x = foo();
return x;
}

/* main.c */
#include <stdio.h>
#include "foo.h"

int main (void)
{
extern int bar (void);
printf ("bar() returns %d\n", bar());
printf ("foo() returns %d\n", foo());
return 0;
}

Here's what happens when I try to build this in Cygwin using gcc:

jbode@JBODE-2K ~/cstuff/badpractice
$ gcc -o bad main.c bar.c
/cygdrive/c/WINDOWS/TEMP/ccYJ0xkP.o(.tex t+0x0):bar.c: multiple
definition of `foo'
/cygdrive/c/WINDOWS/TEMP/ccjj5LwY.o(.tex t+0x0):main.c: first defined
here
collect2: ld returned 1 exit status

The ld linker is complaining because the *definition* of the function
foo() appears in both main.c and bar.c. It's not smart enough to know
that the function is the same in both files, and that one of the
definitions can be safely ignored. You don't want to put global
variable definitions in included files for the same reason.

Another reason for not doing this is that the included file has to be
parsed and compiled every time it appears. So if you include a
hundred-line function in 20 different source files, that same function
has to be parsed and compiled 20 times. And if that function
definition changes, *all* the files that include it must be
recompiled.

Here's the right way to structure the example given above:

/* foo.h -- defines the interface to foo() */
#ifndef FOO_H /* These two lines prevent a file from being included*/
#define FOO_H /* more than once in the same translation unit */

extern int foo (void);

#endif

/* foo.c -- implements the function foo() */
#include "foo.h"

int foo (void)
{
return 1;
}

/* bar.h -- defines interface to bar() */
#ifndef BAR_H
#define BAR_H

extern int bar (void);

#endif

/* bar.c -- implements function bar() */
#include "bar.h"
#include "foo.h" /* remember this is the *prototype*, not the
definition */

int bar (void)
{
int x = foo();
return x;
}

/* main.c -- calls foo() and bar() functions */
#include <stdio.h>
#include "foo.h"
#include "bar.h"

int main (void)
{
printf ("foo() = %d\n", foo());
printf ("bar() = %d\n", bar());
return 0;
}

Now, if I make any changes to foo.c, all I have to do is recompile
that one file and relink; I don't have to recompile bar.c and main.c.
Nov 13 '05 #9
In article <43************ *************@p osting.google.c om>, John
Bode wrote:
in*******@yahoo .com (ambika) wrote in message
news:<bf******* *************** ****@posting.go ogle.com>...
But I have a small question.I tried to save a program in C with the
".h"extenti on.I did not include the header files.I just wrote a few
functions.Then i included the file as what we do include our header
files.It just worked fine.Here I have both declared and defined the
functions.


The whole matter of using .h to mark a file as a header file and .c to
mark a file as a source file is a matter of convention, not a
requirement of the C language itself.


I like to call my C source files .a and my header files .dll.
Ha HAH!

--
Neil Cerutti
Nov 13 '05 #10

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

Similar topics

3
2228
by: Chris Mantoulidis | last post by:
Seperate compilation (that's what it's called, right?) seems to be quite popular, so I decided to get some info about it, and (d'oh) use it... But it's whole structure seems weird to me... Here's what I think of how it is (from what I've read): THE PROJECT +1st header file
21
2618
by: Hattuari | last post by:
I'm learning C++ after having spent several years in the computer industry doing both system administration and engineering. I've written code in Perl, Bash, Pascal, Ada, C, Mathematica (hundreds of lines of OO code, not 1+1), JavaScript, Lisp, and Java, as well as C++. Each of these languages has it's strengths and weaknesses. My experience with C++ has shown me that it is by far the most difficult for me to learn. I have the sense...
16
12549
by: matthurne | last post by:
I just started learning C++ on my own...I'm using Accelerated C++. Something it hasn't explained and I keep wondering about is how header files actually work. I suspect it doesn't get into it because it is, as the authors love to say, "implementation specific". If that's the case, how does the compiler commonly handle them? I use Linux and gcc specifically. Basically, I don't understand how a header file being included makes a...
11
2763
by: Steven T. Hatton | last post by:
In the past there have been lengthy discussiions regarding the role of header files in C++. People have been very adamat about header files serving as in interface to the implementation. I do understand the objective. This example may represent an interface in need of a bit of refactoring, but it goes to demonstrate the basic idea as I understand it. http://developer.kde.org/documentation/library/cvs-api/kdevelop/html/ast_8h-source.html...
3
3097
by: pooja | last post by:
Suppose i have created a class c1 with f1()in c1.cpp and included this c1.cpp in file1.cpp file , which is also having main() by giving the statement #include "c1.cpp". the same i can do by using header file. i can create a class c1 with f1() in c1.h and include this c1.h in file1.cpp by giving the statement #include "c1.h" tell me that what exactly is the difference between c1.h and c1.cpp? Since they both are doing the same things.
18
2750
by: John Smith | last post by:
Hi all What does the group think of the practise of including one header file from inside another? I have some legacy code where this has been done, and it creates a dependency on a module (collection of files) which are not required, except for one header file's contents. I'd say 'No, header files should be included in the C source, not in another
8
2785
by: ginnisharma1 | last post by:
Hi All, I am very new to C language and I got really big assignment in my work.I am wondering if anyone can help me.........I need to port compiler from unix to windows and compiler is written partially in c and partially in fortran. I guess i need to change host specific files to make it working. I wonder if standard header files are going to change in this case.my current windows compiler doesn't have sys/resource.h but unix compiler...
9
4043
by: chat | last post by:
Hi, every body. I have 3 files like this: -------------------------------------------------------- file name : header.h #ifndef TEST_H #define TEST_H int a=1; double b=0.5;
36
3854
by: zouyongbin | last post by:
Stanley B Lippman in his "C++ Primer" that a definition like this should not appear in a header file: int ix; The inclusion of any of these definitions in two or more files of the same program will result in a linker error complaining about multiple definitions. So this kind of definition should be avoided as much as possible. But as we know, the definition of a class is always in a header file. And we can use "#ifndef" to eliminate...
0
9589
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...
1
9997
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
8873
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
7413
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...
0
6675
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5309
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5448
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3965
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.