g++

Implementation of programming language C++

g++ is the short nickname for GNU C++ compiler, which is included in GNU Compiler Collection.

The Collection was extended to handle C++ in December 1987, the same year as it was released. It is distributed under GNU GPL and is an important example of free software.

GCC logo
GCC logo

Examples:

Hello, World!:

Example for versions Borland C++ Builder 6, Microsoft Visual C++ 6, Microsoft Visual C++ 9 (2008), g++ 3.4.5
#include <iostream>

int main(void)
{
    std::cout << "Hello, World!" << std::endl;
    return 0;
}

Factorial:

Example for versions Borland C++ Builder 6, Microsoft Visual C++ 6, Microsoft Visual C++ 9 (2008), g++ 3.4.5

This example uses recursive factorial definition.

#include "stdio.h"

__int64 factorial(__int64 n)
{
	return ( n==0 ? 1 : n*factorial(n-1) );
}

int main(int argc, char* argv[])
{
	for (int n=0; n<=16; n++)
		printf( "%d! = %I64d\n", n, factorial(n) );
	return 0;
}

Factorial:

Example for versions Borland C++ Builder 6, g++ 3.4.5

This example uses recursive factorial definition.

#include <iostream>

unsigned long long factorial(unsigned long long n)
{
	if (n == 0) 
		return 1;
	else 
		return n * factorial (n - 1);
}

int main(void)
{
    for (int n = 0; n <= 16; n++) 
        std::cout << n << "! = " << factorial(n) << std::endl;
    return 0;
}

Fibonacci numbers:

Example for versions Borland C++ Builder 6, Microsoft Visual C++ 6, Microsoft Visual C++ 9 (2008), g++ 3.4.5

This example uses recursive definition of Fibonacci numbers.

#include <iostream>

int fibonacci(int n)
{
    return ( n<=2 ? 1 : fibonacci(n-1) + fibonacci(n-2) );
}

int main(void)
{
    for (int n=1; n<=16; n++)
        std::cout << fibonacci(n) << ", ";
    std::cout << "..." << std::endl;
    return 0;
}

Hello, World!:

Example for versions Turbo C++ 1.01, g++ 3.4.5, gcc 3.4.5, gcc 3.4.5 (Objective-C), gcc 4.2.4
#include <stdio.h>

int main()
{
    printf("Hello, World!\n");
    return 0;
}