gcc
Implementation of programming language Cgcc is a short name for GNU C compiler, which is included in GNU Compiler Collection.
C was the first language of the Collection, and first version of its compiler was released in 1987. It is distributed under GNU GPL and is an important example of free software.
GCC logo
Examples:
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;
}
Factorial:
Example for versions gcc 3.4.5, gcc 3.4.5 (Objective-C), gcc 4.2.4This example uses recursive factorial definition. Note that 13! and larger causes an overflow, so the last lines of the output look like this:
13! = 1932053504
14! = 1278945280
15! = 2004310016
16! = 2004189184
#include <stdlib.h> /* needed for EXIT_SUCCESS */
#include <stdio.h>
unsigned long long factorial(unsigned long long n)
{
if (n == 0) {
return 1;
} else {
return n * factorial (n - 1);
}
}
int main(void)
{
int n;
for (n = 0; n <= 16; n++) {
printf("%i! = %lld\n", n, factorial(n));
}
return EXIT_SUCCESS;
}
Fibonacci numbers:
Example for versions gcc 3.4.5, gcc 3.4.5 (Objective-C)This example uses recursive definition of Fibonacci numbers. Note the difference from C++ example: loop counter must be declared outside of the loop, and printf is used for output instead of std::out.
#include <stdio.h>
int fibonacci(int n)
{
return ( n<=2 ? 1 : fibonacci(n-1) + fibonacci(n-2) );
}
int main(void)
{
int n;
for (n=1; n<=16; n++)
printf("%d, ", fibonacci(n));
printf("...\n");
return 0;
}
Comments
]]>blog comments powered by Disqus
]]>