C Programming_Akim_chapitre_1
C Programming_Akim_chapitre_1
POLYTECHNIQUE DE
LOME (EPL)
C Programming
By
Pr, PhD Adekunlé Akim SALAMI
Electrical Engineer
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.
#include <stdio.h>
int main()
{
printf("Hello, World!");
return 0;
#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.
int main() {
return 0;
A. Akim SALAMI, EPL-UL 2022-2023 8
}
A.Introduction
C Comments
Let's take another example of comments in a program.
#include <stdio.h>
int main() {
/* Le programme imprime
Hello World
*/
printf("Hello World"); // print Hello World
return 0;
}
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.
C Variables
Variables are containers to store data like numbers and characters.
➢ create variables
➢ print variables
➢ change values assigned to variable
Note: The ; at the end is mandatory in C programming. It indicates the end of the statement.
- \ ~ + do if static while
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.
#include <stdio.h>
int main() {
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
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
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;
}
int a = 5;
++a;
int b = 10;
--b;
#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;
}
#include<stdio.h>
int main()
{
int a = 10;
int b = a;
printf("a = %d \t b = %d\n",a,b);
return 0;
}
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;
}