← Old blog

27 Sep 2020 · C language · How to C

How to C for beginners, part 1 Hello World!

Written when I was a student and restored as it was. Archived original

Why C?

When you are learning a new language you ought to have a reason for it. If nothing else than to soothe your academic interests. While C is old fashioned and difficult to write a sensible program in. It’s got many things to offer even at this age.

To add to the list, you can run C programs virtually anywhere with minimal dependencies. It will also help you pass the CS major, since it is taught at every decent university.

Why not?

The advantages stated above are also its main disadvantages. If you want to write a high-level application that uses mulitple levels of abstractions and you’ve got little time, C is definitely not the language for it.

How?

So now you’ve got an idea of why to learn it. But how and where do you start? Well… You’ve come to the right place! This is the first part of a long series to learn the basics of C to anyone. I will update this post with a link to the next one when available. You can find the whole series under the “How to C” category on the side 😉

Compiler

That’s for the course intro. But how to actually run a code written in C? If you’re accustomed to interpreted languages—like Python—which code can be run as soon as it’s written, you might be baffled at first.

The code written in C must be compiled first before running it. That means that a special type of program—compiler—needs to be run against the source code we have written. This compiler turns our code into machine code consisting of 0s and 1s which the computer understands and can run efficiently – a binary file is created. Marked with .exe extension on Windows machines.

To see how to get a compiler up and running see the following sections for your operating system.

Linux

Personally, I would recommend compiling the C code on Linux. The tools there are easily set up and ready to use with little to no struggle.

To see what compiler you’ve got installed, type:

$ cc --version

The most popular one is the GNU C compiler (GCC). Another compiler that comes to my mind is the Clang C compiler. It doesn’t matter much which you choose. I’ll be using gcc for the tutorial.

Windows

If you are using Windows I would at least try the Windows Subsystem for Linux (WSL), which provides a Linux environment (terminal) for running Linux programs natively on your Windows filesystem.

If you insist on only using Windows, you’ve got an option of using an IDE like Visual Studio or CLion, which can compile and run the C code internally. If you want to compile C externally, I can suggest tools like Clang or MinGW, I advise you to study how to install and use them.

macOS

Install XCode and then run the following command to check the version of the compiler.

$ cc --version

Online interpreter

There is also a C interpreter that runs your code online. I don’t recommend using it. Learning how to compile your code locally is very important. But if you’re on the run or without proper development tools, the onlinegdb can help you.

Hello World

It wouldn’t be a much of an introductory lesson if we haven’t written a proper Hello World program. So bust out your favorite text editor of choice. I recommend vim if you’re staying in the command line anyway, but anything will do.

Create a hello.c file with these contents in it.

#include <stdio.h>

int main() {
	printf("Hello world!\n");

	return 0;
}

I will explain what each line does later in the series, but just for the completeness. The include directive (prepended with #) inserts the contents of a file to the source file. stdio.h header file contains the standard input and output functions, in our case – printf.

The main function contains code that is executed when the program is run. It returns an int. If the program runs correctly, it returns 0 – the success return code. The code of a function is enclosed in curly braces.

The printf function is the common way of outputting something to the standard output. You will learn more about various output streams later on.

How to run it?

We need to compile it first, as I said before. I am using the GCC compiler in a Linux shell for the series. To compile the code, run the gcc command with the file name as an argument.

$ gcc hello.c

This will generate a binary file called a.out. When run, the following output should be produced.

$ ./a.out
Hello world!

gcc can be provided with the -o argument to specify the outputted binary’s name.

$ gcc -o hello hello.c

Running ./hello now will produce the same results.

What’s next?

Congratulations on running your first C program! It is quite an accomplishment to get the tools working :) Compiling is fairly simple when using one file, but can get quite complicated with multiple source files and custom header files. This where makefiles come in, I will show them in the next lesson. They will make the compiling even simpler with just one command. Formatted output and different data types are right after that!

See you in the next one! 😀

wecros.com