1

I need to know what is meant by "Header" in C language? is it:

  • an area in the code, like header area in HTML code (a title> or declaration area? or definition area?, or kind of)?
  • or it is like a function (can be: function, or sub routine, or a scope, or kind of) that can be called.
alk
  • 69,737
  • 10
  • 105
  • 255
  • 1
    I'm not exactly sure what kind of answer you're looking for here. "Header" is not actually a formal language-level concept in C, it's just a convention around how to use `#include`. Are you asking what that convention is, or what `#include` is? – Oliver Charlesworth Aug 14 '16 at 12:38
  • Just a bunch of definitions (instructions) for the compiler, typically declared in an `h` file. No area in the code is generated for these instructions, unless you explicitly declare a variable or implement a function in that header file (which is conceptually wrong and should be avoided). – barak manos Aug 14 '16 at 12:40
  • @barakmanos: you can have inline functions or macro in header too. – Florian Burel Aug 14 '16 at 12:42
  • @FlorianBurel: For inline, you are correct in the fact that they can be implemented in the header file. For macros - it is still "copy-pasted" (if you will) into the source file. The compiler does not generate object code for a macro in the header file. In fact, same thing for `inline` functions. If there is no source file which includes that header file, then no object code will be generated for these functions. – barak manos Aug 14 '16 at 12:43
  • @barakmanos: Emphasising "*conceptually*", as from the language perceptive itself a "header" file is treated the same as any other C source file. – alk Aug 14 '16 at 12:48
  • @alk: You're right. In most IDE's perceptive, default behavior is that header files are not compiled. – barak manos Aug 14 '16 at 12:49
  • @barakmanos: IDE? What IDE? ;-) – alk Aug 14 '16 at 12:51
  • @alk: You're favorite one (Turbo-C I suppose). – barak manos Aug 14 '16 at 12:54
  • @OliverCharlesworth yes I'm asking about the convention.. i need to understand the meaning of header. – Yassir Rabiea Aug 14 '16 at 12:55
  • https://en.wiktionary.org/wiki/header – Some programmer dude Aug 14 '16 at 12:59
  • thanks..but I need "Header" only not the header file.. look, when we say source code we mean the lines of language that perform the program so source file is the container that contains the source code. Now I need to know "Header" then a header file is a container that contains "header". I need to understand that. @OliverCharlesworth – Yassir Rabiea Aug 15 '16 at 19:48

4 Answers4

2

A header is a convention generally accepted by C programmers.

It is a usually a .h file which is included into C source files which provides several benefits.

1.- Provides declaration of data types, global variables, constants and functions.

So you don't have to rewrite them time and again. And if they need being changed you just need to change it in a single file.

In example this program composed of two compliation units (two .c files) compiles and runs just fine.

// File funcs.c
#include <stdio.h>
struct Position {
  int x;
  int y;
};

void print( struct Position p ) {
  printf("%d,%d\n", p.x, p.y );
}

// File main.c
struct Position {
  int x;
  int y;
};

int main(void) {
  struct Position p;
  p.x = 1; p.y = 2;
  print(p);
}

But it is more mantainable to have the declaration for the struct Position in a header file and just #include it everywhere it is needed, like this :

// File funcs.h
#ifndef FUNCS_H
#define FUNCS_H
struct Position {
  int x;
  int y;
};
#endif // FUNCS_H

//File funcs.c
#include <stdio.h>
#include "funcs.h"

void print( struct Position p ) {
  printf("%d,%d\n", p.x, p.y );
}

//File main.c
#include "funcs.h"
int main(void) {
  struct Position p;
  p.x = 1; p.y = 2;
  print(p);

2.- Provides some type safety.

C features implicit declaration of functions. A "feature" (or rather an arguable language design mistake) which was fixed in C99.

Consider this program composed of two .c files :

//File funcs.c
#include<stdio.h>
int f(long n)
{
  n = n+1;
  printf("%ld\n", n );  
}

// File main.c
int main(void)
{
  f("a");
  return 0;
}

With gcc this program compiles without warnings or errors. But does not behave as we could reasonable expect and desire :

jose@cpu ~/t/tt $ gcc -o test *.c
jose@cpu ~/t/tt $ ./test 
4195930

Using a header file like this :

//File funcs.h
#ifndef FUNCS_H
#define FUNCS_H
int f(long n);
#endif // FUNCS_H

//File funcs.c
#include<stdio.h>
int f(long n) {
  n = n+1;
  printf("%ld\n", n );  
}

// File main.c
#include"funcs.h"
int main(void) {
  f("a");
  return 0;
}

The program still compiles and works wrong but at least we get a warning :

jose@cpu ~/t $ gcc -o test *.c
main.c: In function 'main':
main.c:5:5: warning: passing argument 1 of 'f' makes integer from pointer without a cast
   f("a");
     ^
In file included from main.c:2:0:
funcs.h:3:5: note: expected 'long int' but argument is of type 'char *'
 int f(long n);
     ^
jose@cpu ~/t $ ./test
4195930

3.- Provide a public interface while letting the implementation details remain hidden.

When you design your program it is desirable to make it modular. That is to ensure that different parts of it (modules) are as independient as possible. So that when you need to make a change to one module you need not be worried about such change affecting other modules

Headers help in doing this because you put in the header of a modules the data structures, function prototypes and constants that will be needed by the users of such module.

The implementation details go into .c files.

That is how libraries work. The API interface is specified and distributed in header files. But the API code is in .c files which don't need to be distributed. As an user of the library you just need the headers and the compiled library, not its source code.

Anonymous Coward
  • 3,140
  • 22
  • 39
  • 1+ not just for mentioning "*interface*". :-) – alk Aug 14 '16 at 13:29
  • thanks..but I need "Header" only not the header file.. look, when we say source code we mean the lines of language that perform the program so source file is the container that contains the source code. Now I need to know "Header" then a header file is a container that contains "header". I need to understand that. – Yassir Rabiea Aug 15 '16 at 19:44
  • 1
    Under the coding convention I follow all C headers are C header files and all C header files are C headers. Thus under such convention a C header file is not a container containing a header; the file IS the header. And the header (which is a file) contains types, constants, variables and function prototypes; it does not contain headers, though it may #include other headers. – Anonymous Coward Aug 17 '16 at 11:27
  • then we can say that they called "header" because they are in the top of the code, or they are titles to a file (header file) that contains a specific code..@JoseAntonioDuraOlmos – Yassir Rabiea Aug 21 '16 at 14:48
  • Indeed, that is most likely where the name comes from. Headers are usually included at the top of a file, hence header because it usually goes in the head. Though this is not mandatory. And there common use cases where headers are not the first item of a C source file; you may find before them comments describing the purpouse of the file or with copyright information and it is also common to put defines before including headers in order to configure how those headers behave. – Anonymous Coward Aug 21 '16 at 19:15
