What is the difference between g++ and gcc?

The gcc and g++ are both GNU compiler collection tools, but they serve different purposes. gcc is primarily designed for C programs, while g++ is designed for C++ programs. Understanding their differences helps choose the right compiler for your project.

Syntax

gcc program.c -o executable_name
g++ program.cpp -o executable_name

gcc (GNU C Compiler)

The gcc compiler is specifically designed to compile C programs. It treats files with .c extension as C source code and links against the standard C library by default.

Example: Using gcc for C Program

#include <stdio.h>

int main() {
    int a = 20;
    printf("The value of a: %d\n", a);
    return 0;
}
The value of a: 20

Compilation: Save the code as program.c and compile using: gcc program.c -o program

g++ (GNU C++ Compiler)

The g++ compiler is designed for C++ programs. It can compile both .c and .cpp files, but treats them as C++ source code and links against the C++ standard library.

Example: Using g++ for C++ Program

#include <stdio.h>

int main() {
    int a = 20;
    printf("The value of a: %d\n", a);
    return 0;
}
The value of a: 20

Compilation: Save the code as program.cpp and compile using: g++ program.cpp -o program

Key Differences

Aspect gcc g++
Primary Use C programs C++ programs
File Extensions .c files .cpp, .cxx, .cc files
Default Libraries C standard library C++ standard library
Language Features C language features only Full C++ features (classes, objects, etc.)

Conclusion

Use gcc for pure C programs and g++ for C++ programs. While g++ can compile C code, gcc is more appropriate for C-only projects as it provides better optimization and avoids unnecessary C++ library linking.

Updated on: 2026-03-15T10:01:19+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements