Difference between #include and #include" " in C/C++ with Examples - GeeksforGeeks

08 Dec, 2021

Pre-requisites: Header files in C/ C++ and its uses
The difference between the two types is in the location where the preprocessor searches for the file to be included in the code.
#include <filename>  // Standard library header
#include “filename”  // User defined header
#include<filename>
#include<> is for pre-defined header files. If the header file is predefined then simply write the header file name in angular brackets. 
Example:
#include <iostream>#include <stdio.h>#include <stdlib.h>
The preprocessor searches in an implementation-dependent manner, normally in search directories pre-designated by the compiler/IDE. This means the compiler will search in locations where standard library headers are residing. The header files can be found at default locations like /usr/include or /usr/local/include. This method is normally used to include standard library header files.
Example: Below is the C++ program to demonstrate the above concept:
C
 

 
 










// C program to demonstrate  
// the above concept 
#include <stdio.h> 
  
// Driver code 
int main()  
{ 
  printf("GeeksforGeeks | "); 
  printf("A computer science portal for geeks"); 
  return 0; 
} 






Output:
GeeksforGeeks | A computer science portal for geeks
#include “FILE_NAME”
#include ” “ is for header files that programmer defines. If a programmer has written his/ her own header file, then write the header file name in quotes. 
Example:
#include “mult.h” Here, mul.h is header file written by programmer.
The preprocessor searches in the same directory as the file containing the directive. The compiler will search for these header files in the current folder or -I defined folders. This method is normally used to include programmer-defined header files. 
mul.h Header file:
// mul.hint mul(int a, int b){    return(a * b);}
Below is the C program to include and use the header file mul.h:
C
 

 
 










// C program to demonstrate  
// the above approach 
  
// Include user-defined  
// header file 
#include "mul.h" 
  
// Driver code 
int main() 
{ 
  int a = 10; 
  int b = 20; 
    
  // Invoke the function defined in 
  // header file 
  int c = mul(a, b); 
  printf("%d", c); 
  
  return 0; 
}






Output:
200
#include<> vs #include””
Case 1: Include standard library header using notation #include””.
C
 

 
 










// C program to demonstrate  
// the difference 
// Header file 
#include "stdio.h" 
  
// Driver code 
int main()  
{ 
  int a = 10; 
  printf("%d", a); 
  return 0; 
}






Output:
10
Explanation:#include ” ” will search ./ first. Then it will search the default include path. One can use the below command to print the include path.
gcc -v -o a filename.c
Case2: Include standard header file using the notation #include<>
C
 

 
 










// C program to demonstrate  
// the difference 
// Header file 
#include <stdio.h> 
  
// Driver code 
int main()  
{ 
  int a = 10; 
  printf("%d", a); 
  return 0; 
}






Output:
10
Case 3: Include standard header file using both notation #include”” and #include<>, such as stdio.h
// stdio.hint add(int a, int b){    return(a + b);}
C
 

 
 










// C program to demonstrate  
// the difference between  
// ""  and <> 
  
// This will include the standard 
// header file 
#include <stdio.h> 
  
// This will include the user-defined 
// header file 
#include "stdio.h" 
  
// Driver code 
int main() 
{ 
  int a = 10; 
  int b = 20; 
    
  // Invoke the function defined in 
  // header file 
  int c = add(a, b); 
  printf("Result of addition is: %d", c); 
  
  return 0; 
}






Output:
Explanation:
1.  When stdio.h is created in the current directory then the code in Case 1 will generate an error but the code in Case 2 will work fine.2. ” ” and < > can be included together in the same code and the code will work fine since the search path priority is different for the two notations. Here, “” will include the user-defined header file and <> will include the standard header file.

C Language


C++


Difference Between

Writing code in comment? 
                                    Please use ide.geeksforgeeks.org, 
                                    generate link and share the link here.
                                
TCP Server-Client implementation in C
Exception Handling in C++
'this' pointer in C++
Multithreading in C
Arrow operator -> in C/C++ with Examples
Vector in C++ STL
Inheritance in C++
Initialize a vector in C++ (6 different ways)
Map in C++ Standard Template Library (STL)
C++ Classes and Objects