0% found this document useful (0 votes)
6 views68 pages

C Language Basics and Programming Concepts

The document provides a comprehensive overview of the C programming language, covering fundamental concepts such as data types, operators, control structures, loops, and functions. It also includes practical examples and explanations of input/output operations, variable declarations, and the use of libraries. Additionally, it discusses the syntax and structure of C programs, including the main function and conditional statements.

Translated by

ScribdTranslations
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)
6 views68 pages

C Language Basics and Programming Concepts

The document provides a comprehensive overview of the C programming language, covering fundamental concepts such as data types, operators, control structures, loops, and functions. It also includes practical examples and explanations of input/output operations, variable declarations, and the use of libraries. Additionally, it discusses the syntax and structure of C programs, including the main function and conditional statements.

Translated by

ScribdTranslations
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

C LANGUAGE

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

12: The Files


GENERALITIES

The C language was created in the early 1970s


Successor language to the B language (the 1960s),
The C language is one of the programming languages.
procedural
Compiled language
Better portability (operation on
numerous platforms

2
!

FREE IDE FOR C

Code::Blocks for Windows, Mac, and Linux.


Visual Studio Express, Visual C++, Dev C++
for Windows
Xcode, On Mac

CHAPTER 2:
CCONCEPTS OF STORAGE AND
DISPLAYING DATA
TYPES OF VARIABLES

There are generally three types of variables:


Integers: int, short int, long int
Reals: float, double
The characters :char

5
TYPES OF VARIABLES

A char type variable occupies one byte


in memory.

A float: 4 bytes (single precision real)


A Double: 8 Bytes (real double precision)
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

The Assignment Operator (=)


Arithmetic operators (+, -, *, /, %)
Comparison operators (==, !=, <, <=, >, >=)
Logical operators (&&, ||, !)
ARITHMETIC OPERATORS

+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()

the main function


from your program,
Without the main function, it is
impossible to generate a
executable program
NEW CONSOLE PROJECT

Code::Blocks has thus generated the minimum code in


C language that we need.

#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

C distinguishes between lowercase and uppercase letters.

abc, Abc, ABC are valid identifiers and


all different.

Invalid identifiers:
3rd starts with a number
x#y unauthorized character (#)
no position Unauthorized character (-)
Nb pos Unauthorized character (space)

15
#include <stdio.h>

This line requests to include files in the


project, that is to say adding files for the
compilation.

libraries
INPUTS OUTPUTS: PRINTF, SCANF

The standard library is a collection used


to implement common operations, such as
the input/output and string management of
characters, mathematical functions.

The prototype of input-output functions


(scanf and printf) are in <stdio.h>
INPUTS OUTPUTS: PRINTF, SCANF

printf allows displaying text at


the screen
scanf allows you to retrieve the entered text at
keyboard

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>

getch reads a single character from the keyboard and returns it


as a result without writing it on the screen and without
wait for a line break.
Creation of 1erprogram printf
# Include <stdio.h>
#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

These functions use formats that


allow to read/write variables of
different types:
%f : réels float
%lf : double type reals
%d : integers int
%ld : entiers long int
%s : string
%c : a character
%x will display a hexadecimal
%o : will display a value in base 8 (octal base) 24
Read data from the keyboard: scanf

The use of & is essential with scanf.

(value read and therefore modified),

scanf("%c",&x); x is a character

scanf("%d", &y); y is an integer

scanf("%f", &z); z is a real number

scanf("%s", &ch); ch is a string.


Read data from the keyboard: scanf

We can fix the number of characters of


the data to read.
%10s for a string of 15 characters,

%5d for an integer that spans 5 digits.

%.2f the real is displayed with 2 digits after the


comma. (by default 6 digits after the comma).
EX1

Write a program that inputs two integers and


advertise their product.
Ask the user to enter 1ernumber
Ask the user to enter the 2nd digit

Calculate sum, product, difference, the


quotient and the remainder of the division.
EX1

#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

Write a program that exchanges


two integers entered. Display them
whole numbers before and after
the exchange. (permutation)
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

printf("give the two numbers \n");


scanf("%d%d", &i, &j);
EQUIVALENT OF PRINTF

Putchar(..), This function has the effect


to emit the character passed in
parameter to the output stream
standard. char c='A'; putchar(c);
putchar('s'); // is equivalent to printf("s");

Puts(..) : This function has the effect of


to emit the string of characters
passed as a parameter to the stream of
[Link] (" the result ");
// equivalent printf(" the result \n");
EQUIVALENT OF SCANF

getchar, for reading a character.

char x = getchar();

gets, for reading strings.

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 (a = b): frequent error,


if (a==b) : a is equal to b
if (a!=b) : a is different from b
if (a>b) : a greater than b
if ((a>=0)&&(a<10)) : comprises between 0 and 9
if ((a<=b)||(a>0)) : a less than or equal to b
positive egg
if (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')
39
EXPRESSION 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)

Default: propose an instruction to


perform in the case where the expression has not

none of the values proposed in the


successive cases.
Break: returns to the end of the switch. If the

break is not set, the program


will execute the following case.
USE OF SWITCH CASE

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;

for( c =1; c <= NUMBER; c++)


total += c;

The total is %d
}
WHILE LOOP

#include <stdio.h>
#define NUMBER 10
main()
{
int c = 1, total = 0;

while(c <= NUMBER)


{
total += c;
c++;
}
The total is : %d
}

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>

the sine, cosine, and tangent functions.


•x =sin(..); y =cos(..); z =tan(..);

Logarithmic functions
•x = log(..);
Exponential function,
double x = exp(..);

Power function
x = 5 raised to the power of 2;

Square root function


x = sqrt(25);
Absolute value function
x = fabs(-7);
STRING FUNCTIONS
#INCLUDE<STRING.H>

•strlen:longueur de chaîne
Hello
int x= strlen(string);

strcpy: Copy a string


char chaine2[ ], chaine[ ]= "TDI";
strcpy(chaine2, chaine);

strcat: concatenate two strings


char c1[10] = "bon"; char c2[10] = "jour";
strcat(c1, c2); /* we concatenate */
strcmp: compare 2 strings

char ch[3]; scanf("%s", &ch);


if (strcmp(string1, "TDI") == 0)
{ }
else { }

sprintf: Write into a string


char string[100]; int N = 1;
NTIC TDI %d Year!
CTYPE.H
int isupper(C) : returns a non-zero value,
if C is an uppercase letter
int islower(C) : returns a non-zero value,
if c is a lowercase
int isdigit(C) : returns a non-zero value,
if C is a decimal digit
int isalpha(C) : returns a non-zero value,
if (C) is the alphabet
int isalnum(C) : returns a non-zero value,
if isalpha(C) or isdigit(C)
int isspace(C) : returns a non-zero value,
if C is a space sign
touuper(C): converts c to uppercase
tolower(C): converts C to lowercase
INT I=0;
TEST STRING.
CHAR C;
WHILE(STR[I]) // WHILE(I<STRLEN(STR))

{C=STR[I];
IF(ISLOWER(C))
C=TOUPPER(C);
PUTCHAR(C);
I++; }
CCHAPTER5: LESFFUNCTIONS AND
PROCEDURES
FUNCTIONS AND PROCEDURES

The program contains procedures and


The procedures specify the tasks to
executed by the program.
The 'main' program establishes the logic of
the whole code. It calls the different
functions or procedures necessary to perform the
sub-tasks.

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");
……

You might also like