C Programming

Fatal error: iostream: No such file or directory in compiling C program using GCC

A header in C is a .h file, which is created and placed at the start of the program to contain shared declarations and macro definitions. This header file must always be at the top of a C program, providing the root information that is later used in the script to compile the program. The header declares the types of instructions and symbols (i.e., data structures, variables, and statements) that appear in the code, which helps the computer’s memory recognize them when the program is compiled.

iostream: No Such File or Directory in Compiling C Program Using GCC

The response to the question “Can we use a C++ header <iostream> in a C program?” is an emphatic “No”. A C++ header such as <iostream> is not compatible with a C program, so it cannot be used. It will generate the error “iostream: No such file or directory”. This error indicates that the library file called iostream was not located in the include directory provided with the GCC compiler that the programmer was using. Programmers should be aware of the distinctions between C and C++. Writing code that is not compatible with the language it is written in will cause the compiler to fail and the program will not execute properly.

A header file is a kind of file that contains declarations and definitions of functions and types in addition to being included within another file. It is important to remember that C and C++ header files are not necessarily interchangeable. In the C language, header files generally have the .h file extension, such as “stdio.h”, “math.h”, and “stdlib.h”, whereas in C++ header files have the .hpp extension, such as “iostream.hpp” and “string.hpp” or without .hpp such as “iostream”.

The <iostream> header file is not valid in C and if you try to add it in C code, you will get an error.

Code with Incorrect Header

#include <iostream>
int main() {
    int number;
    printf("Enter an integer: ");
    scanf("%d", &number);
    printf("You entered: %d", number);
    return 0;
}

In the above code, an error “iostream: No such file or directory” is generated as we are using the header <iostream> in a C file.

Output

If you are using C language, you have to add <stdio.h> header instead of “iostream” to fix the error.

Code with Correct Header

#include <stdio.h>
int main() {
    int number;
    printf("Enter an integer: ");
    scanf("%d", &number);
    printf("You entered: %d", number);
    return 0;
}

In the above code, we are using the appropriate header i.e., <stdio.h> for the C Program, so the output is generated.

Conclusion

We cannot use a C++ header <iostream> in a C program. This is because C and C++ are two different languages with different syntaxes and libraries. In C, the header file supporting the functions provided by <iostream> is “stdio.h”. Furthermore, there are other notable differences between the two languages, such as the support for object-oriented programming in C++.

About the author

Hiba Shafqat

I am a Computer Science student and a committed technical writer by choice. It is a great pleasure to share my knowledge with the world in which I have academic expertise.