Exploring the C program
C program is a simple language, lets see the language syntax and the minimum valid program
As we beginning learning about C, let us take a look of the minimal valid C program.
// hello.c
#include
int main( int argc, char ** argv ) {
printf("Hello, World!\n");
return 0;
}
This is the smallest practical example of a correct C program. let’s take a look at all of the different components.
Comments
// hello.c
At the first line in the program we have a comment, comment is introduced by double slash. this syntax was borrowed from C++ and it was added to C standard with C99.
But it’s commonly used, and it has. the advantage that if you come out to the end of a line and you want to put just a comment to the end of the line, you can do it that way.
Instead of using the old syntax, which would have been to have a slash and an and then another then slash at the end of the comment.
printf("Hello, world!\n"); // this will print Hello, world!
Now, the advantage of the old style C comments is that you can actually have your comment span multiple lines.
/*
the next print command will print
Hello, world!
*/
printf("Hello, world!\n");
Each style has its advantages. I tend to like the slash slash version. It’s simple and it’s quick and most of my comments are just one line anyway.
Pre-processor Directives
#include
The next line is a pre-processor directives, and we will cover it in depth in the next topics, but this one includes a file and it includes this file in place. This file is called stdio.h which sometimes simply pronounced standard-io. The entire file will be included in the compilation process in this place. And that allows us to have all our function declarations for the standard IO functions from the standard library.
Main function
int main( int argc, char ** argv ){
printf("Hello, World!\n");
return 0;
}
Next we have the Declaration of the main function The main function is reserved in C, all C programs must have a main function. It’s where execution begins. So it is required. It is not optional.
Correctly, the main function should return an int. And so you see that first word, INT, that declares the return type for the function. And you notice we have a return statement that says, return zero. And that 0 is the int that’s being returned by this function.
After the word main, you have parentheses ( int argc, char argv ) . And inside of those parentheses are declarations of variables. These are the variables that will be used for parameters. When parameters are past this function, they’ll be copied into these variables and those variables become accessible inside the body function.
There is an opening curly brace { , and there’s a closing curly brace }** at the end of the function. Those mark off the beginning and the ending of the block of code that is the body of the function.
And inside the body of this function, we have 2 statements. The 1st statement is a function call, calling a function called printf.
printf("Hello, World!\n");
And you know it’s a function called because it’s got a name and parentheses and something inside of the parentheses.
return 0;
And then. the next statement is the return statement with the zero.
Semicolon
Both of the statements in the main function are terminated by a semicolon, and that ; is not optional.
In C all statements must be terminated by a semicolon, even if it’s the last statement in a block.
A lot of languages, a semicolon can be admitted as the last statement of the block that makes the ; more of a separator than a terminator and see its a terminator of all statements must be terminated by ; If we remove this ; from the return statement, you’ll notice we get a syntax error.
Compile and run
To compile this program you have to save it in hello.c file and you can compile it locally on your machine by any C compiler like gcc on Linux OS, or mingw on Windows OS.
For example gcc compilation command is
$ gcc hello.c
This command will generate a new file called a.out you can run this file on Linux by
$ ./a.out
Hello, World!
As you can see this will print the Hello, World! message on the screen.