C Programming UNIT 1
C Programming UNIT 1
CONTENTS:
Introduction to computer programming,
ALGORITHM
FLOW CHART,
PROGRAM DEVELOPMENT STEPS.
Computer languages:
o Machine level
o Assembly level
o High-level language.
Number Systems:
o DECIMAL NUMBER SYSTEM
o BINARY NUMBER SYSTEM
o OCTAL NUMBER SYSTEM
o HEXA DECIMAL NUMBER SYSTEM
o Conversions- decimal, binary, octal, hexadecimal.
‘C’ Fundamentals:
❖ STRUCTURE OF A C-PROGRAM
❖ C-CHARACTER SET
❖ C TOKENS-
➢ VARIABLES,
➢ CONSTANTS
➢ IDENTIFIERS
➢ DATA TYPES AND SIZES
➢ OPERATORS
1|Page
1. A Brief Introduction To Computers
2|Page
1.1 Binary System
To understand Computer System, we should understand Binary Number System. For this
we should understand Decimal System. In Decimal (base-10) system there are 10 different
digits. i.e., { 0,1,2,3,4,5,6,7,8,9}.
To prove a number as 7579 (Seven thousand Five hundred and Seventy Nine), we follow
weighted system. Each Decimal digit is having a weight... like unit (10 0), tens (101), hundred
(102), thousand (103),…,.and so on……
Now let us think about the range of a decimal system. Assume that you have three
A decimal digit is a space where you can place any digit from 0 to 9.
3|Page
Q1. What range of numbers you can represent with the digits.
Or
Q2. With 3 digits you can address how many students (by giving Roll no’s or Hall ticket no’s)?
Or
That means, the Range of numbers or the No of students or Houses we can address are
from 000 to 999 which are 1000.
✓ The range is 0 to 103 -1. (Here 103 because there are 3 digits).
✓ Why we should use 10 – powers – because we are in Decimal System.
In the same with N digits we can address 0 to 10N -1 students or houses or Line No’s. The range
is 0 to 10N -1.
1.2 Binary Numbers – a Brief Introduction
How to understand a binary system? Let us consider a binary number: 11010 (S 1:ON
S2:ON S3:OFF S4:ON S5:OFF), what is this binary number represents? What is the value of
11010 in decimal? For this we follow the same notation of Decimal. Each and every bit of a
binary number is also represented with weights (in the same way as decimal system) as shown
below.
A binary system is also a weighted system (20, 21, 22, 23,…, 2N) like Decimal system.
4|Page
In computer terminology to represent Binary digit - we use Binary bit. A bit can be either
Range in Binary
5|Page
Q1. Give me the range of binary numbers and their equivalent decimal numbers with 3 bits?
Or
Q2. With 3 bits how many students we can address by giving roll no’s or hall ticket no’s?
Or
22 21 20 =7
That means just like Decimal Number System the range with 3 binary bits is 000 to 2 3 -1
= 0 to 7.
The base.
8 bits
7 6 5 4 3 2 1 0 0 to 7
0 0 0 0 0 0 0 0 to 1 1 1 1 1 1 1 1
= 0 to 28-1
= 0 to 255
6|Page
The range of numbers we can represent with N bits is 0 to 2N-1.
Say what range of numbers we can represent with 2, 4, 6, 8, 10, 12, 14, 16, 32, 64 bits?
1. What is a Memory?
Anything which is capable of storing information is called Memory. (Eg. note books, text
Whenever we type through keyboard, the alphanumeric characters are stored in the main
memory in binary form (i.e. Zero’s or one’s) that is in terms of electrical voltages.
• The Main memory is a Volatile memory that is if the power goes down all the contents
stored will be removed or deleted or vanished. This is the main disadvantage with the
main memory. For example black board is a volatile memory with respect to water.
Means by throwing or pouring water on black board the written contents will be
• Hence, we have to save our programs or documents i.e., saving means copying the
contents from main memory to secondary memory, which is on Hard Disk. In Hard disk,
the contents are not deleted even though the power goes down.
7|Page
2. Logical view of Main memory
The Main memory looks like a LKG square book as shown below. In each square, we can
write only a single symbol. In Main memory, the square is called a Cell. In each cell, we can
place a Zero or One (which means that each cell is a switch and if 1 is placed in it, it will be ON
✓ The Line No’s are called as Location No’s in Computer terminology which specifies the
✓ In location 2, the data stored is 11000. Here location 2 is the Address and 11000 is the
Data. That means Line No’s or Location No’s represent the address of the Data.
Line 0 1 0 0 0 0 0 0 0
Line 1 1 1 1 1 1 0 1 1
Line 2 1 1 0 0 1 1 0 0
Line 3 1 1 0 0 1 1 0 1
8|Page
Line 4 1 1 0 1 1 1 0 0
Line 5
Line 6
Line 7
WHAT IS A PROGRAM?
execute instructions step by step in the given order. The order of execution is important. In our
day-to-day life, everything can be considered as a program. For example consider a situation -
coming to college.
Begin
9|Page
CENTRAL PROCESSING UNIT (CPU)
CPU Central Processing System: The CPU is a place where our programs are decoded
and executed. CPU can be viewed as a Brain of a Human being. To execute a program CPU
The brain or CPU is contains some internal memory – which is called as Registers in computer
terminology
Algorithm:
A step-by-step method of solving a problem or making decisions.
Properties of the algorithm
Example:
Write an algorithm to find the largest among three different numbers entered by user.
6. Step 1: Start
7. Step 2: Declare variables a,b and c.
8. Step 3: Read variables a,b and c.
10 | P a g e
9. Step 4: If a>b
10. If a>c
11. Display a is the largest number.
12. Else
13. Display c is the largest number.
14. Else
15. If b>c
16. Display b is the largest number.
17. Else
18. Display c is the greatest number.
19.Step 5: Stop
Example:
Step 1: Start
D←b2-4ac
Step 4: If D≥0
r1←(-b+√D)/2a
r2←(-b-√D)/2a
Else
rp←b/2a
11 | P a g e
ip←√(-D)/2a
Step 5: Stop
FLOW CHART
A flowchart is a type of diagram that represents an algorithm, workflow or process, showing
the steps as boxes of various kinds, and their order by connecting them with arrows. This
diagrammatic representation illustrates a solution model to a given problem. Flowcharts are
used in analyzing, designing, documenting or managing a process or program in various fields.
The following are some of the commonly used shapes used in flowcharts. Generally, flowcharts
flow from top to bottom and left to right.
12 | P a g e
Represented as a diamond (rhombus) showing where a decision
is necessary, commonly a Yes/No question or True/False test.
The conditional symbol is peculiar in that it has two arrows
coming out of it, usually from the bottom point and right point,
one corresponding to Yes or True, and one corresponding to No
Decision
or False. (The arrows should always be labeled.) More than two
arrows can be used, but this is normally a clear indicator that a
complex decision is being taken, in which case it may need to be
broken-down further or replaced with the "predefined process"
symbol. Decision can also help in the filtering of data.
14 | P a g e
PROGRAM DEVELOPMENT STEPS:
The following steps are used in sequence for developing an efficient program:
• Specifying the problem statement
• Designing an algorithm
• Coding
• Debugging
• Testing and Validating
• Documentation and Maintenance.
COMPUTER LANGUAGES
1)Machine code or machine language is a set of instructions executed directly by
a computer's central processing unit (CPU). Each instruction performs a very specific task, such
as a load, a jump, or an ALU operation on a unit of data in a CPU register or memory. Every
program directly executed by a CPU is made up of a series of such instructions.
It is represented in 0’s and 1’s … computers can understand this language only.
16 | P a g e
Example: ADD for addition
MOV for moving the data etc...
In computer science, a high-level programming language is a programming language with
strong abstraction from the details of the computer. In comparison to low-level programming
languages, it may use natural language elements, be easier to use, or may automate (or even
hide entirely) significant areas of computing systems (e.g. memory management), making the
process of developing a program simpler and more understandable relative to a lower-level
language. The amount of abstraction provided defines how "high-level" a programming
language is.
In the 1960s, high-level programming languages using a compiler were commonly
called autocodes.[2] Examples of autocodes are COBOL and Fortran.
1. Third Generation Languages: Earlier assembler programs produced only one machine
instruction for every one assembly language instruction. To speed up the process, these
languages are developed.
Example: BASIC, FORTRAN, COBOL, etc…
2. Fourth Generation Languages: To make the Programming easy, these languages are
developed. The instructions in these languages are more like English Words.
Number Systems
Human beings use decimal (base 10) number system for counting and measurements .
Computers use binary (base 2) number system, as they are made from binary digital
components (known as transistors) operating in two states – on (1’s) and off (0’s). In
computing, we also use hexadecimal (base 16) or octal (base 8) number systems, as
a compact form for represent binary numbers.
Each 1 and 0 is called a bit. 4 bits is called a nibble. 8 bits is called a byte. 16/32/64/128
bits is called a word.
Binary(2) 0 1
Octal (8) 0 1 2 3 4 5 6 7
Decimal (10) 0 1 2 3 4 5 6 7 8 9
Hexadecimal (16) 0 1 2 3 4 5 6 7 8 9 A B C D E F
17 | P a g e
2. Decimal to Binary Conversion
To convert a decimal number to its equivalent binary number representation, the easiest method
is to use repeated division by 2. Each time, save the remainder. The first remainder is the least
significant bit. The last remainder is the most significant bit.
3. Octal Numbers
Octal to Decimal Conversion Converting octal to decimal is just like converting binary to
decimal, except instead of powers of 2, we use powers of 8. That is, the LSB is 8 0, the next is
81, then 82, etc.
Decimal to Octal Conversion is just like converting decimal to binary, except instead of
dividing by 2, we divide by 8.
Octal to Binary conversion can be done by looking at 3 bit combinations, and then
concatenating them together.
Octal (8) 0 1 2 3 4 5 6 7 372 777
Binary 000 001 010 011 100 101 110 111 011111010 111111111
(2)
18 | P a g e
Binary 000 001 010 011 100 101 110 111 010111010 111111111
(2)
Octal (8) 0 1 2 3 4 5 6 7 372 777
4. Hexadecimal Numbering
The hexadecimal numbering system is the most common system seen today in representing raw
computer data. This is because it is very convenient to represent groups of 4 bits. Consequently,
one byte (8 bits) can be represented by two groups of four bits easily in hexadecimal.
Hex to Decimal Conversion is just like converting binary to decimal, except instead of powers
of 2, we use powers of 16. That is, the LSB is 160, the next is 161, then 162, etc.
Decimal to Hex Conversion is just like converting decimal to binary, except instead of dividing
by 2, we divide by 16.
19 | P a g e
Structure of C program
documentation section
link section
definition section
declaration part
execution part
function 1
function 2
function n
20 | P a g e
3. Definition section: The definition section defines all symbolic constants.
4. Global declaration section: There are some variables that are used in more than one
function. Such variables are called global variables and are declared in the global
declaration section that is outside of all the functions. This section also declares all the
user-defined functions.
5. Function prototype section: In this section the prototype (i. e., the nature of the function)
of the user defined function is specified to the compiler.
6. main () function section: Every C program must have one main function section. This
section contains two parts; declaration part and executable part
7. Declaration part: The declaration part declares all the variables used in the executable
part.
8. Executable part: There is at least one statement in the executable part. These two parts
must appear between the opening and closing braces. The program execution begins at
the opening brace and ends at the closing brace. The closing brace of the main function is
the logical end of the program. All statements in the declaration and executable part end
with a semicolon.
9. Subprogram section: The subprogram section contains all the user-defined functions that
are called in the main () function. User-defined functions are generally placed
immediately after the main () function, although they may appear in any order.
All section, except the main () function section may be absent when they are not required.
C Character Set :
Whenever we write any C program then it consists of different statements. Each C Program is
set of statements and each statement is set of different c programming lexims. In C
Programming each and every character is considered as single lexim. i.e [ Basic Lexical
Element ]
Uppercase Letters A to Z
Digits 0-9
21 | P a g e
Types Character Set
C Tokens
• In C Programming punctuation, individual words ,characters etc are called tokens.
• Tokens are basic building blocks of C Programming
Token Example :
1 Keyword do while
3 Constants -76 89
5 Special Symbol * @
6 Operators ++ /
22 | P a g e
Basic Building Blocks and Definition :
Token Meaning
Special Symbols other than the Alphabets and Digits and white-
Symbol spaces
23 | P a g e
32 Keywords in C Programming Language
do if static while
[box]A Variable is a name given to the memory location where the actual data is stored.[/box]
Consider real time example , suppose we need to store water then we can store water in glass if
quantity of water is small and Bucket is the quantity of water is large. And big can to store
water having quantity larger than bucket similarly Variable (i.e Value container) may have
different size for storing different verities of values.
24 | P a g e
3. This would Overwrite the earlier value 5 since memory location can hold only one
value at a time
4. Since the location ‘x’can hold Different values at different time so it is refered as
‘Variable‘
5. In short Variable is name given to Specific memory location or Group.
25 | P a g e
Initially before declaring variable ,We have Only memory.
1. Now we have declared a variable. (in this case we have declared character variable)
2. Compiler Checks data type . Depending on data type it will allocate appropriate bytes of
memory. (in this case Compile will allocate 1 byte because we have declared Character
data type)
3. It’s only declaration so , garbage value inside that address remains the same.
26 | P a g e
Step 3 : Initialize Variable in C Programming
Conclusion :
1. Characters Allowed :
o Underscore(_)
o Capital Letters ( A – Z )
o Small Letters ( a – z )
o Digits ( 0 – 9 )
27 | P a g e
2. Blanks & Commas are not allowed
3. No Special Symbols other than underscore(_) are allowed
4. First Character should be alphabet or Underscore
5. Variable name Should not be Reserved Word
Valid Names
num
Num
Num1
_NUM
NUM_temp2
Invalid Names
number 1
num 1
addition of program
Valid Identifier
num_1
number_of_values
status_flag
28 | P a g e
Tip 4 : First Character must be underscore or Alphabet
Valid Identifier
_num1
Num
Num_
_
__
Invalid Identifier
1num
1_num
365_days
• C is case sensitive.
• Variable name should not be Reserve word.
• However if we capitalize any Letter from Reserve word then it will become legal variable
name.
Valid Identifier
iNt
Char
Continue
CONTINUE
Invalid Identifier
int
29 | P a g e
char
continue
What is Constant ?
Constant in C means the content whose value does not change at the time of execution of a
program.
Definition :
Explanation :
30 | P a g e
Constant Type of Value Stored
We can declare constant using const variable. Suppose we need to declare constant of type
integer then we can have following two ways to declare it –
const int a = 1;
int const a = 1;
above declaration is bit confusing but no need to worry, We can start reading these variables
from right to left. i.e
Declaration Explanation
31 | P a g e
Primary Constant :
• Integer
• Float
• Character
Secondary Constant :
• Array
• Pointer
• Structure
• Union
• Enum
32 | P a g e
Mantissa Integer Part of Real Number Has Optional +/- Sign
Note :
Illustrative Examples :
4567.45 4.56745e3
0.00045 4.5e-4
Consider Example 2:
33 | P a g e
C Identifiers:
Identifiers are used as the general terminology for the names of variables, functions and arrays.
These are user defined names consisting of arbitrarily long sequence of letters and digits with
either a letter or the underscore (_) as a first character.
There are certain rules that should be followed while naming C identifiers:
1. They must begin with a letter or underscore (_).
2. They must consist of only letters, digits, or underscore. No other special character is
allowed.
3. It should not be a keyword.
4. It must not contain white space.
5. It should be up to 31 characters long as only first 31 characters are significant.
Examples
Name Remark
_A9 Valid
Temp.var Invalid as it contains special character other than the underscore
void Invalid as it is a keyword
Long Data Type long Stores the Long range Integer Value
34 | P a g e
Data Type keyword Description
Double Data Type double Stores the long range Floating Value
Explanation :
1. Whenever we declare variable in Computer’s memory, Computer must know the type of
the data to be stored inside the memory.
2. If we need to store the single character then the size of memory occupied will be different
than storing the single integer number.
3. The memory in our computers is organized in bytes. A byte is the minimum amount of
memory that we can manage in C.
4. A byte can store a relatively small amount of data one single character or a small integer
(generally an integer between 0 and 255).
Integer 2 Bytes
Float 4 Bytes
Character 1 Byte
Long 4 Bytes
1. Integers are whole numbers with a wide range of values that are machine dependent.
2. Integer occupies 2 bytes memory space and its value range limited to -32768 to +32767
1. Integer
2. Short Integer
3. Long Integer
36 | P a g e
Syntax of Declaring Integer
int num1;
short int num2;
long int num3;
• 500
• 6764
• 32100
OPERATORS:
Arithmetic Operators
arithmetic operators are shown in the following table. Arithmetic operators are used to perform
arithmetic operations in c programming.
+ Addition Operator 10 + 20 = 30
- Subtraction Operator 20 – 10 = 10
/ Division Operator 20 / 10 = 2
% Modulo Operator 20 % 6 = 2
#include <stdio.h>
int main()
{
int num1,num2;
int sum,sub,mult,div,mod;
printf("\nEnter First Number :");
37 | P a g e
scanf("%d",&num1);
printf("\nEnter Second Number :");
scanf("%d",&num2);
sum = num1 + num2;
printf("\nAddition is : %d",sum);
sub = num1 - num2;
printf("\nSubtraction is : %d",sub);
mult = num1 * num2;
printf("\nMultiplication is : %d",mult);
div = num1 / num2;
printf("\nDivision is : %d",div);
mod = num1 % num2;
printf("\nModulus is : %d",mod);
return(0);
}
Output :
Assignment Operators
38 | P a g e
1. Assignment Operator is Used to assign value to an variable.
2. Assignment Operator is denoted by equal to sign
3. Assignment Operator is binary operator which operates on two operands.
4. Assignment Operator have lower precedence than all available operators but has higher
precedence than comma Operator.
int main() {
int value;
value = 55;
return (0);
}
int value;
value = 55.450;
printf("%d",value);
Assignment operator can type cast higher values to lower values. It can also cast lower values to
higher values. Shorthand assignment operator is used express the syntax that are already
available in shorter way
39 | P a g e
Operator Name of the Equivalent
Example
symbol operator construct
+= Addition assignment x += 4; x = x + 4;
Subtraction
-= x -= 4; x = x – 4;
assignment
Multiplication
*= x *= 4; x = x * 4;
assignment
/= Division assignment x /= 4; x = x / 4;
Remainder
%= x %= 4; x = x % 4;
assignment
Unary operators:
unary minus:
The minus operator changes the sign of its argument. A positive number becomes negative, and
a negative number becomes positive.
int a = 10;
int b = -a; // b = -10
unary minus is different from subtraction operator, as subtraction requires two operands.
Increment Operators are used to increased the value of the variable by one and Decrement
Operators are used to decrease the value of the variable by one in C programs.
Both increment and decrement operator are used on a single operand or variable, so it is
called as a unary operator. Unary operators are having higher priority than the other operators
it means unary operators are executed before other operators.
40 | P a g e
Syntax
++ // increment operator
-- // decrement operator
Note: Increment and decrement operators are can not apply on constant.
Example
• pre-increment
• post-increment
In pre-increment first increment the value of variable and then used inside the expression
(initialize into another variable).
Syntax
++ variable;
Example pre-increment
#include<stdio.h>
#include<conio.h>
void main()
{
int x,i;
i=10;
x=++i;
printf("x: %d",x);
printf("i: %d",i);
getch();
41 | P a g e
}
Output
x: 11
i: 11
In above program first increase the value of i and then used value of i into expression.
post-increment (variable ++)
In post-increment first value of variable is used in the expression (initialize into another
variable) and then increment the value of variable.
Syntax
variable ++;
Example post-increment
#include<stdio.h>
#include<conio.h>
void main()
{
int x,i;
i=10;
x=i++;
printf("x: %d",x);
printf("i: %d",i);
getch();
}
42 | P a g e
Output
x: 10
i: 11
In above program first used the value of i into expression then increase value of i by 1.
Type of Decrement Operator
• pre-decrement
• post-decrement
Pre-decrement (-- variable)
In pre-decrement first decrement the value of variable and then used inside the expression
(initialize into another variable).
Syntax
-- variable;
Example pre-decrement
#include<stdio.h>
#include<conio.h>
void main()
{
int x,i;
i=10;
x=--i;
printf("x: %d",x);
printf("i: %d",i);
getch();
}
43 | P a g e
Output
x: 9
i: 9
In above program first decrease the value of i and then value of i used in expression.
post-decrement (variable --)
In Post-decrement first value of variable is used in the expression (initialize into another
variable) and then decrement the value of variable.
Syntax
variable --;
Example post-decrement
#include<stdio.h>
#include<conio.h>
void main()
{
int x,i;
i=10;
x=i--;
printf("x: %d",x);
printf("i: %d",i);
getch();
}
Output
x: 10
i: 9
In above program first used the value of x in expression then decrease value of i by 1.
44 | P a g e
Relational operators in C: Relational operators are used to find the relation between
two variables. i.e. to compare the values of two variables in a C program.
• In this program, relational operator (==) is used to compare 2 values whether they are
equal are not.
• If both values are equal, output is displayed as “values are equal”. Else, output is
displayed as “values are not equal”.
• Note: double equal sign (==) should be used to compare 2 values. We should not single
equal sign (=).
|| Or Operator
! Not Operator
#include<stdio.h>
int main()
{
int num1 = 30;
int num2 = 40;
45 | P a g e
if(num1>=40 || num2>=40)
printf("Or 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
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.
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 :
46 | P a g e
Operator Applied Condition Condition Final
Between 1 2 Output
False - True
where
• expression1 is Condition
• expression2 is Statement Followed if Condition is True
• expression2 is Statement Followed if Condition is False
47 | P a g e
Meaning of Syntax :
1. Expression1 is nothing but Boolean Condition i.e it results into either TRUE or FALSE
2. If result of expression1 is TRUE then expression2 is Executed
3. Expression1 is said to be TRUE if its result is NON-ZERO
4. If result of expression1 is FALSE then expression3 is Executed
5. Expression1 is said to be FALSE if its result is ZERO
#include<stdio.h>
int main()
{
int num;
flag = ((num%2==0)?1:0);
if(flag==0)
printf("\nEven");
else
printf("\nOdd");
}
int main()
{
int num;
(num%2==0)?printf("Even"):printf("Odd");
48 | P a g e
}
Comma Operator
#include<stdio.h>
void main() {
int num1 = 1, num2 = 2;
int res;
res = (num1, num2);
printf("%d", res);
}
Explanation :
1. Comma Operator has Lowest Precedence i.e it is having lowest priority so it is evaluated
at last.
2. Comma operator returns the value of the rightmost operand when multiple comma
operators are used inside an expression.
3. Comma Operator Can acts as –
o Operator : In the Expression
Separator : Declaring Variable , In Function Call Parameter List
o
Usage of Comma Operator
1. Function calls
2. Function definitions
3. Variable declarations
4. Enum declarations
49 | P a g e
Comma as Operator
res = (num1, num2);
In this case value of rightmost operator will be assigned to the variable. In this case value of
num2 will be assigned to variable res.
Examples of comma operator :
Comma Operator have lowest priority in C Programming Operators.
sizeof operator
printf("%d", sizeof(ivar));
printf("%d", sizeof(cvar));
printf("%d", sizeof(fvar));
return 0;
}
Output :
214
In the above example we have passed a variable to sizeof operator. It will print the value of
variable using sizeof() operator.
50 | P a g e
(type) operator:
(type) operator is used for type conversion. i. e., one type of data type is converted into
another type of data type.
Example:
int a = 10;
float b;
b = (float) a;
in above, value in variable a is converted into float. i.e., 10.00000 is assigned to variable b.
Bitwise Operators in C
& Binary AND Operator copies a bit to the result if it exists in both operands. (A & B) =
12, i.e.,
0000 1100
^ Binary XOR Operator copies the bit if it is set in one operand but not both. (A ^ B) =
49, i.e.,
0011 0001
~ (~A ) = -60,
i.e,. 1100
Binary Ones Complement Operator is unary and has the effect of 'flipping'
0100 in 2's
bits.
complement
form.
<<
Binary Left Shift Operator. The left operands value is moved left by the A << 2 =
51 | P a g e
number of bits specified by the right operand. 240 i.e.,
1111 0000
>> Binary Right Shift Operator. The left operands value is moved right by the A >> 2 = 15
number of bits specified by the right operand. i.e., 0000
1111
Example
Try the following example to understand all the bitwise operators available in C −
#include <stdio.h>
main() {
int c = 0;
c = a | b; /* 61 = 0011 1101 */
c = a ^ b; /* 49 = 0011 0001 */
52 | P a g e
printf("Line 4 - Value of c is %d\n", c );
When you compile and execute the above program, it produces the following result −
Line 1 - Value of c is 12
Line 2 - Value of c is 61
Line 3 - Value of c is 49
Line 6 - Value of c is 15
53 | P a g e
54 | P a g e