Hello World in C and Java
First steps in programming
Now, that we have all the background covered, I want to show you the beginnings of programming. I will be showing you the steps to build and run the ubiquitous “Hello World” program in both C and Java. The C version will use the C99 standard ( though it matches most standards as well ) and the Java version will use Java 17 ( though it will likely run in whichever Java you have ). These two language standards will be where I will work as I write more posts.
Hello World in C
Write your source code
Open your editor and type in the following program ( or copy it from the code block ).
#include <stdio.h>;
int main( void ){
printf( "Hello World!\n" );
return 0;
}
What does what I wrote mean?
This is the basis of everything you will write in C from now on. I’ll explain each part.
#include <stdio.h>;
This line is actually not code, but a preprocessor directive. All preprocessor directives begin with # followed by a word. The #include tells the compiler to go and find the header file listed and include it in the program. The stdio.h is the header file for standard input/output functions used by almost every program you will write.
printf is a standard input/output function declared in stdio.h. The two right and left arrow brackets also have a meaning to the compiler. Somewhere in your file system is the default include folder or directory. They tell the compiler to look in that folder for the stdio.h file.
There are quite a few standard input/output functions in stdio.h of which printf is one of the most useful. As you can see, it is given the parameter of "Hello World!\n".
printf is a formatted printing function. It will print a literal string which is character(s) bounded by quotation marks. It also handles control characters like the \n which is the newline character. Without this added character, the cursor would remain at the end of the sentence when printed instead of advancing to the next line on the screen.
Sometimes, we might want to write our own header file. That header file would be in our project folder and the compiler wouldn’t know where to look for it. If that is the case, we would write our personal header file include like this:
#include "my_personal.h";
Header files can contain both function or variable declarations. It can also contain C functions. However, it is more common to compile the actual functions into a library ( a C object file with a .a or .so ending ) and link the library during compile. Yet, every C compiler already knows to link in the stdio.h library as long as you include the header file. No extra steps are necessary.
We also see this line:
int main( void ){
This is the opening to the main C function. Every program in C written to be a standalone executable should have a main function called main. The int in front means that our main function will return an integer number back to the operating system upon completion.
The parentheses is where one places any parameters needed by the function. We place void here in order to tell the compiler there are no parameters. There are two that can go here, but since we aren’t using them; yet, we just use void.
You should also notice that every function has its code block located between braces {}. There must be an opening and closing brace for every function.
This method is also legal in C:
int main( void )
{
}
We already covered the printf function and its parameter. Next comes our return statement:
return 0;
This line tells the compiler to return a 0 as a status of success to the operating system. Any other number returned tells the OS that this program failed in some way.
Every line of code in C must end in a semicolon ( ; ). This is how the compiler knows when to stop reading a statement and process it before moving on. Semicolons, brackets, arrow brackets, and braces are all code punctuation that the compiler uses to know what you mean. Other languages, like Python, depend upon spacing to know what you mean. I have always found it better to depend on punctuation to hold meaning, just like we do it in regular writing.
Compiling linking execution
At its very basic, we can compile and link our Hello World code into an executable program with this command:
gcc hello.c
Assuming that we saved our content in a file named hello.c, this would compile and link the output to a file named a.out. If you run a.out, it should display Hello World! on your terminal screen. If anything else happened or you received errors while compiling, recheck the file contents. Properly typed in, this file will compile and run.
If you must have your program named as you wish, you can do it like this:
gcc hello.c -o hello
Of course, you don’t have to name it hello, you can name it after yourself or your mom or whatever. Those things don’t matter. You could also have saved your file as myfirstfile.c or nearly anything as long as you add .c to the end. That’s how gcc knows it is a C program file.
Now, there are many, many more things to learn about how to write in C, but these things are the basics. Don’t stop here. Play with this. Have printf say other things to the terminal. Write multiple printf functions with different things. See what happens if you don’t include stdio.h.
Remember, you are in complete control with C. But, with great power also comes great responsibility, or some such nonsense.
I am getting tired today. I will come back to this later and add the Java version. It is a bit more involved, but only some. Until then, happy coding!