C Language Basics and Programming Concepts
C Language Basics and Programming Concepts
1: Introduction
2: Concepts of data storage and display
3: The conditions (Alternative structure)
4: Loops (repetitive structure)
5: Vector Tables
6:Functions and Procedures
7: Strings
8: The Matrix Tables
9: Recursion
10: The Pointers
11: The Structures 1
2
!
5
TYPES OF VARIABLES
Declaration of variables
int x;
float y,w;
Declaration and assignment of variable
int x = 1, z = 5
int y=x+2;
float pi = 3.14;
Char z = 'a';
Declaration and assignment of constant
We use the command line: #define.
7
#define PI 3.14159
Standard operators in C
+addition
subtraction
multiplication
/division
%modulo (remainder of integer division)
7/2 3
7.0/2 3.5
9
On ajoute les opérateurs parenthèses ( )
to establish the order of calculations.
float x = (y + z) / 2
INCREMENT AND DECREMENT
++ increment : i++
i += 1
i=i+1
decrement : i--
i - =1
i=i-1
11
intmain()
#include <stdio.h>
#include <stdlib.h>
intmain()
Hello world!
return 0;
}
void main() void main()
{char c; { char c = 'A';
int i, j, k;
c = 'A'; int i=50,j, k;
i = 50; j=k=10;
j = 10;
K=10;
Identifiers
Invalid identifiers:
3rd starts with a number
x#y unauthorized character (#)
no position Unauthorized character (-)
Nb pos Unauthorized character (space)
15
#include <stdio.h>
libraries
INPUTS OUTPUTS: PRINTF, SCANF
18
THE LIBRARIES
#include <stdio.h> : library (input/output).
#include <math.h> : for mathematical functions
#include <string.h> : String processing
#include <time.h> Date and time processing ()
DISPLAY A MESSAGE: PRINTF();
printf("Hello");
newline; printf("Hello\n");
: tabulation.
a : ringtone
b : space
#include<conio.h>
void main ()
first line
printf (" ");
printf (" second line ");
printf (" ");
third line
getch()
}
22
Display the data: printf
printf (" message ");
printf(" %d ", a ); a is an integer
printf(" %c", x); x is a character
printf("%f ", z); z is a real number
printf("%s", ch); ch is a string of characters
printf(" The result is %d ", a);
printf(" The result is %d \n", a );
printf(" The sum of 1ernumber %d and the 2thnumber %d
is equal to %d
printf(" %d +2 = %d ", a, a+2);
PRINTF, SCANF
scanf("%c",&x); x is a character
#include <stdio.h>
int i,j;
int main()
{printf("give the 1st integer ");
scanf("%d",&i);
give the 2nd integer
scanf("%d", &j);
printf("the product of %d and %d is equal to %d ", i, j, i*j);
%d * %d = %d
}
EX2
#include <stdio.h>
Void main()
int i, j, z;
give the 1ernumber" ); scanf("%d", &i) ;
Print("give the 2nd digit"); scanf("%d", &j);
the 1ernumber is %d on the 2thnumber is %d
i, j) ;
z=i;
i=j;
j=z;
the 1hethe number is %d on the 2ndththe number is %d
i,j) ;}
READING TWO VARIABLES IN A SCANF
char x = getchar();
char ch[10];
gets(ch);
CCHAPTER3: LES CONDITIONS
(STRUCTUREAALTERNATIVE)
IF...THEN...ELSE
if (condition)
Instruction1;
if (condition)
{
Instruction1;
Instruction2; }
IF...ELSE
if (condition)
Instruction1;
else
Instruction2;
if (condition)
{ Instruction1;
Instruction2; }
else
{Instruction3;
Instructions4; }
IF...ELSE
if (condition)
{if (condition)
Instruction1;
Instruction2; }
else
Instruction3;
Instructions4; }}
else
Instruction5;
Instructions6; }
IF...ELSE
if (condition)
{Instruction1;
Instructions2; }
else
if (condition)
Instruction3;
Instruction4; }
else
{Instruction5;
Instructions6; }
EXPRESSIONS EVALUATED IN TESTS
if ((h<0)||(h>23))
The entered time is incorrect.
Else
if ((m<0)||(m>59))
the entered minute is incorrect
else
m=m+1
…..
TEST WITH IF
int main()
{int n,m;
print("give the 1st number ");
scanf("%d", &n);
give the 2nd digit
scanf("%d", &m);
if (n>m)
the 1st number %d is the largest
else
the 2nd digit %d is the largest
return 0;}
#include <stdio.h>
int main(void)
{ int i;
Enter a number
scanf("%d",&i);
if (i > 0)
the number %d is positive
else if (i<0)
printf("the number %d is negative\n", i);
else
the number is zero
return 0;}
42
Test with switch (according to)
Syntax:
switch(expression)
{
case value1 : { instructions1;break; }
case value2 : { instructions2;break; }
...
case valeurn : { instructionsn;break; }
default : { instructionsother;break; }
}
Test with switch (according to)
int main()
{int i;
Enter a number either 1 or 2 or 3
scanf("%d", &i);
switch(i)
{ case 1 :printf(" le chiffre est 1 "); break;
case 2: printf("the number is 2"); break;
case 3 :printf(" the number is 3 "); break;
default :printf(" the number is different from 1,2,3");
}
}
printf(" enter the date D M Y ");
scanf(" %d %d %d ", &j, &m, &a);
switch(m)
{ case 1: case 3: case 5: case 7: case 8: case 10:case 12:
if((j>=1)&&(j<=31))
printf(" the date is valid ");
else
the value of the day is incorrect
break;
case 4: case 6: case 9: case 11:
if((j>=1)&&(j<=30))
printf(" the date is valid ");
else
The value of the day is incorrect.
break;
case 2 : if((j>=1)&&(j<=29))
printf(" the date is valid ");
else
The value of the day is incorrect
break;
default: printf("the value of the month is incorrect");
SWITCH(VALUE RANGE)
int t;
printf(" enter the water temperature ");
scanf("%d", &t);
switch (t)
case -100 ... 0 : printf("ice cream"); break;
case 1 ... 99 : printf(" liquid "); break;
case 100 ... 130 : printf(" gas "); break;
default: printf(" incorrect input "); break;
}
CCHAPTER4: LTHE LOOPS
(REPETITIVE STRUCTURE)
FOR LOOP
#include <stdio.h>
#define NUMBER 10
int main()
{
int c, total = 0;
The total is %d
}
WHILE LOOP
#include <stdio.h>
#define NUMBER 10
main()
{
int c = 1, total = 0;
51
DO WHILE LOOP
ALLOWS TO EXECUTE THE INSTRUCTIONS AT LEAST ONCE
BEFORE EVALUATING THE TEST
#include <stdio.h>
#define NUMBER 10
main()
{
int c = 1, total = 0;
do
{ total += c;
c++;
} while(c <= NUMBER);
The total is : %d
}
FORCE USER TO ENTER THE DATA IN
KEYBOARD OF AN INTEGER BETWEEN 1 AND 10,
int a;
Do
Enter an integer between 1 and 10:
scanf("%d",&a);
while ((a <= 0) || (a > 10));
EX3: DISPLAY ALL INTEGERS FROM 0 TO 9:
int i;
for (i = 0; i < 10; i++)
i = %d
break
The break instruction can be
employee inside any
What a loop. It allows
to interrupt the course of the
loop, and move to the first
instruction that follows the loop.
main()
int i;
for (i = 0; i < 5; i++)
i = %d
if (i == 3)
break;
}}
print on the screen
i=0
i=1
i=2
i=3
continue
Continuing education allows for
go straight to the value
next, without executing the others
instructions of the loop.
main()
int i;
for (i = 0; i < 5; i++)
if (i == 3)
continue;
printf("i = %d\n",i);}
print on the screen
i=0
i=1
i=2
i=4
#include <stdio.h>
int c, i, nbCh, nbBlancs, nbalpha, nbAutres;
int main(){nbCh=nbBlancs=nbAutres=nbalpha=0;
while((c=getchar())!= '.')
if((c>='0') && (c<='9'))
nbChiffres++;
else if(((c>='a')&&(c<='z'))||((c>='A')&&(c<='Z')))
nbalphabet++;
else if((c==' ')||(c==' ')||(c==' '))
whiteCount++;
else nbOthers++;
printf("digits:%d", nbChiffres);
printf("alphabet:%d", nbalphabet);
whites:%d
printf("Others:%d\n",nbOthers); }
MATHEMATICAL FUNCTIONS
#INCLUDE<MATH.H>
Logarithmic functions
•x = log(..);
Exponential function,
double x = exp(..);
Power function
x = 5 raised to the power of 2;
•strlen:longueur de chaîne
Hello
int x= strlen(string);
{C=STR[I];
IF(ISLOWER(C))
C=TOUPPER(C);
PUTCHAR(C);
I++; }
CCHAPTER5: LESFFUNCTIONS AND
PROCEDURES
FUNCTIONS AND PROCEDURES
66
Typical program in C
include
Main() always there
first function called
main(){
a();
b();}
Call the function a
function a()
Call of function b
function b()
USE OF SWITCH TO CHOOSE
int main()
{ char choice;
a : the addition
b : multiplication
c : the subtraction
d : the division
Please provide your choice:
scanf("%c", &choice);
switch(choice)
{ case 'a' : procA(); break;
case 'b' : procB(); break;
case 'c' : procC(); break;
case d : procD(); break;
default: printf('incorrect choice');
}}
procA()
printf("you are executing procedure A: addition");
……