1

What is C header Linguistically?

Referring linguistically I'd say a C header file describes the interface as provided by a translation unit, namely the accompanying C file.

"interface" by means of

  • types
  • constants
  • (global) variables
  • functions

Not all of the above need to be part of a C header.

C headers are optional from the C language's perspective, but by convention they are expected to exist.

alk
  • 69,737
  • 10
  • 105
  • 255
  • thanks..but I need "Header" only not the header file.. look, when we say source code we mean the lines of language that perform the program so source file is the container that contains the source code. Now I need to know "Header" then a header file is a container that contains "header". – Yassir Rabiea Aug 15 '16 at 19:43
0

Usually when talking about "headers" in C, what is meant is the files you include with the #include preprocessor directive. Those are called header files.

It could also be other things, like a comment at the top of a source (or header file) could be header. A comment before a function could be a function header.

Or when reading data files or data over the internet, many protocols have the data split into a header and the actual data (see e.g. HTTP).

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
0

a header is a file with the .h extension. It is meant to help the compiler to understand the symbol (method, function, struct, variable...) that are used within the code. See of it like an glossary at the end of a book. It is used solely for development purpose. It helps develloper knowing what function are available without having to look in all the implementation file. Like a man page.

A search on google? http://www.tutorialspoint.com/cprogramming/c_header_files.htm :)

Florian Burel
  • 3,408
  • 1
  • 19
  • 20
  • "*It is not compile, not present in the release binary, and is used solely for development purpose.*" well, no. – alk Aug 14 '16 at 12:50
  • @alk : Care to correct me? I'll gladly correct my answer if you could explain yourself. – Florian Burel Aug 14 '16 at 12:57
  • Although *conceptually* headers won't carry implementations they are very well part of the compilation process. Also their "content" not necessarily would lack in production builds. – alk Aug 14 '16 at 13:06
  • You are right, they are part of the compilation. The compiler and possibly linkers uses them to produce the compiled product. What I meant is that most .c files carry the implementation details and it's them who are turned in assembly, it's clear in my mind but I realize it's not actually accurate. As for the production build, It has always appear to me that a extra step (automatic nowadays) was needed to add them to the library when building, well, libs... so I assumed... – Florian Burel Aug 14 '16 at 13:14
  • Headers get included ***prior*** to compilation, so to the compiler it's just one big file that gets compiled. – alk Aug 14 '16 at 13:27
  • 1
    `cc -c someheader.h -o someheader.o` works pretty well. Depends what you put in there. Any answer needs to distinguish between *rule* and *convention* – tofro Aug 14 '16 at 13:57
  • thanks..but I need "Header" only not the header file.. look, when we say source code we mean the lines of language that perform the program so source file is the container that contains the source code. Now I need to know "Header" then a header file is a container that contains "header". I need to understand that. @FlorianBurel – Yassir Rabiea Aug 15 '16 at 19:45
  • In C (or objective C, or C++) header means header file (.h) ... there is no such thinks as header like in "header - title - body - footer" as you can have in html or printing. – Florian Burel Aug 15 '16 at 22:11
  • @FlorianBurel, thank you v.much.. it is begun to be clear.. thanks.. although I knew the process, I still wondering why the call it header not module (for example).. I suppose they meant the fist thing that translated (first thing the compiler begins with) or they called it header because it always written in the beginning of the code!!!.. thank you a lot and if you can help further please do... – Yassir Rabiea Aug 16 '16 at 13:46
  • "*... they called it header because it always written in the beginning of the code*" exactly this is the reason for the name, I strongly suspect. @YassirRabiea – alk Aug 20 '16 at 08:03