- C Programming Tutorial
- C - Home
- Basics of C
- C - Overview
- C - Features
- C - History
- C - Environment Setup
- C - Program Structure
- C - Hello World
- C - Compilation Process
- C - Comments
- C - Tokens
- C - Keywords
- C - Identifiers
- C - User Input
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Integer Promotions
- C - Type Conversion
- C - Type Casting
- C - Booleans
- Constants and Literals in C
- C - Constants
- C - Literals
- C - Escape sequences
- C - Format Specifiers
- Operators in C
- C - Operators
- C - Arithmetic Operators
- C - Relational Operators
- C - Logical Operators
- C - Bitwise Operators
- C - Assignment Operators
- C - Unary Operators
- C - Increment and Decrement Operators
- C - Ternary Operator
- C - sizeof Operator
- C - Operator Precedence
- C - Misc Operators
- Decision Making in C
- C - Decision Making
- C - if statement
- C - if...else statement
- C - nested if statements
- C - switch statement
- C - nested switch statements
- Loops in C
- C - Loops
- C - While loop
- C - For loop
- C - Do...while loop
- C - Nested loop
- C - Infinite loop
- C - Break Statement
- C - Continue Statement
- C - goto Statement
- Functions in C
- C - Functions
- C - Main Function
- C - Function call by Value
- C - Function call by reference
- C - Nested Functions
- C - Variadic Functions
- C - User-Defined Functions
- C - Callback Function
- C - Return Statement
- C - Recursion
- Scope Rules in C
- C - Scope Rules
- C - Static Variables
- C - Global Variables
- Arrays in C
- C - Arrays
- C - Properties of Array
- C - Multi-Dimensional Arrays
- C - Passing Arrays to Function
- C - Return Array from Function
- C - Variable Length Arrays
- Pointers in C
- C - Pointers
- C - Pointers and Arrays
- C - Applications of Pointers
- C - Pointer Arithmetics
- C - Array of Pointers
- C - Pointer to Pointer
- C - Passing Pointers to Functions
- C - Return Pointer from Functions
- C - Function Pointers
- C - Pointer to an Array
- C - Pointers to Structures
- C - Chain of Pointers
- C - Pointer vs Array
- C - Character Pointers and Functions
- C - NULL Pointer
- C - void Pointer
- C - Dangling Pointers
- C - Dereference Pointer
- C - Near, Far and Huge Pointers
- C - Initialization of Pointer Arrays
- C - Pointers vs. Multi-dimensional Arrays
- Strings in C
- C - Strings
- C - Array of Strings
- C - Special Characters
- C Structures and Unions
- C - Structures
- C - Structures and Functions
- C - Arrays of Structures
- C - Self-Referential Structures
- C - Lookup Tables
- C - Dot (.) Operator
- C - Enumeration (or enum)
- C - Structure Padding and Packing
- C - Nested Structures
- C - Anonymous Structure and Union
- C - Unions
- C - Bit Fields
- C - Typedef
- File Handling in C
- C - Input & Output
- C - File I/O (File Handling)
- C Preprocessors
- C - Preprocessors
- C - Pragmas
- C - Preprocessor Operators
- C - Macros
- C - Header Files
- Memory Management in C
- C - Memory Management
- C - Memory Address
- C - Storage Classes
- Miscellaneous Topics
- C - Error Handling
- C - Variable Arguments
- C - Command Execution
- C - Math Functions
- C - Static Keyword
- C - Random Number Generation
- C - Command Line Arguments
- C Programming Resources
- C - Questions & Answers
- C - Quick Guide
- C - Cheat Sheet
- C - Useful Resources
- C - Discussion
Header Files in C
The #include preprocessor directive is used to make the definitions of functions, constants and macros etc. from one file, usually called as a header file, available for use in another C code. A header file has ".h" extension from which you can include the forward declarations of one or more predefined functions, constants, macros etc. The provision of header files in C facilitates a modular design of the program.
System Header Files
The C compiler software is bundled with many pre-compiled header files. These are called system header files. A well-known example is "stdio.h" – a header file included in almost every C program.
Each of the system header files contains a number of utility functions. These functions are often called library functions. For example, printf() and scanf() functions, needed for performing IO operations, are the library functions available in the "stdio.h" header file.
The preprocessor statements that load one or more header files are always in the beginning of the C code. We start our journey to learn C programming with a basic "Hello World" program that starts with including the "stdio.h" file −
#include <stdio.h> int main() { /* my first program in C */ printf("Hello, World! \n"); return 0; }
The C preprocessing directive #include basically is a request to the compiler to load the contents of a specific header file, so that they can be used in the program.
A usual practice in C or C++ programs is that we keep all the constants, macros, system wide global variables, and function prototypes in the header files and include that header file wherever it is required.
Syntax to Include Header Files in C
A header file is loaded with the #include directive. Its usage follows either of the following two methods −
#include <filename.h>
The name of the header file put inside angular brackets if it is available in system/default directory.
#include "filename.h"
The name of the header file put inside double quotation marks for user defined or non-standard header files available in same directory as source file
The #include directive works by directing the C preprocessor to scan the specified file as input before continuing with the rest of the current source file. The output from the preprocessor contains the output already generated, followed by the output resulting from the included file, followed by the output that comes from the text after the #include directive.
Standard Header Files in C
A typical C compiler is bundled with a number of pre-compiled header files. Each header file contains the set of predefined standard library functions. The "#include" preprocessing directive is used to include the header files with ".h" extension in the program.
Here is the table that displays some of the header files in C language −
Header Files | Description | Functions/macros/variables |
---|---|---|
stdio.h | Input/Output functions | scanf(), printf(), fopen(), FILE |
stdlib.h | General utility functions | atoi(), atof(), malloc() |
math.h | Mathematics functions | sin(), cos(), pow(), sqrt() |
string.h | String functions | strcpy(), strlen(), strcat() |
ctype.h | Character handling functions | isalpha(), isupper(), ispunct() |
time.h | Date and time functions | asctime(), gmtime(), mktime() |
float.h | Limits of float types | FLT_ROUNDS, FLT_RADIX, |
limits.h | Size of basic types | CHAR_BIT, CHAR_MIN, CHAR_MAX |
wctype.h | Functions to determine the type contained in wide character data. | iswalpha(), iswctype(),iswupper() |
Example
A few of the library functions from some header files are used in the code below −
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <math.h> int main() { char s1[20] = "53875"; char s2[10] = "Hello"; char s3[10] = "World"; int res; res = pow(8, 4); printf("Using math.h, The value is : %d\n", res); long int a = atol(s1); printf("Using stdlib.h, the string to long int: %d\n", a); strcpy(s2, s3); printf("Using string.h, the strings s2 and s3: %s\t%s\n", s2, s3); return 0; }
Output
When you run this code, it will produce the following output −
Using math.h, The value is: 4096 Using stdlib.h, the string to long int: 53875 Using string.h, the strings s2 and s3: World World
User-defined Header Files
We can add one or more user-defined functions (apart from the main() function) in the C program. However, if the code consists of a large number of function definitions, putting them in a single source code file with ".c" extension becomes difficult to handle and maintain. Hence, functions/macros/variables of similar nature are clubbed together in header files, and included as we include the standard header files, and call the functions defined in them.
The user-defined header files are usually placed in the same directory in which the C source code is present.
The procedure to create and use a header file with CodeBlocks IDE has been described below. Start CodeBlocks IDE and create a new Console Application −
Choose a suitable name to the project. Add a new empty file in the folder, and save the following code as "myheader.h" −
#ifndef MYHEADER_H #define MYHEADER_H void sayHello(); int add(int a, int b); double area(double radius); int length(char *x); #endif
If a header file happens to be included twice, the compiler will process its contents twice and it will result in an error. The standard way to prevent this is to enclose the entire real contents of the file in a conditional definition with #ifndef directive, known as a header guard.
The header guard checks whether the header file has been defined or not. If it's not defined, it means the file is being included for the first time, and the code inside the guard will be processed.
The header file contains the forward declarations or the prototypes of the functions to be included. The actual definition of these functions is provided in a ".c" file with the same name as the header file.
Creating a Header File
Save the following code in "myheader.c" file −
#include <stdio.h> #include <string.h> #include <math.h> #define PI 3.142 void sayHello(){ printf("Hello World\n"); } int add(int a, int b){ int result; result = a+b; return result; } double area(double radius){ double areaofcircle = PI*pow(radius, 2); return areaofcircle; } int length(char *x){ return strlen(x); }
Using the Header File
We can now include the "myheader.h" file in the program and call any of the above functions. Save the following code as "main.c" in the project folder.
#include <stdio.h> #include "myheader.h" int main() { int p = 10, q = 20; double x = 5.25; sayHello(); printf("sum of %d and %d is %d\n", p, q, add(p,q)); printf("Radius: %lf Area: %lf", x, area(x)); return 0; }
Build the project and run "main.c", either from the run menu of CodeBlocks IDE or from the command-line to get the following result −
Hello World sum of 10 and 20 is 30 Radius: 5.250000 Area: 86.601375
Computed Includes
Sometimes it is necessary to select one of the several different header files to be included into your program. For instance, they might specify configuration parameters to be used on different sorts of operating systems. You could do this with a series of conditionals as follows −
#if SYSTEM_1 # include "system_1.h" #elif SYSTEM_2 # include "system_2.h" #elif SYSTEM_3 ... #endif
But as it grows, it becomes tedious, instead the preprocessor offers the ability to use a macro for the header name. This is called a computed include. Instead of writing a header name as the direct argument of #include, you simply put a macro name there −
#define SYSTEM_H "system_1.h" ... #include SYSTEM_H
SYSTEM_H will be expanded, and the preprocessor will look for system_1.h as if the #include had been written that way originally. SYSTEM_H could be defined by your Makefile with a -D option.