0% found this document useful (0 votes)
4 views

C Programming_Akim_chapitre_1

The document is a course overview for a C programming class taught by Pr. PhD Adekunlé Akim Salami at Ecole Polytechnique de Lome. It covers the fundamentals of C programming, including variables, data types, operators, and basic program structure, with hands-on training to develop coding skills. By the end of the course, students will be proficient in writing and troubleshooting C programs, preparing them for advanced projects.

Uploaded by

patricegnanzim
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

C Programming_Akim_chapitre_1

The document is a course overview for a C programming class taught by Pr. PhD Adekunlé Akim Salami at Ecole Polytechnique de Lome. It covers the fundamentals of C programming, including variables, data types, operators, and basic program structure, with hands-on training to develop coding skills. By the end of the course, students will be proficient in writing and troubleshooting C programs, preparing them for advanced projects.

Uploaded by

patricegnanzim
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 28

ECOLE

POLYTECHNIQUE DE
LOME (EPL)

C Programming
By
Pr, PhD Adekunlé Akim SALAMI
Electrical Engineer

A. Akim SALAMI, EPL-UL 2022-2023 1


Course Overview

C is a programming language created in the mid 1970s by Dennis


MacAlistair Ritchie & Kenneth Lane Thompson of Bell Labs.
It is a structure-based, top-down methodology arranged language,
normally utilized for building framework applications, database
frameworks and illustrations bundles.
The course likewise gives hands-on preparing to assist you with
composing and test your coding aptitude, and set you up for
genuine application.

A. Akim SALAMI, EPL-UL 2022-2023 2


Course objectives?
Before the finish of this C programming affirmation course, you will
be conversant in C, and furthermore realize how to manufacture
fundamental C projects and work on cutting edge ventures. This
course will assist you with achieving the accompanying :

a. Gain information on principal ideas of the C language


b. Learn about factors and constants, information types, number
juggling administrators, exhibits and circles
c. Execute programs utilizing direction line contentions
d. Differentiate among printf and scanf
e. Implement conditionals and switch case in your code
f. Write your very own code, run and troubleshoot it
A. Akim SALAMI, EPL-UL 2022-2023 3
Course Outline
A. Introduction
B. Flow Control
C. Functions
D. Arrays
E. Strings
F. Pointers
G. Struct
H. Additional Topics
I. Completing C Basics
J. Projets

A. Akim SALAMI, EPL-UL 2022-2023 4


A.Introduction
C is a powerful general-purpose programming language that was initially developed to rewrite
the UNIX operating system.

Apart from that, modern web browsers like Google's Chrome and Firefox, database
management systems like MySQL and hundreds of other applications use C.

C is taught as the first programming language for many beginners. This is because learning C
helps you to understand how the software and hardware interact with each other.

This course is a great introduction to C programming and that too in the most interactive way
possible. Here's how the learning process works:

➢ Learn a concept
➢ Edit and run code related to it
➢ Practice what you have learned in real-time
➢ By the end, you'll write hundreds of programs and become comfortable writing code in C.
A. Akim SALAMI, EPL-UL 2022-2023 5
A.Introduction
Your First C Program
To get a feel of what C code looks like, let's write our first C program.

We will write a simple Hello, World! program.

#include <stdio.h>

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

return 0;

A. Akim SALAMI, EPL-UL 2022-2023 6


}
A.Introduction
Basic Structure of a C Program

#include <stdio.h>

int main()
{
...

return 0;
} A. Akim SALAMI, EPL-UL 2022-2023 7
A.Introduction
C Comments
In programming, we can make computers ignore certain parts of the program by using
comments.

Comments are hints that we add to our program to make our code easier to understand. They
are mainly used for humans to read and understand the code.

In C programming, we use // (two forward slashes) to start a comment. For example,


#include <stdio.h>

int main() {

// print Hello World


printf("Hello World");

return 0;
A. Akim SALAMI, EPL-UL 2022-2023 8
}
A.Introduction
C Comments
Let's take another example of comments in a program.

// Program to print Hello World

#include <stdio.h>

int main() {

/* Le programme imprime
Hello World
*/
printf("Hello World"); // print Hello World

return 0;
}

A. Akim SALAMI, EPL-UL 2022-2023 9


A.Introduction
Literals
In programming, we will be dealing with different types of data. All these fixed data (values) that
we can use directly in our program are called literals. For example, 5, -23.5, 'c', etc.

Different Types of Literals


For now, we will look at three types of literals:

1. Integer Literals - Numbers without decimal parts. For example, 25, -54, 923, etc.

2. Floating-point Literals - Numbers that have a decimal part. For example, 32.9, -56.5, 9.0, etc.

3. Character Literals - A single character that is enclosed within single quotation marks. For
example, 's', '}', 'N', '*', etc.

Now that we know what literals are, let's learn how to store them.

A. Akim SALAMI, EPL-UL 2022-2023 10


A.Introduction
Variables Introduction
In the previous lesson, we learned about number and character values. In programming, we
may need to store these data and use them later in the program.

