C - Hello, World
Tags: c, shell, gcc
Published: 2022-09-13 14:14:31

C is a imperative (procedural) language that was created by [Dennis Ritchie](https://en.wikipedia.org/wiki/Dennis_Ritchie) used to develop software like operating systems, databases, compilers, and so on.

What do we mean when we say imperative (procedural)? We first need to understand different paradigm of programming language and paradigm just means the style in which they are written of structured.

There are different types programming language paradigm, some of them are:

- Imperative programming

- Procedural programming

- Declarative programming

- Object-oriented programming

Of these paradigm we just need to know the two of them for c language.

**Imperative programming**: a set of instructions that are given to the computer to execute in a given order.
**Procedural Programming**: is a derivation of imperative programming, adding to it the feature of functions (also known as "procedures" or "subroutines").

---

## # Why choose c

- A good amount of embedded programming is done in C (Hardware programming)

- it fastest language that is high level enough to build practical things.

- With the appropriate libraries, you can truly use C for anything.

- Web Backend Development.

- Desktop App Development.

- Game Development.

- Language/Compiler Development.

- Operating Systems and Drivers.

---

## # Compilation Process

Before we start learning c language we first need to understand the compilation process and the commands used on each process.

The tool we are going to be using in this blog to compile c is **gcc** tool.

The process of compilation this like this in order:

- **preprocessor**: takes the source code(.c file) to generate intermediate file that is sent to compiler.

- **Compiler**: convert the intermediate code in to assembly code.

- **Assembler**: convert the assembly code in to an object code.

- **Linker**: link the object code with the necessary library to generate the executable file.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cj5jdu2q6anzn77r4vbv.jpg)

Now let's see each one of them individually.

**Preprocessor**: takes the source file to generate the intermediate file by:

- Remove the comments

- include header file's code in the file

- Replace macro names with code

Source code( .c file)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ljhwbe2fz00fqt6z0zjz.png)
  

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t0zhupydun21b4tf1m1r.png)

Now that we created the source code we need to preprocess it and we do that by the command

`gcc -E <FILE_NAME>      `

we can also use the `-o` function to output into a file.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xxeq2p1m1gpvru6aaxj4.png)

**Compiler**: takes the source code to convert it into an assembly code and the command we are going to use is:

`gcc -c <FILE_NAME>      `


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/new742xqtcoy9wg0vsdt.png)

**Assembler**: convert the assembly code into binary code 0's and 1's or machine code and the command we are going to use is:

`gcc -S <FILE_NAME>      `


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/w0p5rp3cuzqfmb5rorcc.png)

**Linker**: linker's includes some of the library files  into the program to create an executable file and the command we are going to use is:

`gcc <FILE_NAME>        `


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/umykqk0dybmdq92tpfka.png)

Now that we learned about the compilation process, we need to understand the gcc compiler.

---

## gcc

GCC (GNU Compiler Collections) is a compiler mainly used for C and C++ language.

`-o ` : is an option to specify the output of the file.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x1545rpiqzj7d0lbw706.png)

`-Werror `: is an option to show the warning if any error is there in the program.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ldg9mr9d9eznn2fn6j15.png)

`-Wall`: is an option to show all kind of warning like unused variables errors.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bebawj33ijpxg33m5qdb.png)

`-std= `: is an option to set the standard(version) of c to compile source file.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4w4562y440b27yhe3li3.png)

> Tip : For more information on gcc compiler use the command `man gcc` to read the manual. 

---

## #C - Hello, world

Now we finished learning about compilation process and gcc compiler, we need to get our hand dirty and start codding in c.

The first thing we need to do is learn the most common style of writing c language and how to document them,For this course we need to use the Linux kernel coding style [Linux style](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/plain/Documentation/process/coding-style.rst).


**Hello world code**

The structure of a hello world code

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rer1uis3400ldg8kkvv1.png)

`#include <stdio.h>     ` is the header file that define the input output routines used by a program.   
`int main(void)         ` is the entry point of our program
`printf("Hello world")  ` is a line that print the text


**Comments**

used to document you code and some of c language commenting syntax are:

```
/* comment  */
```

```
/*
 * muti line
 * commnet
 */
```

> Warning: you can not nest a comment in a comment

**Variables**
  
Used to connect a value to a symbol

`<type> <var_name>         `

Names of variables: [a-zA-z_][a-zA-Z_0-9]*

```
int var;
char c;
```

**Data types**

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cz7mg95eb0zbq96wkt83.png)

> Tip: for more info on data type read [Data Type](https://publications.gbdirect.co.uk//c_book/chapter2/integral_types.html)

**Modifiers**

some of the modifiers used in c are long, long long,short,unsigned and signed.

They are used to specify how much space the variable take on memory.

`long, long long` : are used to extend the space used on the system and can be used with int, double.
`short`: takes half the space on the system.
`signed`: support both positive and negative numbers, we can use it with int, char.
`unsigned`: support only positive numbers, we can use it with int,char.
 
**sizeof()**

This function is used to find out how a much size types use.

```
sizeof(char);
```
 
**Type casting**

Is a way of converting one type of data into another.

Example:

```
char c = 'a';
int i = 5;

int_num = (int)c;
float_num = (float)i;
```

we essentially converted the char into int and the int into float.

**pritnf()**

Used for printing or writing a statement on to the stdout.

```
printf("hello world")

printf("string:%s deciaml:%d float:%f char:%c","name",12,2.6,'C');

```

special symbols like %s,%d and %f are used to represent different data's that are imputed to the right of the main string.

**puts**

Does not include null character inputs to write or print to stdout.

```
puts("hello world");
```

**putchar**

Used to write a character, of unsigned char type, to stdout.

```
char i = 'a';

putchar(i);

```
putchar can sometimes be used to print digit's,To do this we need using ASCII value of '0'.

```
int i = 10;

putchar(i + '0');
```