Unit 1 C
Unit 1 C
A Binary number system has only two digits that are 0 and 1. Every
number (value) represents with 0 and 1 in this number system. The
base of binary number system is 2, because it has only two digits.
Octal number system has only eight (8) digits from 0 to 7. Every
number (value) represents with 0,1,2,3,4,5,6 and 7 in this number
system. The base of octal number system is 8, because it has only 8
digits.
Flowchart
The flowchart shows the steps as boxes of various kinds, and their
order by connecting the boxes 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.
Advantages of Flowchart in C:
Disadvantages of Flowchart in C:
Examples of flowchart:
Example 1:
Example 2:
Design a flowchart for finding the largest among three numbers
entered by the user.
Problem Analysis
Identify the issues. Be clear about what the problem …
Understand everyone’s interests. …
Decision Tree
A decision tree is a graphical representation used to make decisions
and predict outcomes by mapping various possible choices and their
possible consequences.
A Decision Tree is a flowchart-like structure used to make decisions
based on a series of conditions. In C programming, this is often
represented by nested if-else or switch-case statements.
Root Node:
o The topmost node in a decision tree represents the entire
dataset or the initial question that needs to be answered.
Decision Nodes:
o These internal nodes represent decision points or tests on
an attribute.
Branches:
o This is the connectors between nodes that represent the
outcome of a decision or test.
o Example: “Yes” or “No” based outcomes.
Leaf Nodes (Terminal Nodes):
o Endpoints of a decision tree that represent the outcome or
decision.
Working Mechanism
Use/Applications
Characteristics
Conditions:
o These are the different scenarios or criteria that need to be
evaluated.
Actions:
o These are the possible outcomes or decisions that will be
executed based on the conditions.
Condition Entries:
o These are the possible states or values for each condition.
Action Entries:
o These are the specific actions to be taken for each set of
conditions.
Structure of a Decision Table
A typical decision table consists of four quadrants:-
Actions
In this table:
Customer Type and Order Amount > 500 are the conditions.
Offer Discounts, Free Shipping, and Priority Handling are
the actions.
The rules specify different combinations of conditions and the
corresponding actions.
Pseudo code
Example:
Fundamentals:
Character Set
Keywords
Identifiers
Data types
Letters
Uppercase Letters: A to Z
Lowercase Letters: a to z
Digits
Special Characters
comma , slash /
period . backslash
semicolon ; percentage %
White Spaces
Blank Spaces
Tab
Carriage Return
New Line
2. Keywords
32 Keyword
for float go if
int age;
float height;
Here, int is a keyword which declares age as a variable of integer
data type.
3. Identifiers
Data types
float 4 %f
1.2E-38 to 3.4E+38
double 8 %lf
1.7E-308 to 1.7E+308
#include <stdio.h>
int main()
int a = 9;
int b = -9;
int c = 89U;
return 0;
#include <stdio.h>
int main()
{
char a = 'a';
char c;
return 0;
Constants
The constants refer to fixed values that the program may not alter
during its execution. These fixed values are also called literals.
Constants can be of any of the basic data types like
the name
the value
int a;
float b;
int i=10;
Expressions:
An expression is a formula in which operands are linked to each other
by the use of operators to compute a value. An operand can be a
function reference, a variable, an array element or a constant.
Eg. 6*2/8 + 8 * 2
Statements:
1.Expression Statements
Examples
i = 0;
i++;
a = 5 + i;
y = (m * x) + b;
2.Compound Statement
A sequence of statements within a pair of curly braces { } forms a
single compound statement or block.
{
float speed;
3.Control Statements
Eg.
If( )
}
What is a C Operator?
An operator in C can be defined as the symbol that helps us to
perform some specific mathematical, relational, bitwise,
conditional, or logical computations on values and variables
c = a + b;
Here, ‘+’ is the operator known as the addition operator, and ‘a’ and
‘b’ are operands
Types of Operators in C
1. Arithmetic Operations in C
Adds two
+ Plus a+b
1 numeric values.
Subtracts right
– Minus operand from a–b
2 left operand.
Multiply two
* Multiply a*b
3 numeric values.
Divide two
/ Divide a/b
4 numeric values.
Returns the
remainder after
% Modulus diving the left a%b
operand with the
5 right operand.
#include <stdio.h>
int main()
int a = 25, b = 5;
return 0;
Output
a + b = 30
a - b = 20
a * b = 125
a/b=5
a%b=0
2. Relational Operators in C
The relational operators in C are used for the comparison of the two
operands. All these operators are binary operators that return true or
false values as the result of comparison.
These are a total of 6 relational operators in C:
S. No. Symbol Operator Description Syntax
#include <stdio.h>
int main()
int a = 25, b = 5;
return 0;
Output
a<b :0
a>b :1
a <= b: 0
a >= b: 1
a == b: 0
a != b : 1
4. Logical Operator in C
Returns true
Logical if both the
&& a && b
AND operands are
1 true.
Returns true
if both or any
|| Logical OR of the a || b
operand is
2 true.
Returns true
Logical
! if the operand !a
NOT
3 is false.
// C program to illustrate the logical operators
#include <stdio.h>
int main()
int a = 25, b = 5;
return 0;
Output
a && b : 1
a || b : 1
!a: 0
5. Assignment Operators in C
#include <stdio.h>
int main()
int a = 25, b = 5;
// using operators and printing results
return 0;
Output
a = b: 5
a += b: 10
a -= b: 5
a *= b: 25
a /= b: 5
a %= b: 0
6.Conditional Operator ( ? : )
The conditional operator is the only ternary operator in C++.
Here, Expression1 is the condition to be evaluated. If the
condition(Expression1) is True then we will execute and return
the result of Expression2 otherwise if the condition(Expression1)
is false then we will execute and return the result of Expression3.
Syntax
operand1 ? operand2 : operand3;
// C program to find largest among two
#include <stdio.h>
int main()
int m = 5, n = 4;
m, n)
n, m);
return 0;
Output
m is greater than n that is 5 > 4
7.Unary operators in C
Unary operators are the operators that perform operations on a
single operand to produce a new value.
. 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
#include <stdio.h>
int main()
{
int a = 100;
int b = -a;
printf("a = %d\n", a);
printf("b = %d",b);
return 0;
}
Output
a = 100
b = -100
2. Increment
The increment operator ( ++ ) is used to increment the value of the
variable by 1. The increment can be done in two ways:
2.1 prefix increment
In this method, the operator precedes the operand (e.g., ++a). The
value of the operand will be altered before it is used.
Example:
int a = 1;
int b = ++a;
2.2 postfix increment
In this method, the operator follows the operand (e.g., a++). The
value operand will be altered after it is used.
Example:
int a = 1;
int b = a++;
int c = a;
// C program to illustrate increment
#include <stdio.h>
int main()
{
int a = 5;
int b = 5;
printf("Pre-Incrementing a = %d\n", ++a);
printf("Post-Incrementing b = %d", b++);
return 0;
}
Output:
Pre-Incrementing a =6
Post-Incrementing b =5
3.Decrement
The decrement operator ( — ) is used to decrement the value of the
variable by 1. The decrement can be done in two ways:
3.1 prefix decrement
In this method, the operator precedes the operand (e.g., – -a). The
value of the operand will be altered before it is used.
Example:
int a = 1;
int b = --a;
3.2 postfix decrement
In this method, the operator follows the operand (e.g., a- -). The
value of the operand will be altered after it is used.
Example:
int a = 1;
int b = a--;
int c = a;
// C program to illustrate decrement
#include <stdio.h>
int main()
{
int a = 5;
int b = 5;
printf("Pre-Decrementing a = %d\n", --a);
printf("Post-Decrementing b = %d", b--);
return 0;
}
Output:
Pre-Decrementing a =4
Post-Decrementing b =5
4. sizeof() operator
#include <stdio.h>
int main()
return 0;
}
Output
Size of double: 8
Size of int: 4