For this, we use something called variables.

C Variables
Variables are containers to store data like numbers and characters.

In this lesson, we will learn to

➢ create variables
➢ print variables
➢ change values assigned to variable

A. Akim SALAMI, EPL-UL 2022-2023 11


A.Introduction
Declare Variables
The syntax to declare variables in C programming is:
<type > <variable> ;
Exemple
int age;
Here,
Int is a type
age is a variable
The age variable can only store integer values. We know this because we have used the data
type int to declare it.

Note: The ; at the end is mandatory in C programming. It indicates the end of the statement.

A. Akim SALAMI, EPL-UL 2022-2023 12


A.Introduction
Rules for naming identifiers
➢ A valid identifier can have letters (both uppercase and lowercase letters), digits and underscores.
➢ The first letter of an identifier should be either a letter or an underscore.
➢ You cannot use keywords like int, while etc. as identifiers.
➢ There is no rule on how long an identifier can be. However, you may run into problems in some compilers if the
identifier is longer than 31 characters.
Special Characters C Keywords
C Keywords
Special Characters in C Programming
, < > . _ auto double int struct

( ) ; $ : break else long switch

% [ ] # ? case enum register typedef

' & { } " char extern return union

^ ! * / | continue for signed void

- \ ~ + do if static while

default goto sizeof volatile

const
A. Akim SALAMI, EPL-UL 2022-2023 float short unsigned
13
A.Introduction
Assign Values to Variables
Once we declare a variable, we can assign a value to it.

// declare the variable


int age;
// assign the data 60 to it
age = 60;

Declare and Assign Value together


We can also assign values to variables during their declaration

// declare the variable and assign the data 60 to it

int age = 60;

A. Akim SALAMI, EPL-UL 2022-2023 14


A.Introduction
Declare Multiple Variables
To declare more than one variable of the same type, use a comma-separated list:

// declare multiple variables and assign the data 60 to it

int age = 60, taille = 174;

A. Akim SALAMI, EPL-UL 2022-2023 15


A.Introduction
Print Variables
We use the printf() function to print variables in C programming. We will learn in detail about
printing output in later lessons.

#include <stdio.h>

int main() {

Note: The %d format specifier can only


// create a variable
be used to print int variables. To
int age = 60;
print double variables, we use %lf,
which we will see next.
// print the variable
printf("%d", age);

return 0;
A. Akim SALAMI, EPL-UL 2022-2023 16
}
A.Introduction Basic types
Type Size (bytes) Format Specifier
Data Types
Data types determine the type of data associated with int at least 2, usually 4 %d, %i

variables. char 1 %c

float 4 %f

1. int type - used for integer values without decimal. For double 8 %lf

example, 4, -43, etc. short int 2 usually %hd

unsigned int at least 2, usually 4 %u

2. double type - used for floating-point numbers with


long int at least 4, usually 8 %ld, %li
decimal. For example, 34.9, 98.2, etc.
long long int at least 8 %lld, %lli

3. char type - used for characters. For example, 'g', '(', '-', unsigned long int at least 4 %lu

etc.
unsigned long long
at least 8 %llu
int

signed char 1 %c

unsigned char 1 %c

at least 10, usually


long double %Lf
12 or 16

A. Akim SALAMI, EPL-UL 2022-2023 17


A.Introduction
C Input Output (I/O)
#include<stdio.h>
the scanf() function is used to read int main()
formatted data from the console (stdin) {
int age;
scanf("<format specifier>" , <address of variable>); float weight;

How to get the address of a variable ? //getting input from the user and storing it into
Using ampersand (&) operator, we can get the memory addresses
address of a variable. In C, we will refer & as address
of operator scanf("%d%f",&age,&weight);
printf() function sends formatted output
to the screen. printf("age = %d \nweight = %f \n",age,weight);

printf("format string",argument_list)
return 0;
}

A. Akim SALAMI, EPL-UL 2022-2023 18


A.Introduction
Operators in C
Operators are used to perform mathematical and logical calculation in program.
Unary operator
Only one operand is required to perform calculation.
++(increment operator),-- (decrement operator), --a / ++a /a++ /a--
Binary Operator
Two operands are required to perform calculation.
a=b+c;
Ternary Operator
Three operands are required to perform calculation.
a > b?printf("a"):printf("b");

A. Akim SALAMI, EPL-UL 2022-2023 19


A.Introduction
Arithmetic operators
Example: (a=200,b=10,a
Operator Description ns=0)
+ Add to operands ans = a + b; ans will be
210.
- Subtract operand_2 ans = a - b; ans will be
form operand_1 190.
* Multipy two operands ans = a * b; ans will be
2000.
/ divides operand_1 by ans = a / b; ans will be
operand_2 20.
% Gives the remainder ans = a % b; ans will
after the actual be 0.
division of operands Because 10 will
perfectly divide 200 so
remainder will be zero.
8 % 3 = 2.
A. Akim SALAMI, EPL-UL 2022-2023 5 % 4 = 1. 20
A.Introduction
Increment and decrement operator
Using increment and decrement operators, we can increment the value of a variable by 1 or
decrement by 1

