Recently I've been learning how to create some basic programs in the C programming language with my father and brother.
hello.c
#include <stdio.h>
int main(void)
{
printf("Hello, world\n");
return 0;
}
This is the first program we wrote, which says "Hello, world". On the first line we included stdio which is a library of functions. "stdio" stands for standard input / output.
The next part of our program is the main function. Every C program must have a main function to work.
int stands for integer, which is a whole number. The line int main(void) means that our main function returns a whole number to say if the function worked or failed. It returns 0 if it worked, or any other number if it failed.
The void means that we ignore any extra words on the command-line.
The printf stands for print formatted. This is a function that we use to output words. The \n at the end of the string stands for newline, it moves onto the next line.
The last of the function return 0; means that our program succeeded.
hello_name.c
#include <stdio.h>
int main(void)
{
char *name = "Bongo2";
printf("Hello %s\n", name);
}
In our second program, we changed it so that we print someone's name instead of "world", for example, "Bongo2". We created a string variable called name, and set it to "Bongo2".
In the printf %s stands for a string. In this case, the string is the name variable, which we set to "Bongo2".
maths.c
#include <stdio.h>
int main(void)
{
int a = 18;
int b = 6;
printf("The answer is %d\n", a * b);
}
The last program we wrote was to do with maths. We used two int variables called a and b, and gave them the values 18 and 6.
The %d in the printf format string is going to print a whole number. The "d" stands for decimal or base 10. The number it prints is a calculation, a times b, which equals 108.
Next post: Loops and Math in C, by Sean
Previous post: Deciding on a Programming Language for Game Dev, by Sam
Contents: Game Dev From Scratch
Top comments (0)