A Student’s C Book: 1.1. Hello, World!

Level 1. Introduction to C

1.1. Hello, World!

What if I told you that you could talk to your computer via text files? Yeah, that’s right. Opening up a text file is like opening your messaging app to talk to your friend. The sorts of things that you can write on a messaging app, such as asking whether your friend will come to the university tomorrow or asking for the solution for homework that the teacher gave during one of yesterday’s classes, are also possible to do with your computer. However, there is a small but surely expected catch: you have to open up a new text file on your computer instead of an app on your phone, and you have to learn the computer language to be able to speak to it. I mean, what else would one expect? If you were to talk to people from different countries, you wouldn’t have been able to use your mother tongue all the time. As we sometimes need to learn foreign human spoken languages to be able to communicate with other people, now you need to learn the computer’s language to communicate with your computer. The language is called C.

Computers talk with commands.

Humans tend to usually be polite when they talk to each other. They avoid shouting, screaming, and giving other people commands. Here’s a typical conversation that might be held between two adults:

* Hello, John. How have you been?
* Would you like to go to the library today?
^ Hi, Nina. I am fine, thanks for asking. What about you?
* I am trying to be fine, too. Have just been trying to understand what this C stuff is… 😬
^ Yeah, sure. I also crave to understand how this thing works. 😅
^ I’ll see you in the library then.

This kind of conversation between humans is too complex and chaotic for computers to comprehend. Computers aren’t as smart as us (yet). Since they have limited “intelligence” compared to us, they need to be given concrete and rigorous commands. This is the same as interacting with kids; you don’t explain every little detail why you “commanded” an 8-year-old kid to not come home later than 8 pm. You can try to convey your reasoning, but it just might not get through. So, if John and Nina were just a couple of computers, then the conversation could have gone as follows instead:

* Hello, John! Answer “fine!” if you have been fine lately!
^ Hello, Nina! fine! Answer “fine!” if you have been fine lately!
* Answer “yes!” if you would like to go to the library to study C today!
^ yes!

Asking “How have you been?” nicely in human language is the same as commanding to shout “fine” if John feels alright. In the third line, Nina didn’t mention “fine.”, so John automatically understood that Nina wasn’t fine. Also, pay attention to how they did not talk over each other; instead, they spoke one line at a time. From the third person’s view, they may seem like a bunch of angry, stupid computers shouting at each other for no reason, but for them, this is the normal way of talking. So, we have to get used to it for now. At least, it is much more concise and faster than ours.

Computers have their own command vocabulary.

Again, there is nothing unexpected here, as we have words in the Azerbaijani vocabulary, and computers have their own words in their C vocabulary. It shouldn’t be surprising that their vocabulary contains only a few words. Here are a few examples that are translated from our language to pseudo (fake) computer language:

In English, we say: Hey Nina, could you calculate the area of a rectangle with sides 3 cm and 4 cm?
In C, we say: Multiply 3 by 4!
(because computers are dumb, remember? you can’t just ask it to do math; you have to teach it.)

In English, we say: Hey John, pick a number – an integer, and I’ll try to guess it.
In C, we say: Make up an empty place in your mind for a number and hold a random integer there!
(humans remember the number they pick without needing to remind themselves that they have to store it somewhere in their brain, but computers need to be told every single step of the process.)

The exact words that the computers use look like this in reality:

C keywordsTranslation to English
intWhen you would like to talk about integers (for example, -23 or 0 or 889)
float/doubleWhen you would like to talk about the real number (for example, -23.45 or 0.0 or 3.1415)
charWhen you would like to talk about symbols (for example, the letter ‘a’ or the digit ‘1’ or the ‘!’ sign)
printfWhen you would like to display something on your screen
includeWhen you would like to remind the computer of the things that it can already do (for example, computers don’t understand printf if you don’t remind them by using include first)

Hello, World!

Now, it is time to tell the computer to print (display) a greeting message on the screen. Here’s the code for it:

