What is C programming?
C programming
#include <stdio.h>
#include <stdlib.h>
int main()
printf("Hello world!\n");
return 0;
}
Source Binary
Compiler
Code Code
.
Operator Action
– Subtraction
+ Addition
* Multiplication
/ Division
% Modulus
–– Decrement
++ Increment
Operator Action
> Greater than
>= Greater than or equal
< Less than
<= Less than or equal
== Equal
!= Not equal
Logical Operators Bitwise Operators
Operator Action Operator Action
&& AND & AND
|| OR | OR
! NOT ^ Exclusive OR (XOR)
~ One's complement (NOT)
>> Shift right
<< Shift left
Data_type varible1;
int number;
Data_type variable2;
Example
char character;
Data_type variable3;
: :
float weight;
: :
: :
Data_type variable n;
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)
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
}
If()….else Demo
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
The ternary operator (?) test code
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;
}
Switch Statement Demo
There are 3 types of loop in C programming language, namely:
1. for loop
2. while loop
3. do…while loop
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
}
for( ) loop Demo
while (condition test) { //Statements to be executed repeatedly // Increment (++) or Decrement (--) Operation }
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
}
while( ) loop Demo
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);
do…while loop Demo
The general form of a function is :
Return_Type Function_Name (Parameters)
{
Local Variable Declaration;
Logic;
Executable Statement 1;
……
}
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.
Pass by value example
Pass by referent example
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];
Single dimension array code example
The general form for declaring a two-dimension array is :
data_type array_name [row][column];
Example :
int grades [5][5];
Research yourself for more information