Hello World¶
#include <stdio.h>
/**
* comment
*/
int main(int argc, char* argv[]) {
printf("Hello, World!\n");
return 0;
}
To compile and run:
gcc hello.c # creates executable a.out
./a.out
gcc -o hello hello.c # creates executable hello
gcc -g -Wall -std=c11 -o hello hello.c # creates executable hello with debugging on
./hello
SumTo¶
#include <stdio.h>
/**
* A summation function that uses recursion.
*/
int result = 0;
int sumTo(int max);
int main(int argc, char* argv[]) {
sumTo(3);
printf("Result is: %d\n", result);
}
int sumTo(int max) {
if (max == 1) return 1;
result = max + sumTo(max - 1);
return result;
}
To compile and run:
gcc -g -Wall -std=c11 -o sumTo sumTo.c
./sumTo