MCS-011 Problem Solving and Programming

First year, Semester 1

Chapters

Newsletter

Reading from Other files using #include

The #include directive in C is used to include the contents of one file into another during the preprocessing phase of compilation. This allows for modular programming and code reuse, enabling you to separate code into multiple files and include them as needed.

Syntax

#include <filename>

or

#include "filename"

here, Angle Brackets < >: Used for standard library headers or system headers, Double Quotes " ": Used for user-defined or local header files.


Example

Including Standard Library Header

#include <stdio.h>

This includes the standard input/output library, allowing you to use functions like printf and scanf.

or

Including a Local Header

#include "myheader.h"

This includes a local file named myheader.h from the same directory or a specified include path.


Example of #include

File: main.c

#include <stdio.h>
#include "myfunctions.h" int main() { printHello(); return 0; }

File: myfunctions.h

#ifndef MYFUNCTIONS_H
#define MYFUNCTIONS_H void printHello(); #endif

File: myfunctions.c

#include <stdio.h>
#include "myfunctions.h" void printHello() { printf("Hello, World!\n"); }

Using #include effectively helps in organizing code, improving readability, and enabling code reuse across different parts of a project.

Report an issue

Reporting: Reading from Other files using #include (topic)

Related Posts