Bit Pattern of the data can be shifted by specified number of Positions to Left
When Data is Shifted Left , trailing zero’s are filled with zero.
Left shift Operator is Binary Operator [Bi - two]
Binary means , Operator that require two arguments
Quick Overview of Left Shift Operator
Original Number A
0000 0000 0011 1100
Left Shift
0000 0000 1111 0000
Trailing Zero’s
Replaced by 0 (Shown in RED)
Direction of Movement of Data
<<<<<=======Left
Syntax : Bitwise Left Shift Operator
[variable]<<[number of places]
Live Example : Bitwise Operator [Left Shift Operator]
#include<stdio.h>intmain(){int a =60;printf("\nNumber is Shifted By 1 Bit : %d",a <<1);printf("\nNumber is Shifted By 2 Bits : %d",a <<2);printf("\nNumber is Shifted By 3 Bits : %d",a <<3);return(0);}
Output :
Number is Shifted By 1 Bit :120
Number is Shifted By 2 Bits :240
Number is Shifted By 3 Bits :480
Explanation of Left Shift Binary Operator :
We know the binary representation of the 60 is as below -
0000000000111100
Now after shifting all the bits to left towards MSB we will get following bit pattern -
In the previous topic we have studied Precedence of Value at and Address Operator of Pointer. In this chapter we will be looking more advance form of pointer. We are looking one step forward to learn how (++*ptr) works ?
Meaning of (++*ptr) in C Programming :
Consider the Following Example :
int num =20,*ptr ;
ptr =#printf("%d",++*ptr);
Explanation :
‘++’ and ‘*’ both have Equal Precedence
Associativity is from Right to Left ( Expression evaluated from R->L)
In the previous chapter, we have learnt about the memory organization of the pointer variable. In this chapter we are learning to compute or find the size of pointer variable.
What is the size of Pointer variable in C ?
Pointer is the variable that stores the address of another variable.
How to calculate size of Pointer ?
Consider the following memory map before declaring the variable.In the memory comprise of blocks of 1 byte.
Whenever we declare any variable then random block of memory is chosen and value will be stored at that memory location.
Similarly whenever we declare any pointer variable then random block of memory will be used as pointer variable which can hold the address of another variable.
Calculating Size of Pointer : Tips
Pointer stores the address of the Variable.
Size of Pointer Variable can be evaluated by using sizeof operator
Address of variable is considered as integer value.
Similarly if we declare the character array then it can hold only character. So in short character array can store character variables while floating array stores only floating numbers.
B. Array Used for Maintaining multiple variable names using single name
Suppose we need to store 5 roll numbers of students then without declaration of array we need to declare following -
int roll1,roll2,roll3,roll4,roll5;
Now in order to get roll number of first student we need to access roll1.
Guess if we need to store roll numbers of 100 students then what will be the procedure.
Maintaining all the variables and remembering all these things is very difficult.
Consider the Array :
int roll[5];
So we are using array which can store multiple values and we have to remember just single variable name.
C. Array Can be Used for Sorting Elements
We can store elements to be sorted in an array and then by using different sorting technique we can sort the elements.
Different Sorting Techniques are :
Bubble Sort
Insertion Sort
Selection Sort
Bucket Sort
D. Array Can Perform Matrix Operation
Matrix operations can be performed using the array.We can use 2-D array to store the matrix.
Matrix can be multi-dimensional
E. Array Can be Used in CPU Scheduling
CPU Scheduling is generally managed by Queue. Queue can be managed and implemented using the array. Array may be allocated dynamically i.e at run time. [Animation will Explain more about Round Robin Scheduling Algorithm | Video Animation]
F. Array Can be Used in Recursive Function
When the function calls another function or the same function again then the current values are stores onto the stack and those values will be retrieve when control comes back. This is similar operation like stack.
if-else-if Statement OR else if Ladder : Tutorial on If
if-else-if Statement OR else if Ladder :
Suppose we need to specify multiple conditions then we need to use multiple if statements like this -
voidmain(){int num =10;if( num >0)printf("\n Number is Positive");if( num <0)printf("\n Number is Negative");if( num ==0)printf("\n Number is Zero");}
which is not a right or feasible way to write program. Instead of this above syntax we use if-else-if statement.
Consider the Following Program -
voidmain(){int num =10;if( num >0)printf("\n Number is Positive");elseif( num <0)printf("\n Number is Negative");elseprintf("\n Number is Zero");}
Explanation :
In the above program firstly condition is tested i.e number is checked with zero. If it is greater than zero then only first if statement will be executed and after the execution of if statement control will be outside the complete if-else statement.
elseif( num <0)printf("\n Number is Negative");
Suppose in first if condition is false then the second condition will be tested i.e if number is not positive then and then only it is tested for the negative.
elseprintf("\n Number is Zero");
and if number is neither positive nor negative then it will be considered as zero.
Which is faster and feasible way for Writing If ?
Point
Example 1
Example 2
No of If Statements
3
2
No of times Condition Tested (Positive No.)
3
1
No of times Condition Tested (Negative No.)
3
2
No of times Condition Tested (Zero No.)
3
2
Flowchart for If-Else Ladder :
Difference between Declaration and Definition in C Programming
Some Examples of Declaration :
Declaration of Function :
Whenever we write function after the main function, compiler will through the error since it does not have any idea about the function at the time of calling function. If we provide prototype declaration of function then we compiler will not look for the definition.
int sum(int,int);main(){int res = sum(10,20);}int sum(int n1,int n2){return(n1+n2);}
In the above example , first line is called as function declaration.
int sum(int,int);
Declaring Variable
Whenever we write declaration statement then memory will not be allocated for the variable. Variable declaration will randomly specify the memory location.
int ivar;float fvar;
Variable Declaration Vs Definition Differentiation Parameters
A. Space Reservation :
Whenever we declare a variable then space will not be reserved for the variable.
Whenever we declare a variable then compiler will not look for other details such as definition of the variable.
Declaration is handy way to write code in which actual memory is not allocated.
struct book {int pages;float price;char*bname;};
In the above declaration memory is not allocated. Whenever we define a variable then memory will be allocated for the variable.
struct book b1;
B. What it does ?
Declaration will identify the data type of the identifier.
Definition of the variable will assign some value to it.
C. Re-Declaration Vs Re-Definition
In Both the cases we will get compile error. Re-declaration and Re-definition is illegal in C programming language. Re-Declaring the Variable in Same Scope :
main(){int num1;int num1;}
&
struct book {int pages;float price;char*bname;};struct book {int pages;float price;char*bname;};
above statement will cause error. Re-Defining the Variable :
int sum(int n1,int n2){return(n1+n2);}int sum(int n1,int n2){return(n1+n2);}
above definition will cause compile error.
Difference between Declaration and Definition
No
Declaration
Definition
1
Space is Not Reserved
In Definition Space is Reserved
2
Identifies Data Type
Some Initial Value is Assigned
3
Re-Declaration is Error
Re-Definition is Error
Some Re-commanded Reading List : Link 1 | Link 2 | Link 3
What is Data Type in C Programming ?
What is Data Type in C Programming ?
A Data Type is a Type of Data.
Data Type is a Data Storage Format that can contain a Specific Type or Range of Values.
When computer programs store data in variables, each variable must be assigned a specific data type.
Some of the Data Types Supported by C :
Data Type
keyword
Description
Integer Data Type
int
Stores the Integer Value
Float Data Type
float
Stores the Floating Point Value
Character Data Type
char
Stores the Single Character Value
Long Data Type
long
Stores the Long range Integer Value
Double Data Type
double
Stores the long range Floating Value
Explanation :
Whenever we declare variable in Computer’s memory, Computer must know the type of the data to be stored inside the memory.
If we need to store the single character then the size of memory occupied will be different than storing the single integer number.
The memory in our computers is organized in bytes. A byte is the minimum amount of memory that we can manage in C.
A byte can store a relatively small amount of data one single character or a small integer (generally an integer between 0 and 255).
Size Required to Store Variable of Different Data Types
Data Type
Borland C/C++ Compiler
Visual C++
Integer
2 Bytes
4 bytes
Float
4 Bytes
4 Bytes
Character
1 Byte
1 Byte
Long
4 Byte
8 Byte
1-D Array - Compile Time Initializing in C Programming
Different Methods of 1-D Array - Compile Time Initializing
Whenever we declare an array we can initialize array directly at compile time. Initialization of an array is called as compiler time initialization if and only if we assign certain set of values to array element before executing program. i.e at Compilation Time.
Different Ways Of Array Initialization :
Size is Specified Directly
Size is Specified Indirectly
A. Method 1 : Array Size Specified Directly
In this method , we try to specify the Array Size directly.
int num[5]={2,8,7,6,0};
In the above example we have specified the size of array as 5 directly in the initialization statement.Compiler will assign the set of values to particular element of the array.
num[0]=2
num[1]=8
num[2]=7
num[3]=6
num[4]=0
As at the time of compilation all the elements are at Specified Position So This Initialization Scheme is Called as “Compile Time Initialization“.
Graphical Representation :
B. Method 2 : Size Specified Indirectly
In this scheme of compile time Initialization, We does not provide size to an array but instead we provide set of values to the array.
int num[]={2,8,7,6,0};
Explanation :
Compiler Counts the Number Of Elements Written Inside Pair of Braces and Determines the Size of An Array.
After counting the number of elements inside the braces, The size of array is considered as 5 during complete execution.
This type of Initialization Scheme is also Called as “Compile Time Initialization“
Live Example :
#include <stdio.h>intmain(){int num[]={2,8,7,6,0};int i;for(i=0;i<5;i++){printf("\nArray Element num[%d] : %d",i+1,num[i]);}return0;}
Output :
Array Element num[1]:2
Array Element num[2]:8
Array Element num[3]:7
Array Element num[4]:6
Array Element num[5]:0
Basic Stack Operation : Data Structure
Primitive Basic Stack Operation in C
We know that Stack can be represented using an array. Stack is open at one end and operations can be performed on single end. We can have different primitive operations on Stack Data Structure.
We have declared data array in the above declaration. Whenever we add any element in the ‘data’ array then it will be called as “Pushing Data on the Stack”. Suppose “top” is a pointer to the top element in a stack. After every push operation, the value of “top” is incremented by one.
C. Pop Operation on Stack :
Whenever we try to remove element from the stack then the operation is called as POP Operation on Stack.
Some basic Terms :
Concept
Definition
Stack Push
The procedure of inserting a new element to the top of the stack is known as Push Operation
Stack Overflow
Any attempt to insert a new element in already full stack is results into Stack Overflow.
Stack Pop
The procedure of removing element from the top of the stack is called Pop Operation.
Stack Underflow
Any attempt to delete an element from already empty stack results into Stack Underflow.
Logical Operator in C Programming
Logical Operator in C Programming
Whenever we use if statement then we can use relational operator which tells us the result of the comparison, so if we want to compare more than one conditions then we need to use logical operators. Suppose we need to execute certain block of code if and only if two conditions are satisfied then we can use Logical Operator in C Programming.
Operator
Name of the Operator
&&
And Operator
||
Or Operator
!
Not Operator
Let us look at all logical operators with example -
#include<stdio.h>intmain(){int num1 =30;int num2 =40;if(num1>=40|| num2>=40)printf("Or If Block Gets Executed");if(num1>=20&& num2>=20)printf("And If Block Gets Executed");if(!(num1>=40))printf("Not If Block Gets Executed");return(0);}
Output :
Or If Block Gets Executed
And If Block Gets Executed
Not If Block Gets Executed
Explanation of the Program :
Suppose OR Operator is used inside if statement then if part will be executed if any of the condition evaluated to be true.
if(num1>=40|| num2>=40)
In the above if statement first condition is false and second condition is true , so if part will be executed.
if(num1>=20&& num2>=20)
In the above if statement both the conditions are true so if statement will be executed, AND Operator will look for truthness of first condition, If it founds the true condition then it will look for 2nd condition. If 2nd Condition founds to be true then it will look for next condition.
Logical Operator Chart :
Operator Applied Between
Condition 1
Condition 2
Final Output
AND
True
True
True
True
False
False
False
True
False
False
False
False
OR
True
True
True
True
False
True
False
True
True
False
False
False
NOT
True
-
False
False
-
True
Newsletter
Get C Programming Updates Via Email
Please Click on Activation Link in Your Mail to receive alerts !