Intro To C Programming
Intro To C Programming
@c12mentor
Agenda
Intro to programming languages
Compiling C programs
Once upon a time
programming
languages were
born…
What is a programming language?
● Till now, thousands of programming languages have come into form, with
specific purposes, and variations in terms of the level of abstraction that
they all provide from the hardware.
● A few of these languages provide less or no abstraction at all, while the
others provide a very high abstraction.
● On the basis of this level of abstraction, there are two types of
programming languages:
○ Low-level language
○ High-level language
Low and High level programming languages???
● What is a function? A function is a routine that takes one or more arguments, and
returns a single value.
● In the case of main(), the function gets no arguments, and returns an integer. We identify
that using the void keyword for the argument, and the int keyword for the return value.
● The function has a body, which is wrapped in curly braces. Inside the body we have all the
code that the function needs to perform its operations.
● The printf() function is written differently, as you can see. It has no return value defined,
and we pass a string, wrapped in double quotes. We didn't specify the type of the
argument.
● That's because this is a function invocation. Somewhere, inside the stdio library, printf is
defined as int printf(const char *format, ...);
C program execution
● The C prog. will be run by the operating system when the program is
executed
● To run the program we must first compile it. Any Linux or macOS computer
already comes with a C compiler built-in.
● A compiler is a special program that translates a programming language's
source code into machine code, bytecode or another programming
language.
● GCC is the most common C language compiler
C program compilation
● The compiler will take the preprocessed file and generate IR code
(Intermediate Representation), so this will produce a .s file.
● Other compilers might produce assembly code at this step of compilation.
Step 3: Assembling -c
● The assembler takes the IR code and transforms it into object code, that is
code in machine language (i.e. binary). This will produce a file ending in .o
Step 4: Linking
The linker creates the final executable, in binary, and can play two roles:
○ linking all the source files together, that is all the other object codes in the
project. For example, if I want to compile main.c with another file called
secondary.c and make them into one single program, this is the step where the
object code of secondary.c (that is secondary.o) will be linked to the main.c
object code (main.o).
○ linking function calls with their definitions. The linker knows where to look for
the function definitions in the static libraries or dynamic libraries.
cont..
● By default, after this fourth and last step, that is when you type the whole
gcc main.c command without any options, the compiler will create an
executable program called a.out, that we can run by typing ./a.out in the
command line.
● We can also choose to create an executable program with the name we
want, by adding the “-o” option to the gcc command, placed after the name
of the file or files we are compiling
Resources