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.cAssuming 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 helloOf 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.
Hello World in Java ( simple )
I write the word “simple” in my heading because I do not wish to fool anyone. This method of writing and compiling a Java class is really only useful for small, single class Java programs. One would not want to use this for full scale Java apps. For that you need Gradle, which is another discussion entirely. But, doing it this way is legal and useful for learning.
First, you need to ensure that you have a Java SDK ( Software Developement Kit ). The easiest way to know is type javac --version on the commandline. If you get an error, it is not installed. If you get status and a version number, it is installed. If the SDK is not installed, go take some time to install it on your system and return. By the way, this is one of the few things I do that can be done on Windows as well as Linux. And, it works equally well on both.
Java source file ( Java is extremely picky )
There is no end of folks that dislike how picky Java is about syntax and its larger statements. However, that can be overcome with experience and time. You need to open a text editor and type in the following code. Pay attention to every detail. It’s important.
public class Hello {
public static void main( String[] args ){
System.out.println( "Hello World!" );
}
}Do you see similarities to C? They are there. The use of semicolons to end a statement. The use of braces to block out code into functions and classes. Function names with code bodies. I like to use the similarities to help me write.
The largest difference is in the first line ( and it’s block of code ). Java is an Object Oriented Programming language. In short, it has classes. Classes allow you to group common attributes and code together as a unit, or object. Consider a Car class. It has a brand name, model name, year of make, and a host of other attributes. It also has methods toDrive, toPark, openDoor, and others. All of these attributes ( field variables ) and methods ( class functions ) create an object called Car.
Therefore, we have the public as opposed to private class Hello. It is also important that your file name Hello.java match this class name when saving the file, including capitals.
Inside the braces {} that define our Hello class we have one method, main( ). However, the main method is extremely important. It defines the entry point of our program. Not just the entry point of the class, but of the entire program. No other class can have a main method.
This method is of type public, just like our class. If not, the operating system could not enter the program. It is also of type static, meaning that it doesn’t require a class name to call it into operation. Finally, it is of type void meaning it doesn’t return a value. We could also use void in C to define a function that doesn’t return a value.
The String[] args or the “array of Strings called args” isn’t used often in Java, but it must exist here or the compile will fail.
The main method calls just one method function System.out.println( ) and is send as a paramter the String “Hello World!”. Notice here that we do not need the newline character \n. It is included in the definition of println. Our method println is a member of the System class that has a sub-class of out where our method can be called from. You will see this feature a lot in Java and you will likely create some of your own.
To compile our file Hello.java after we have saved it is to run the command line statement: javac Hello.java. This will compile Hello to a byte-coded intermediate file that can be run on any machine with a Java Runtime Engine ( JRE ). Most operating systems now include a JRE so they can run Java classes, but without the SDK, they cannot compile Java classes.
If everything went well, you should be able to find a Hello.class in your project folder ( or current folder ). Run it with: java Hello. If it doesn’t run or complains about not finding a main function, recheck your source file. Something might be amiss. Then recompile and try again.
Off to the races
There you have it. The two simplest programs to write and run in both C and Java. All so that you can say Hello World on your terminal screen. Play with these and try things. Some will succeed. Some will fail. In the future, I will write for more about both and also, how to use and/or create their build systems for more complex programming. Until then, happy coding!