Introduction to C programming language

Getting started with C (a basic programming language)

C is probably the most widely known programming language. It is used as a reference language for computer science courses all over the world, and it may be the language that people learn most in schools using Python and Java.

I remember it is the second programming language after Pascal.

C is not just something students use to learn programming. This is not an academic language. I would say that this is not the simplest language, because C is a fairly low-level programming language.

Today, C is widely used in embedded devices and provides support for most Internet servers built with Linux. The Linux kernel is built using C, which also means that C is the core of all Android devices. It can be said that C code occupies a large share of the entire world. right now. It's quite remarkable.

When C was created, it was considered a high-level language because it was portable across machines. Today, we can run programs written on the Mac on Windows or Linux. This may be a matter of course, and maybe we can use Node.js or Python. Once upon a time, this was not the case. What C brings is an easy-to-implement language with a compiler that can be easily ported to different machines.

I said about the compiler: C is a compiled programming language, such as Go, Java, Swift or Rust. It will also explain other popular programming languages such as Python, Ruby or JavaScript. The difference is the same: the compiled language generates a binary file that can be directly executed and distributed.

C is not garbage collection. This means that we must manage the memory ourselves. This is a complex task that requires a lot of attention to prevent errors, but it is also ideal for programming embedded devices such as Arduino in C.

C will not hide the complexity and functions of the machine below. Once you know what you can do, you have a lot of power.

I now want to introduce the first C program, which we will call "Hello, World!".

Hello C

#include <stdio.h>

int main(void) {
	printf("Hello, World!");
}

Let's describe the source code of the program: we first importstdioLibrary (the name stands for the standard input and output library).

This library allows us to access input/output functions.

C is a very small language, and its core is any language that is not part of the core. Some of these libraries are built by ordinary programmers and can be used by others. The compiler also has some other libraries built in. likestdioAnd others.

stdioIs a library that provides the followingprintf()Features.

The function is wrapped into amain()Features. Thismain()A function is the entry point of any C program.

But what is a function?

A function is a routine that accepts one or more parameters and returns a single value.

in the case ofmain(), The function does not take any parameters and returns an integer. We usevoidThe keyword of the parameter, andintThe key of the return value.

The function has a body, wrapped in curly braces, and inside the body, we have all the code that the function needs to perform its operations.

Thisprintf()As you can see, the way the function is written is different. It does not define the return value, we passed a string enclosed in double quotes. We did not specify the type of the parameter.

That's because this is a function call. somewherestdiolibrary,printfis defined as

int printf(const char *format, ...);

You don’t need to understand what this means, but in short, this is the definition, when we callprintf("Hello, World!");, That's where the function runs.

Thismain()The functions we defined above:

#include <stdio.h>

int main(void) {
	printf("Hello, World!");
}

Will be run by the operating system when the program is executed.

How do we execute the C program?

As mentioned earlier, C is a compiled language. To run the program, we must first compile it. Any Linux or macOS computer already has a built-in C compiler. For Windows, you can use the Windows Linux Subsystem (WSL).

In any case, when you open a terminal window, you can typegcc, And this command should return an error stating that you did not specify any files:

That's very good. This means that the C compiler is there and we can start using it.

Now, type the above program intohello.cfile. You can use any editor, but for the sake of simplicity, I will usenanoEditor in the command line:

Input program:

Now pressctrl-Xdrop out:

Confirm by pressingyKey, and then press Enter to confirm the file name:

That's it, we should now go back to the terminal:

Type now

gcc hello.c -o hello

The program should not give you any errors:

But it should have been producedhelloexecutable file. Type now

./hello

Run it:

I'm in front./To the program name to tell the terminal that the command is in the current folder

Astonishing!

Now if you callls -al hello, You can see that the program is only 12KB in size:

This is one of the advantages of the C language: highly optimized, which is also one of the reasons why it is so good for embedded devices with very limited resources.

Check out some of my other tutorials on C:

Download mine for freeC manual


More clang tutorials: