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

C Program chapitre 2

The document is a course overview for C programming, detailing its history, objectives, and structure. It covers fundamental concepts, flow control, functions, arrays, strings, pointers, and includes hands-on training for practical coding skills. The course aims to equip students with the knowledge to build basic and advanced C projects.

Uploaded by

patricegnanzim
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

C Program chapitre 2

The document is a course overview for C programming, detailing its history, objectives, and structure. It covers fundamental concepts, flow control, functions, arrays, strings, pointers, and includes hands-on training for practical coding skills. The course aims to equip students with the knowledge to build basic and advanced C projects.

Uploaded by

patricegnanzim
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

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 Plan
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


B. Flow Control
Control statements
Control statements are used to decide the program execution order based on some conditions.
It is also called decision making statements.

Example
Let’s write a program for dividing two integers.
But, if we divide a number by 0, program execution will
stop as its undefined behavior.
Here, we must decide and restrict the execution of
dividing code, if the divisor is zero.

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


B. Flow Control
If statement
Statements under if executes only, if the given condition is true.
Otherwise those statements will not be part of program.
#include<stdio.h>
int main()
{
int number1,number2;

scanf("%d%d",&number1,&number2);

// if number2 value not equal to zero, it will become if(1).


// otherwise it will become if(0)

if(number2 != 0)
printf("number1 / number2 = %d\n",number1/number2);

return 0;
}
A. Akim SALAMI, EPL-UL 2022-2023 6
B. Flow Control
If statement
Statements under if executes only, if the given condition is true.
Otherwise those statements will not be part of program.
#include<stdio.h>
int main()
{
int number1,number2;

scanf("%d%d",&number1,&number2);

// if number2 value not equal to zero, it will become if(1).


// otherwise it will become if(0)

if(number2 != 0)
printf("number1 / number2 = %d\n",number1/number2);

return 0;
}
A. Akim SALAMI, EPL-UL 2022-2023 7
B. Flow Control
If else statement
Statements under if executes only, when the given condition is true.
Otherwise, the statements under else will be executed.
#include<stdio.h>
int main()
{
int number1,number2;

scanf("%d%d",&number1,&number2);

// if number2 value not equal to zero, it will become if(1).


// otherwise it will become if(0)

if(number2 != 0)
printf("number1 / number2 = %d\n",number1/number2);

return 0;
}
A. Akim SALAMI, EPL-UL 2022-2023 8
B. Flow Control
If else statement
Statements under if executes only, when the given condition is true.
Otherwise, the statements under else will be executed.
#include<stdio.h>
int main()
{
int num;

scanf("%d",&num);

if(num == 0)
printf("Zero");
else
printf("Non-zero");

return 0;
}
A. Akim SALAMI, EPL-UL 2022-2023 9
B. Flow Control
Ternary operator
We can use ternary operator instead of if..else statement. It will take three arguments.
First argument is comparison statement.
Second argument will be executed, if the comparison becomes true.
Third argument will be executed, if the comparison becomes false.
#include<stdio.h>

int main()
{
int num;

scanf("%d",&num);

num < 0 ? printf("Negative") : printf("Positive");

return 0;

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


#include<stdio.h>

B. Flow Control int main()


{
int score;
Nested if else statement scanf("%d",&score);
Nested if else statements
if(score <=100 && score >= 90)
are used to execute printf("S");
different piece of code else if(score < 90 && score >= 80)
when we have more than printf("A");
else if(score < 80 && score >= 70)
two options to handle. printf("B");
if(condition) else if(score < 70 && score >= 60)
{ printf("C");
else if(score < 60 && score > 50)
}
else if(condition)
printf("D");
{ else if(score == 50)
printf("E");
} else if(score < 50 && score >= 0)
else if(condition)
{ printf("U");
else
} printf("Enter a valid score between 0 and 100");
else
{
return 0;
}
}

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


B. Flow Control
Switch Statement
Switch statement allows us to pick one options at a time among many options.
If none of them are match, then default case will execute.

switch(choice)
{
case 1:
//statement
break;
case 2:
//statement
break;
default:
//statement
}

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


B. Flow Control
Looping in C
printf("Good Morning\n");
printf("Good Morning\n");
printf("Good Morning\n");
printf("Good Morning\n");
printf("Good Morning\n");
printf("Good Morning\n");
printf("Good Morning\n");
printf("Good Morning\n");
printf("Good Morning\n");
printf("Good Morning\n");

Looping statements allows us to repeat statements execution


till the given condition becomes true.

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


B. Flow Control
For loop in c
In general, we should use for the loop when we know the exact loop count

#include<stdio.h>

for (initialize; condition; increment or decrement) int main()


{ {
//statements int i;
}
for(i=1;i<=5;i++)
{
printf("Good Morning\n");
}

return 0;
}

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


B. Flow Control
#include<stdio.h>
While loop int main()
When we don’t know the exact {
iteration count or the loop count, int num,count = 0;
we should use while loop.
In other words, if iteration count scanf("%d",&num);
depends on the input, we should
use while loop while(num > 1)
{
count++; //increment count variable
initialize variable
num = num >> 1; //equal to num = num / 2.
while(condition)
}
{
//statements
printf("%d",count);
in(de)crement variable
}
return 0;
}

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


B. Flow Control
Do while loop
We have to use do while loop when we need #include<stdio.h>
to execute a group of statements at least
once. int main()
Only difference between while and do while {
loop is that while loop will execute int num;
statements only when given condition is true
do
whereas do while will execute statements at {
least once without checking the condition. scanf("%d",&num); //getting input from user
printf("%d\n",num); //Printing the number

do }while(num != 100); //if the input is 100, loop will terminate


{
//statements return 0;
in(de)crement variable }
}while(condition);

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


B. Flow Control
Do while loop
We have to use do while loop when we need #include<stdio.h>
to execute a group of statements at least
once. int main()
Only difference between while and do while {
loop is that while loop will execute int num;
statements only when given condition is true
do
whereas do while will execute statements at {
least once without checking the condition. scanf("%d",&num); //getting input from user
printf("%d\n",num); //Printing the number

do }while(num != 100); //if the input is 100, loop will terminate


{
//statements return 0;
in(de)crement variable }
}while(condition);

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


B. Flow Control
Nested Looping
C languages allows us to create a loop
inside another loop.
//nested do..while
do
//nested while loop //nested for loop {
while(condition) for(init; condition; inc or dec) do
{ { {
while(condition) for(init; condition; inc or dec) //statements
{ { }while(condition);
//statements //statements
} } }while(condition);
} }

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


B. Flow Control
Break statement int main()
Break statement is used to terminate the {
loop and resume the program execution from int num;
the next statement following the loop.
while(1) //It is known as infinite loop since the condition
If we want to use break statement inside
always 1
loop, we have to use it along with if {
statement. scanf("%d",&num); //getting input from user
It is also used to terminate the switch case
execution. if(num < 0) // if num is less than zero
break; //terminate the loop

printf("%d\n",num); //Printing the number


}

return 0;
}

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


B. Flow Control
#include<stdio.h>
Continue statement
Continue statement is used in looping to skip int main()
some statements. {
It is used inside loop along with the decision- int i;
making statements.
When continue statement encounters, the for(i=1;i<=100;i++)
{
execution will go to the starting of the loop.
if(i == 25 || i == 50 || i == 75)
The statements below continue statement continue;
will not be executed, if the given condition
becomes true. printf("%d\n",i);
}

return 0;
}
If we run the above program, it will print numbers from 1 to 100
except the numbers 25,50 and 75.
A. Akim SALAMI, EPL-UL 2022-2023 20

You might also like