int a = 5;

++a;

//After the above statement execution, the value of a will be 6.

int b = 10;

--b;

//After the above statement execution, the value of b will be 9.

A. Akim SALAMI, EPL-UL 2022-2023 21


A.Introduction
Increment and decrement operator
Using increment and decrement operators, we can increment the value of a variable by 1 or
decrement by 1

#include<stdio.h>

int main()
{
int a = 5;

a++;
printf("After Increment, a = %d\n",a); //now a will be 6
a--;
printf("After Decrementing, a = %d\n",a); // now a will be 5

return 0;
}

A. Akim SALAMI, EPL-UL 2022-2023 22


A.Introduction
Assignment Operator in C
Using assignment operators, we can assign value to the variables.
Equality sign (=) is used as an assignment operator in C

#include<stdio.h>

int main()
{
int a = 10;
int b = a;

printf("a = %d \t b = %d\n",a,b);

return 0;
}

A. Akim SALAMI, EPL-UL 2022-2023 23


A.Introduction
Example (true=1,false=
Operator Description 0)
Relational Operators in C
== Checks both left and right 10 == 10 it will return
Relational operators are used to operands are same true.
compare two variables. 10 == 7 it will return false
It will return 1 , if the comparison or != Checks both left and right 10 != 20 it will return true.
operands are not same. 10 != 10 it will return
relation is true. false.
otherwise it will return 0. < Checks whether the left 5 < 10 it will return true.
operand is smaller than 10 < 10 it will return false.
the right operand.

<= Checks whether the left 10 <= 10 it will return true


operand is smaller than 20 <= 10 it will return
or equal to the right false.
operand.

> Checks whether the left 50 > 10 it will return true


operand is greater than 5 > 10 it will return false.
the right operand.

>= Checks whether the left 10 >= 10 it will return true


operand is greater than 5 >= 10 it will return false.
or equal to the right
A. Akim SALAMI, EPL-UL 2022-2023 operand. 24
A.Introduction A B A || B Example
0 0 0 (5 > 10) || (5 <

Logical Operators in C 4) Both


expressions are

Logical operators are used to false. so, logical


OR output will be

perform logical operations of 0

0 1 1 (10 > 20) || (10 <


given expressions (relational 20) First
expression is
expressions) or variables. false and second
one is true. so,
logical OR output
A !A Example will be 1

0 1 !(100 < 10) 100 is


1 0 1 (10 < 20) || (10 >
greater than 10. So, 100) First
it will return false. expression is
!(false) ==> true true and second
one is false. so,
logical OR output
will be 1

1 1 1 (10 < 20) || (10 <


1 0 !(10 < 100) 10 is 100) Both
less than 100. So, it expressions are
will return true. true. so, logical
!(true) ==> false OR output will be
1

A. Akim SALAMI, EPL-UL 2022-2023 25


A.Introduction
Logical Operators in C
Logical operators are used to A B A && B Example
perform logical operations of 0 0 0 (5 > 10) && (5 <
4) Both
given expressions (relational expressions are
false. so, logical
expressions) or variables. AND output will be
0
0 1 0 (10 > 20) && (10 <
20) First
expression is false
and second one is
true. so, logical AND
output will be 0
1 0 0 (10 < 20) && (10 >
100) First
expression is true
and second one is
false. so, logical
AND output will be
0
1 1 1 (10 < 20) && (10 <
100) Both
expressions are
true. so, logical AND
output will be 1

A. Akim SALAMI, EPL-UL 2022-2023 26


A.Introduction #include<stdio.h>

Logical Operators in C int main()


Logical operators are used to {
perform logical operations of int a = 5, b = 10 ,ret;
given expressions (relational
expressions) or variables. ret = ( (a <= b) || (a != b) );
// 5 <= 10 ==> true. 5 != 10 ==> true. So, 1 || 1 will return 1.
printf("Return value of above expression is %d\n",ret);

ret = ( ( a < b) && (a == b ) );


// 5 < 10 ==>true. 5 == 10 ==> false. So, 1 && 0 will return 0;
printf("Return value of above expression is %d\n",ret);

ret = ! ( ( a < b) && (a == b ) );


/*we have used the same expression here.
And its result was 0.
So !0 will be 1.*/
printf("Return value of above expression is %d\n",ret);

return 0;
A. Akim SALAMI, EPL-UL 2022-2023 27
}
A.Introduction
Sizeof operator
Using sizeof operator, we can get
the size of a variable or a
datatype. #include<stdio.h>

int main()
{
printf("sizeof(int) = %d\n",sizeof(int));
printf("sizeof(char) = %d\n",sizeof(char));
printf("sizeof(float) = %d\n",sizeof(float));
printf("sizeof(double) = %d\n",sizeof(double));

return 0;
}

A. Akim SALAMI, EPL-UL 2022-2023 28

You might also like