The simplest C program, explained
This is the easiest way I know to write a C program that outputs “Hello World!”:
But what does it all mean? What is #include
? What does <stdio.h>
mean? What is an int
?
Or as a famous family member of mine once said,
“Why not just type
"Hello World!"
into notepad?”.
I’ll do my best in this article to explain, and hopefully, you’ll gain a healthy appreciation for C as a language.
#include <stdio.h>
We’ll get to the “#” later, but the “include” statement tells the compiler to import some code into that line.
This code to be imported is in a file called “stdio.h” which is a header file. STDIO means Standard Input/Output, and this is because the file contains code that enables operations like reading input from the keyboard, and writing output to the screen.
A “#” is placed before “include”, to tell the compiler to pre-process that line before the actual compilation begins. For “#include <stdio.h>”
, this means the compiler replaces that line with all the text in the stdio.h
file.
Oh yes, there is an stdio.h
file, and you can see it here.
Functions
Before we discuss “int main()”, I’d like to explain what functions are. If you already know, you might want to skip to the next section of this article.
Everything in a program is an instruction, unless it’s a comment. Sometimes you want to combine a group of instructions, and give them a common name, like how telling a child to “go to school” would translate into:
Put on school uniform
Leave the house
Enter school bus (maybe)
Wait till bus arrives school.
It’s much easier to say “go to school” everyday, than to repeat all four (4) of the instructions above.
Common names for a block of instructions in programming are called functions. Sometimes they go by other names like “procedures” and “methods”, but when writing C, we’ll call them functions.
int main()
A function declaration (description) in C consists of parts like:
Name: A function’s name is its handle by which it is referenced. In “int main()”, we describe a function called “main”. For our “go to school” command, we can have a function called “goToSchool”.
Return Type: When you give a command, you expect feedback. Was it successful? Did that child go to school? This feedback can come in a variety of ways … it could be a simple “true” or “false” indicating whether the “go to school” command was successful, or a “date/time” indicating the child arrived school. Our “main” function in C returns an integer, which is a number that has no decimal point e.g. 0, 1 or 178.
Arguments: What if you wanted to tell that child to “go to school” in 5 minutes, rather than now? The “in 5 minutes” becomes a modifier for the “go to school” function, and the child knows to wait 5 minutes before leaving for school. These modifiers are known as arguments in C. They’re placed in-between the brackets after the function’s name. In our hello-world program, our “main()” function has no arguments.
Body: The body of a function contains all the instructions it represents. It is surrounded by an opening brace “{“ and closing brace “}”
The “main” function is the entry-point to your program. It is the first function that is called when your program is executed.
printf(“Hello World!”);
Here, we’re executing (or calling) a function called “printf”, which displays text on an output device like the screen. It accepts text, or a char array, as its first argument, which is “Hello World!”.
Note: All statements in a function’s body end with a semi-colon
return 0;
Remember the Return-Type as part of a function’s declaration? The “main” function in C returns an integer.
This is so that another program can know if the program we’re writing is successful or not. Yes, programs can “talk” to one another.
A successful program would return, or respond with 0. Any other response means that an error has occurred. See more on Exit Status Codes.
Yaaay, we made it! 💃
Hopefully, this is much clearer, or I’ve left you with even more questions. Holla in the comments below. 😄