DEV Community

Cover image for C - Hello, World
Haile Melaku
Haile Melaku

Posted on

C - Hello, World

C is a imperative (procedural) language that was created by 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

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

Image description

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

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

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

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

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

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

Image description

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

Image description

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

Image description

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.

Hello world code

The structure of a hello world code

Image description

#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  */
Enter fullscreen mode Exit fullscreen mode
/*
 * muti line
 * commnet
 */
Enter fullscreen mode Exit fullscreen mode

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;
Enter fullscreen mode Exit fullscreen mode

Data types

Image description

Tip: for more info on data type read Data Type

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);
Enter fullscreen mode Exit fullscreen mode

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;
Enter fullscreen mode Exit fullscreen mode

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');

Enter fullscreen mode Exit fullscreen mode

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");
Enter fullscreen mode Exit fullscreen mode

putchar

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

char i = 'a';

putchar(i);

Enter fullscreen mode Exit fullscreen mode

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');
Enter fullscreen mode Exit fullscreen mode

Top comments (0)