Get started with C

by Rickard Borg Rickard Borg

3 thanks

  • Welcome to my C course!

    +
  • Hello and welcome to my C programming course! My name is Rickard and I will take you through this course step by step.

    +
  • In my lessons, we will cover all the basics of C, but we will also get into more advanced topics such as file management, program arguments and dynamic memory allocation.

    +
  • After we've gained some knowledge, we're going to build two applications: a text-based Tic-Tac-Toe game, and a program for weather observations!

    +
  • So what exactly is C?

    +
  • C is a low-level programming language created by Dennis Ritchie and is used to create computer programs. It is usually considered to be a great first language to learn! By learning it, you will also have the basics for learning C++, a newer and more powerful variation often used for video game programming.

    +
  • Dennis Ritchie - Creator of the C programming language
    +
  • Some software that are written in C are Bitcoin, Mozilla Firefox and many of Google's services. Most operating systems, like Windows and the Linux distributions, as well as advanced software, like Microsoft Excel and Adobe Photoshop, are also written in C.

    +
  • A low-level programming language means that it's closer to the hardware. The "lowest-level" language is is called Assembly, where you give every single instruction directly to the CPU (Central processing unit). In C it is not that complicated though, but it can still seem a bit overly explicit sometimes if you're used to using high-level language like Python or Java. For instance, there is no built-in function for sorting a list of numbers – if you need it, you will have to create it yourself.

    +
  • C also uses a compiler in order to translate your source code into machine code so that your computer can understand it. Machine code, in case you didn't know, is simply a huge amount of 11s and 00s (a.k.a binary code) giving your computer instructions.

    +
  • You'll notice that, once you have learnt C, you will find it easy to learn other programming languages. C is often considered to be the "grandfather of all programming languages", since almost all other succeeding languages are heavily influenced by it, both in terms of syntax and general concepts.

    +
  • While we're talking about syntax, let's take a look at what a basic C program looks like!

    +
  • "Hello world" in C

    +
  • Loading code...
    +
  • The code that you see above is one of the most basic C programs. Its only purpose is to print out the text "Hello world!" to the screen.

    +
  • The first row (#include ) is basically telling our program to include the standard input/output library so that we can use input from the user, and output what we want to the screen.

    +
  • int main(void) is actually the most important line of code in our program. Every program must have a function called main in order to run. Without this function, your computer will not know where the program starts. The keyword int in the beginning tells us that the function will return an integer. We will talk more about this later when we start looking at functions in more detail.

    +
  • void is a keyword used when we are not sending any arguments to our functions. We will look more closely at arguments in an upcoming chapter as well.

    +
  • Inside the function, we have a line that says printf("Hello world");. As you probably guessed, that line is responsible for printing out hello world to the screen, using the stdio.h library that we included at the top of our program.

    +
  • return 0 is our return value for the function. We could actually omit this line since it doesn't serve any real purpose in our program. Officially however, the main function in C should always return an integer, so we specify one for the sake of making everything pedantic and correct.

    +
  • You may have noticed those semicolons (;) at the end of some lines. This basically tells the CPU to end that instruction. Every instruction needs to have a semicolon so that our computer knows that it has finished the instruction and can move to the next line.

    +
  • To help you understand the necessity of semicolons, we can draw an analogy to literary languages like English and Swedish . As you know, when you write a sentence, you end it with a dot before starting the next one. It works the same way in programming, but we use semicolons instead of dots!

    +
  • Another thing you might have noticed is the pair of curly braces({}) around the main function. They encapsulate what's called a "code-block", and are used here to define where the function starts and where it ends. All code within this block will be executed when the function is called.

    +
  • One important thing to remember when writing the code is that the code should be indented for every "code-block". This basically means that we use tab (if it is not indented automatically) to indent the text. The reason that we do this is because the code is more readable if we do it like this. Let's look at two codes with the same code blocks, but one is indented and the other one is not. Let's see the difference.

    +
  • Loading code...
    +
  • As you can see, we indent our code inside the code blocks because it's easier to see what is what and what code blocks that belong to each other, and it looks much more nice!

    +
  • Now let's see at the same code but without indentation to see that it is much harder to read it.

    +
  • Loading code...
    +
  • As you can see, this doesn't look very nice, so remember to indent the code yourself unless your IDE (Integrated Development Environment) does it for you! You will get used to how the code should be indented during this course, so don't be worried!

    +
  • Setting up your environment

    +
  • To start writing your programs you will need something called an IDE (Integrated Development Environment) which is basically a program that you can write code in. I am going to use Dev-C++ throughout this course, but there are many other IDEs like Eclipse and Xcode that work just as well!

    +
  • On Linux:

    +
  • If you use Linux, you can run your program in the terminal by navigating to the folder where you have your program. You will have to compile it manually using the gcc-compiler. The compiler should already be built in to your system, if not, go ahead and install it!

    +
  • To compile your program in Linux, you first need to save your file with the .c extension. You can copy the code that we wrote above, and save it as hello.c. Then open your terminal, navigate to that file and run:

    +
  • Loading code...
    +
  • Here, hello.c is the name of your file, and -o program specifies what the compiled output file should be called.

    +
  • After the compilation is done, you can run the program simply by typing ./program.

    +
  • On MacOS:

    +
  • We need two things to run C programs on a mac:

    +
    • 1

      Text editor: I recommend using Sublime text which is great for coding.

    • 2

      Terminal with C compiler: Press + space and type Terminal, press enter when the app "Terminal" appears.

    +
  • Type in xcode-select -p to check if you already have a C compiler installed.

    +
  • If you see an output that looks like this: /Applications/Xcode.app/Contents/Developer, then you already have the full Xcode installed!

    +
  • If not, type:

    +
  • Loading code...
    +
  • The window shown below should appear. Press install

    +
  • This window should appear after entering
    +
  • Verify that the installation was successful by typing xcode-select -p in the terminal. The output should now look like this: /Applications/Xcode.app/Contents/Developer

    +
  • To run a file, navigate to the directory where the file is located with cd, then compile with clang hello.c -o program. After that, you can run the compiled file by typing ./program.

    +
  • On Windows:

    +
  • In order to install GCC on Windows, you'll first need to download MinGW.

    +
  • While installing, you'll also need to get gcc-core, gcc-g++, binutils, and the MinGW runtime, for everything to work correctly.

    +
  • Finally you'll have to add MinGW\bin to your PATH variable.

    +

Did it help you? Click to say "thanks" to the author!

Next lesson: Variables and data types

Want to create your own course on Ludu?

Back to lesson