Wednesday, August 20, 2014

Hello World

I am sitting at a Starbucks sipping my tea. The time has come to start programming again. We will be writing and compiling the standard first program, "Hello World!". We will be coding and compiling using the most basic and primitive tools available on a Windows computer.

I am assuming you are fairly comfortable navigating your computer and know how to create files and folders. I also assume you can grasp the general ideas and adapt them to fit your compiler and operating system. The general idea is the same for each compiler and system, it's only the details that vary. I also assume you have downloaded and installed a compiler. I installed several just so I can see their differences. Write down the location of this compiler as you will be needing this information soon.

Create a folder for your C programming. I made a folder at C:\Programming\C. I will be placing my C programming in sub-folders here. I right click and create a new folder named HelloWorld. I navigate into this folder, right click, and create a new text file.


Open the new text file in whatever default text editor is set up on your system. On mine it is MS Notepad. Type the following (or copy/paste) text into the text editor:

 #include <stdio.h>
 
 int main(void)
 {
    printf("Hello, world!\n"); 
    return 0;
 }

Save this file as "HelloWorld.c". You should see the new file appear in the folder. You can close the text editor now.

Here is the tricky part for many Windows users. Open a command prompt. You may want to look up how to do this on your computer (Windows 8.1 users should read THIS), but if you want to be a programmer, you really should know how your computer works. In this prompt, navigate to the folder you saved your HelloWorld.c file.

You need to pass your code (the .c file) to your compiler. Remember how I told you to write down the location of your compiler? Type that into the prompt now. Add a space and then type HelloWorld.c and press return. Depending on your compiler, you may or may not see something happen.

Type HelloWorld into the command prompt. Again, depending on your compiler or settings, you may see your program execute or, if you have the same compiler as me, you will see a message telling you that:
'HelloWorld' is not recognized as an internal or external command,
operable program or batch file.


What's with that? Checking the working folder in Windows Explorer, I find an .exe file called a.exe. Reading my compiler's help files I see that if the compiler is not told what to call the program it is compiling, the result is an executable named a.exe. Type that into the command prompt. That's it! The program works! We just compiled and executed our first C program.


We could stop there, but I want to fix something. I want my finished program to be called "HelloWorld.exe". If your compiler produced a final .exe named a.exe, you can follow along and fix that with me.


In the command prompt, type path to your compiler again. Type a space and then HelloWorld.c. Type space again followed by "-o HelloWorld" (without the quotes) and press return. This tells the compiler what you want your final program to be named (get to know your compiler's documentation). This should produce "HelloWorld.exe" in the working folder. Type "HelloWorld" into the prompt and press return. There it is!

That's all we set out to do this time around. But let's go for extra credit! We will make a batch file to compile our source code.

In our text editor we make a new text file and type:
@ECHO Compiling Now...
C:\TDM-GCC-64\bin\gcc.exe helloworld.c -o HelloWorld

@ECHO COMPILING FINISHED!
@ECHO.


Of course you should use the path to your compiler instead of the path I have to mine. Save this to the working folder as "CompileHelloWorld.BAT". Now, if we change the source code, we just run this batch file from the command line or Windows Explorer and the batch file will compile for us and save us some typing. As a programmer, batch files can come in very handy. You would do yourself a great favor if you learned how to write and use batch files as you learn to program.

Type the name of the batch file in the command prompt and press return. The batch file will execute. You should now find HelloWorld.exe in the working folder.


If you have done all that is in this blog post and you have never programmed before, you should celebrate. You have written and compiled your first C program. And you did it the hard way! You'll thank me later. It only gets easier from here. We have not gotten into the actual C programming language, but you should have a very good idea how to make your source code into a finished executable program.