Table of Contents
Operators
Operators are the function that use a symbolic name . They are use to perform mathematical and logical functions.
Operator are predefined in C. There are many type of operators such as
- Arithmetic Operators
- Relational Operators
- Logical Operators
- Assignment Operators
- Increment and Decrement Operators
- Conditional Operator
- Bit wise Operator
- Special Operators
Arithmetic Operator
Arithmetic operator is mathematical function that take two operands and perform the calculation on them.
Operator | Use |
---|---|
+ | Addition |
- | Subtraction |
* | Multiplication |
/ | Division |
% | Modulus : It gives remainder after division |
Example
#include
int main()
{
int m=3,n=7,addi,subt,multi,modu; /* Variables Defining and Assign values */
float div;
addi=m+n;
subt=m-n;
multi=m*n;
div =m/(float)n ; // cast any one either numerator or denominator
modu = m%n;
printf("sum of two numbers is %d\n", addi);
printf("substraction of two numbers is %d\n", subt);
printf("multiplication of two number %d\n", multi);
printf("result of division of two number %f \n", div);
printf("remainder after division of two number %d\n", modu);
return 0;
}
output: sum of two numbers is 10
substraction of two numbers is -4
multiplication of two number 21
result of division of two number 0.428571
remainder after division of two number 3
Relational operator
It is a Boolean operator. If relation is true, it gives 1 and if relation is false, it gives 0.
Operator | Use |
---|---|
== | Equal to |
<= | Less than or equal to |
>= | Greater than or equal to |
!= | Not equal to |
> | Greater than |
< | Less than |
Example
#include
int main()
{
int a=3,b=3,c=5;
printf("check if a and b are equal: %d \n", a==b); //output :1 means relation is true
printf("check if a is greater than c: %d\n",a>c);
printf("check if a is not equal to c: %d\n",a!=c);
return 0;
}
Output: check if a and b are equal: 1
check if a is greater than c: 0
check if a is not equal to c: 1
Logical Operator
Logical operators are commonly used in returning Boolean value (either 0 or 1) depending upon whether expression results true or false.
Operator | Use |
---|---|
&& | Logical AND. True only if all operands are true |
|| | Logical OR. True only if either one operand is true |
! | Not operator. It performs logical negation on an expression. |
Example
// Working of logical operators
#include
int main()
{
int m = 3, n = 3, p = 10, result;
result = (m == n) && (p > n);
printf("(m == n) && (p > n) is %d \n", result);
result = (m == n) && (p < n);
printf("(m == n) && (p < n) is %d \n", result);
result = (m == n) || (p < n);
printf("(m == n) || (p < n) is %d \n", result);
result = (m != n) || (p < n);
printf("(m != n) || (p < n) is %d \n", result);
result = !(m != n);
printf("!(m != n) is %d \n", result);
result = !(m == n);
printf("!(m == n) is %d \n", result);
return 0;
}
Output: (m == n) && (p > n) is 1
(m == n) && (p < n) is 0
(m == n) || (p < n) is 1
(m != n) || (p < n) is 0
!(m != n) is 1
!(m == n) is 0
Assignment Operator
Assignment operators are used to assigning value to a variable. The left side operand of the assignment operator is a variable and right side operand of the assignment operator is a value. The value on the right side must be of the same data-type of the variable on the left side otherwise the compiler will raise an error.
Operator | Use |
---|---|
= | a=b, value of b is assign to variable a |
+= | a+=b ; which is equal to, a=a+b |
*= | a*=b ; which is equal to a=a*b |
/= | a/=b ; which is equal to, a= a/b |
%= | a%=b; which is equal to, a=a%b |
Example
#include
int main()
{
int a,b=3;
a=b;// value of a =3
printf("The value of a is %d \n", a);
a+=b; // value of a =3+3 =6
printf("The value of a is %d \n", a);
a*=b; //value of a =a*b= 6*3=18
printf("The value of a is %d \n", a);
a/=b; // value of a = a/b =18/3 =6
printf("The value of a is %d \n", a);
a%=b; //remainder of 6/3 is 0
printf("The value of a is %d \n", a);
return 0;
}
Output: The value of a is 3
The value of a is 6
The value of a is 18
The value of a is 6
The value of a is 0
Increment and decrement Operator
Increment operator
The increment operator is used to increment the value of a variable in an expression. In the Pre-Increment, value is first incremented and then used inside the expression. Whereas in the Post-Increment, value is first used inside the expression and then incremented.
Example
#include
int main()
{
int a =2, b=2;
printf("The new value of a is %d \n", ++a);
printf("The value of b is %d \n",b++);
return 0;
}
Output: The new value of a is 3
The value of b is 2
Decrement Operator
The decrement operator is used to decrement the value of a variable in an expression. In the Pre-Decrement, value is first decremented and then used inside the expression. Whereas in the Post-Decrement, value is first used inside the expression and then decremented.
#include
int main()
{
int a =2, b=2;
printf("The new value of a is %d \n", --a);
printf("The value of b is %d \n",b--);
return 0;
}
Output: The new value of a is 1
The value of b is 2
Conditional Operator
It is also called ternary operator. It is very similar to if else statement but it is more compact. It consist of three expression as show in the syntax below
Expression1 ? Expression2:Expression3
Expression1 -> Checks the condition it could be true or false.
Expression2 -> If Expression1 condition is true then Expression 2 will execute.
Expression3-> If Expression1 condition is false then Expression 3 will execute.
# include
int main()
{
int a=3,b=6;
a
Bit wise Operator
These operators are used to perform bit operations on given two variables.
Special Operator
Operator | Use |
---|---|
& | This is used to get the address of the variable |
* | This is used as pointer to a variable. |
sizeof() | This gives the size of the variable. |
Example
# include
int main()
{
int a=5;
printf("Size of integer variable is %d \n", sizeof(a)); // a is int type, sizeof(a) a gives size of int which is 4 bits
int *b; // b is the pointer variable to store the adress of int variable
b=&a ; // b store the address of a
printf ("value of b is %d \n", *b ); //*b is use for dereferencing
return 0;
}
Output: Size of integer variable is 4
Value of b is 5
Operator Precedence
- Each operator is assigned a precedence level
- Multiplication and division have a higher precedence than addition and subtraction, so they are perform first
- Whatever is enclosed in parentheses is executed first, use parenthesis to group the expression.
Associativity
If the precedence of operator is same then associative rule will follow. Associativity can be either left to right or right to left. This could be confusing so instead of memorizing associativity you can always use parenthesis.
Functions
Function is self contained unit of a program code designed to accomplish particular task. A c program is collection of one or more function
Main () function
If a C program contains only one function, it must be main(). Execution of program begin with main() function.
User defined function
Declaration of function
return type function name (input parameters)
int add (int x, int y , int z); /*function declaration
Definition of function
int add (int x, int y, int z)
{
int d ;
d=x+y+z;
return (d);
}
What is Array?
Arrays allows you to group same data type variable together under a single name. We don’t need to create different different variable of the same type instead we can use array to store fixed number of variable of same type.
Array type is int, and name of the array is var_array, which can contain 5 integer values. Hence size of the array is 5. During declaration only, you have to give size of array explicitly
Declaration
#include
int main()
{
//int var_array[]; //wrong
int var_array[5]; //Correct
Initialization in the array
var_array[0]=2;
var_array[1]=5;
var_array[2]=6;
var_array[3]=8;
var_array[4]=10;
Initialization of array during declaration
We can declare and assign the array at the same time in this case we need not to give array size explicitly . (you can give but it is not mandatory)
int var_array[]={2,5,6,8,10}; //correct
int var_array[5]={2,5,6,8,10};//correct
Multidimensional Array
You can make any dimensions array, here we demonstrating two dimensional array which is most common one. In this array there is 4 rows and three column.
Note: Inner bracket in the code is not necessary but it’s use is to enhance the readability of the code .
int two_dim_array[4][3]={{1,2,3} // first row
{4,5,6}, //second row
{7,8,9},//Third row
{10,11,12} //fourth row
};
What is Storage Class in C ?
Storage classes use to give the following information about the variable
- Default value
- Storage
- Scope
- Life
Default value
If we don’t assign anything in the variable then depend upon storage class some value automatically assigned (It could be either garbage or zero) in the variable.
#include
main()
{
int x;//garbage value stored
}
Storage
Variable normally get the memory in the RAM but if any variable need to be called frequently to perform logics and instructions in CPU, it may directly store in CPU registers. In this way we essentially save the code run time.
Scope
Scope of the variable is the extent up to which particular variable would be accessible. It could be upto single block ‘{}’ or in entire program, depend on storage class.
Life
Life of the variable is the duration up to which allocation of the memory has been done in side the RAM for particular variable. Life of the variable could end after executing single block ‘{}’ or after finishing entire program. It completely depend on the storage class.
Type of Storage class
- Automatic
- Register
- Static
- External
Storage class | Keyword | Default value | Storage | Scope | Life |
---|---|---|---|---|---|
Automatic | auto | Garbage | RAM | Limited to block in which it is defined | Till the execution of the block |
Register | register | Garbage | cpu register | Limited to block in which it defined | Till the execution block |
Static | static | 0 | RAM | Limited to block in which it defined | Till the end of the program |
External | extern | 0 | RAM | Global | Till the end of the program |
Control Flow
Control flow break up the normal top to bottom flow of execution by employing decision making, looping and branching, enabling your program to conditionally execute particular block of the code.
- Decision-making statement (if-then, if-then-else, switch, goto)
- Looping statement (for, while, do-while)
- branching statement (break, continue, return)
Decision Making
- If a condition is true then a statement or statements are executed
- If a condition is false then other statements are executed.
Statement | Description |
---|---|
if statement | An if statement consists of a Boolean expression followed by one or more statements. |
if...else statement | An if statement can followed by an optional else statement, which executes when the boolean expression is false. |
nested if statements | You can use one if or else if statement inside another if or else if statements(s) |
Looping statement
In the loop statement a block of code repeat itself until particular condition becomes false.
- Loop become infinite loop if condition never becomes false.
// program for cheking enter number is graterthan, less than or equal to 5
#include
int main()
{
int num;
printf("please enter the number\n");
scanf ("%d", &num);
// under the 'if statement' if multiline code involved then it must be written inside the '{}'.
if (num >= 5)
{
if (num>5)
printf("number is greater than 5\n"); .
else
printf("number is equal to 5\n");
}
else
printf("number is less than 5 \n");
return 0;
}
input: 4
output: number is less than 5
Loop | Description |
---|---|
For loop | In for loop, the particular block of the code is repeated and loop is controlled by a variable. Each time the loop iterates, the predefined variable gets a new value. For loop is a set of three segments- Initialization, Condition, Updation |
While loop | It repeats or a group of statements while the given condition is true. It test the condition before executing the loop body. |
do...while loop | It is similar to while statement except it test the condition at the end of the loop body. |
Nested loop | You can use loop inside the loop. |