#include <stdio.h>

int main(){
    printf("Hello, World!");
    return 0;

}

A code or a computer program is a bunch of sentences written in the computer language. In the code shown above, the first line (reading #include <stdio.h>) is to remind the dumb computer that it can display stuff on the screen. Then, when the computer reads the third line (reading printf(“Hello, World!”);), it displays “Hello, World!” on your screen. We will not focus on the other lines for now, but you’ll quickly learn what those mean, and soon you’ll realize that it is all a bunch of simple commands that your computer needs to be told by you, a programmer, to do interesting and fun stuff.

I should probably also mention that the program above should be saved in a file (using text files is the first step for communicating with computers, remember?). Then you should follow these steps:

  1. For simplicity, let’s save the file as code.c (note that the file extension must be .c and not .txt)
  2. Now, you need compile the program by typing the following command on the terminal and pressing ENTER or RETURN: gcc -o code code.c (this tells your computer to read the text inside code.c and inform you if there is anything on that file that it does not understand; if everything is understandable by the computer, it will give you another file named code)
  3. Finally, you can execute (run) the program by typing the following and pressing ENTER or RETURN on your terminal: ./code (in contrast to step 2, this line tells your computer to execute or perform the things mentioned in the code.c file)

These steps must be done in the right order. The first step basically puts your commands in the code.c file into the computer’s memory by saving the file. The second step is to make sure that your commands are understandable by the computer because if not, the computer will complain about it so that you can go back and fix the file. Finally, the third step is executed after the second step is successful (that is, a new file named code is generated by the computer, which contains the translated version of your commands). 

Table of Contents

  1. Preface
  2. Level 1. Introduction to C
    1. Hello, World! ←  you are here
    2. Basics
      1. Your computer can memorize things
      2. Your computer can “talk” and “listen”
      3. Compiling and Running programs
    3. Functions
      1. I receive Inputs, You receive Output
      2. Simple pattern matching
      3. Function calling and Recursion
    4. Control Flow
      1. Branching on a condition
      2. Branching back is called Looping
    5. Pointers
      1. Memory address of my variable
      2. Pointer Arithmetic
    6. Arrays
    7. Data Structures
      1. All variables in one place
      2. Example: Stack and Queue
      3. Example: Linked List
  3. Level 2. Where C normies stopped reading
    1. Data Types
      1. More types and their interpretation
      2. Union and Enumerator types
      3. Padding in Structs
    2. Bit Manipulations
      1. Big and Little Endianness
      2. Logical NOT, AND, OR, and more
      3. Arithmetic Bit Shifting
    3. File I/O
      1. Wait, everything is a file? Always has been!
      2. Beyond STDIN, STDOUT, and STDERR
      3. Creating, Reading, Updating, and Deleting File
    4. Memory Allocation and Deallocation
      1. Stack and Heap
      2. Static Allocations on the Stack
      3. Dynamic Allocations on the Heap
    5. Preprocessor Directives
    6. Compilation and Makefile
      1. Compilation Process
      2. Header Files and Source Files
      3. External Libraries and Linking
      4. Makefile
    7. Command-line Arguments
      1. Your C program is a function with arguments
      2. Environment variables
  4. Level 3. Becoming a C wizard
    1. Declarations and Type Definitions
      1. My pointer points to a function
      2. That function points to another function
    2. Functions with Variadic Arguments
    3. System calls versus Library calls
      1. User mode and Kernel mode
      2. Implementing a memory allocator
    4. Parallelism and Concurrency
      1. Multiprocessing
      2. Multithreading with POSIX
    5. Shared Memory
      1. Virtual Memory Space
      2. Creating, Reading, Updating, and Deleting Shared Memory
      3. Critical Section
    6. Safety in Critical Sections
      1. Race Conditions
      2. Mutual Exclusion
      3. Semaphores
    7. Signaling
  5. Level 4. One does not simply become a C master

Leave a Reply

Your email address will not be published. Required fields are marked *