You are on page 1of 33

Introduction to C

programming

Presenter : Riel Sovanndoeur


What is C programming?

C programming is a machine code that allows us to communicate


with the computer

C programming
Structure of C programming
#include <stdio.h>
#include <stdlib.h>
Source code
int main()

printf("Hello world!\n");

return 0;

}
Compiler

Source
Compiler Binary Code
Code

Compiler is a computer program that transforms the human

written language into machine level language.


Data type
Arithmetic Operators
Operator Action
– Subtraction

+ Addition

* Multiplication

/ Division

% Modulus

–– Decrement

++ Increment
Arithmetic Operators Demo
Relational Operators
Operator Action

> Greater than

>= Greater than or equal

< Less than

<= Less than or equal

== Equal

!= Not equal
Logical and Bitwise Operators

Logical Operators Bitwise Operators

Operator Action Operator Action

&& AND & AND

|| OR | OR

! NOT ^ Exclusive OR (XOR)

~ One's complement (NOT)

>> Shift right

<< Shift left


Variable declaration

Data_type varible1;
int number;
Data_type variable2;
Example
char character;
Data_type variable3;
: :
float weight;
: :
: :
Data_type variable n;
Conversion Character
Conversion Character Displays Argument (Variable’s Contents) As
%c Single character
%d Signed decimal integer (int)

%e Signed floating-point value in E notation

%f Signed floating-point value (float)

%g Signed value in %e or %f format, whichever is shorter

%i Signed decimal integer (int)

%o Unsigned octal (base 8) integer (int)

%s String of text
%u Unsigned decimal integer (int)

%x Unsigned hexadecimal (base 16) integer (int)

%% (percent character)
Control Statement
1. If()…..else Statement

if (condition 1 is met)
{
do Task A
}
else if (condition 2 is met)
{
do Task B
}
else if (condition 3 is met)
{
do Task C
}
else
{
do Task D
}
Control Statement
If()….else Demo
Control Statement
2. The ternary operator (?)

The ? : operator is just like an if ... else statement

? : takes the following form:

? : is a ternary operator in that it takes three values

if condition is true ? then X return value : otherwise Y value


Control Statement
The ternary operator (?) test code
Control Statement
3. Switch Statement

The syntax of a switch statement is as follows:


switch (variable used for switching)
{
case firstCase:
do A;
break;

case secondCase:
do B;
break;

default:
do C;
break;
}
Control Statement
Switch Statement Demo
Loops
Type of loop in C

There are 3 types of loop in C programming language, namely:

1. for loop

2. while loop

3. do…while loop
Loops
1. for( ) loop
The for statement executes a block of code repeatedly until the test condition
is no longer valid.

The syntax for a for statement is as follows:

for (initial value ; test condition ; update)

//Do Some Task

}
Loops
for( ) loop Demo
while (condition test) { //Statements to be executed repeatedly // Increment (++) or Decrement (--) Operation }

Loops
2. while( ) loop

A while statement repeatedly executes instructions inside the loop while a certain

condition remains valid.


The structure of a while statement is as follows:

while (condition test)


{
//statement to be executed
//increment / decrement
}
Loops
while( ) loop Demo
Loops
3. do…while( ) loop
The do-while statement is similar to the while statement with one main

difference - the code within the curly braces of a do-while statement is

executed at least once.

do{

//Statements

while(condition test);
Loops
do…while loop Demo
Functions
The general form of a function is :

Return_Type Function_Name (Parameters)


{
Local Variable Declaration;

Logic;

Executable Statement 1;

……
}
Functions
There are two methods to pass the data into the function in C language :

1. Pass by value

In pass by value method, the value of the actual parameters is copied into the formal

parameters. In other words, we can say that the value of the variable is used in the

function call in the call by value method.

2. Pass by referent

In call by reference, the address of the variable is passed into the function call as the actual

parameter.
Functions
Pass by value example
Functions
Pass by referent example
Arrays
Arrays a kind of data structure that can store a fixed-size sequential collection of

elements of the same type.

The general form for declaring a single-dimension array is :

data_type array_name [size]

Example : int number[20];

char letter[30];
Arrays
Single dimension array code example
2 dimensional array
The general form for declaring a two-dimension array is :

data_type array_name [row][column];

Example :

int grades [5][5];


2 dimensional array
End of my introduction to C
Programming language
Research yourself for more information

Thank you !

You might also like