21CSS101J Programming For Problem Solving
21CSS101J Programming For Problem Solving
21CSS101J Programming For Problem Solving
UNIT-1
2
Pseudocode &
History and Evolution of C
Writing Pseudo code
Do’s
1. Use Control Structures.
2. Use appropriate naming conventions.
3. Indentation and white spaces are the key.
4. Keep it simple
5. Keep it concise.
Don’ts
1. Don’t be too generalized.
2. Don’t make the pseudo code abstract.
Advantages
• No standard is followed
• Compilation and Execution of the program cannot
be done.
• It may be time consuming to produce result.
Examples of Pseudo code
Start
Enter
Number A
Line1: Start Program
Enter Line 2: Enter two numbers, A, B
Number B Line 3: Add the numbers together
Line 4: Print C
Line 5: End Program
A+B=C
Print C
End
History and Evolution of C
#include<stdio.h>
int main()
{
printf("\n Welcome to the world of C ");
/* the above statement prints the
statements given in double quotes*/
return 0;
}
IDENTIFIERS
• Identifiers are names given to program elements
such as variables, arrays and functions.
Rules for forming identifier name:
▪ it cannot include any special characters or
punctuation marks (like #, $, ^, ?, ., etc) except the
underscore"_".
▪ There cannot be two successive underscores
KEYWORDS
▪ Keywords cannot be used as identifiers
▪ The names are case sensitive. So, example,
“FIRST” is different from “first” and “First”.
▪ It must begin with an alphabet or an underscore.
▪ It can be of any reasonable length. Though it should
not contain more than 31 characters.
▪ Example: roll_number, marks, name, emp_number,
basic_pay, HRA, DA, dept_code
VARIABLES
• Can provide one value for variables initialized in one
statement:
◻ int x, y, z = 0;
• Each variable declared and then initialized with the
value
Variables
Keywords:
❑ Keywords – Conveys special meaning to Compiler
❑ Cannot be used as variable names
❑ The following are the 32 reserved C keywords:
61
Introduction
Constants:
❑ Definition :Value does not change during execution
❑ Can be a Number (or) a Letter
❑ Types
❑ Integer Constants
❑ Real Constants
❑ Character Constant
❑ Single Character Constants
❑ String Constants
Introduction
Constants condt:
Introduction
Values:
There are two kinds of expressions in C −
• lvalue − Expressions that refer to a memory location are called "lvalue" expressions. An
lvalue may appear as either the left-hand or right-hand side of an assignment.
• rvalue − The term rvalue refers to a data value that is stored at some address in
memory. An rvalue is an expression that cannot have a value assigned to it which
means an rvalue may appear on the right-hand side but not on the left-hand side of an
assignment.
• Variables are lvalues and so they may appear on the left-hand side of an assignment.
• Numeric literals are rvalues and so they may not be assigned and cannot appear on the
left-hand side. Take a look at the following valid and invalid statements −
Names:
Function names and variable names are both identifiers in C.
• The following are all the characters you can use to make a valid variable name:
1. Characters A through Z and a through z.
2. Digit characters 0 through 9, which can be used in any position except the first
of a variable name.
3. The underscore character (_).
For instance, stop_sign, Loop3, and _pause are all valid variable names.
Now, let's see what you cannot use in variable naming:
• A variable name cannot contain any C arithmetic signs.
• A variable name cannot contain any dots (.).
• A variable name cannot contain any apostrophes (`).
• A variable name cannot contain any other special symbols such as *, @, #, ?, and
so on.
Some invalid variable names, for example, are, 4flags, sum-result, method*4, and
what_size?.
Introduction
Scope of Variable:
❑ Definition
❑ A scope in any programming is a region of the program where a
defined variable can have its existence and beyond that variable
it cannot be accessed
Scope of Variable:
Scope of Variable:
a) Local Variables
❑ Variables that are declared inside a function or block are called
local variables
❑ Local variables are created when the control reaches the block
Scope of Variable:
/* Program for Demonstrating Local Variables*/
#include <stdio.h> int main ( )
{
/* local variable declaration */
int a, b; int c;
/* actual initialization */
a = 10; b = 20;
c = a + b;
printf ("value of a = %d, b = %d and c = %d\n", a, b, c); return 0;
}
Introduction
Scope of Variable:
b) Global Variables
❑ Defined outside a function, usually on top of the program
❑ Hold their values throughout the lifetime of the program
❑ Can be accessed inside any of the functions defined for the
program
Scope of Variable:
/* Program for Demonstrating Global Variables*/
#include <stdio.h>
/* global variable declaration */
int g;
int main ( )
{
/* local variable declaration */
int a, b;
/* actual initialization */
a = 10; b = 20;
g = a + b;
printf ("value of a = %d, b = %d and g = %d\n", a, b, g); return 0;
}
Introduction
Binding:
❑ A Binding is an association between an entity and an attribute
❑ Between a variable and its type or value
❑ Between a function and its code
❑ Binding time is the point at which a binding takes place
❑ Types of Binding
a) Design Time
b) Compile Time
c) Link Time
d) Run Time
Introduction
Binding:
a) Design Time
❑ Binding decisions are made when a language is designed
❑ Example
❑ Binding of + to addition in C
b) Compile Time
❑ Bindings done while the program is compiled
❑ Binding variables to datatypes
❑ Example
❑ int a; float b; char c;
Introduction
Binding:
c) Link Time
❑ Compiled code is combined into a full program for C
❑ Example
❑ Global and Static variables are bound to addresses
d) Run Time
❑ Any binding that happens at run time is called Dynamic
❑ Any binding that happens before run time is called Static
❑ Values that are dynamically bound can change
Storage Classes
STORAGE CLASS
✔Storage Class
- Where to store
- Default Initial Value
- Scope
- Life
76
Four Types of Storage Class
•
Auto
• Every time new value
• Extern
Variable is defined Outside
{
int month;
auto int month;
}
.
Static-Example
#include <stdio.h>
/* function declaration */
void func(void);
static int count = 5; /* global variable */
main() { Output:
while(count--)
{
func(); i is 6 and count is 4
} i is 7 and count is 3
return 0;
} i is 8 and count is 2
/* function definition */ i is 9 and count is 1
void func( void )
{
i is 10 and count is 0
static int i = 5; /* local static variable */
i++;
printf("i is %d and count is %d\n", i, count);
}
Extern-Example
First File: main.c
#include <stdio.h>
Output:
int count ;
extern void write_extern();
count is 5
main() {
count = 5;
write_extern();
}
Second File: support.c
#include <stdio.h>
extern int count;
void write_extern(void) {
printf("count is %d\n", count);
}
Register-Example
{
register int miles;
}
Numeric Data Types : Integers and Floating
Point
Numeric Data Types : Integers and Floating Point
- Example: 12
45
1274
100000000
-3
-5735
Numeric Data Types : Integers and Floating Point
Examples:
String :
“ anything between
double quotes”
Numeric and Non-Numeric Data
Types-Example
Operators
Topics
- Unary operator
- ++ (the increment operator)
- -- (the decrement operator)
- either a prefix or postfix
position with different results
91
INCREMENT AND DECREMENT OPERATORS
++ Post-increment. aNumber++
-- Post-decrement. aNumber--
++ Pre-increment. ++yourNumber
-- Pre-decrement. --yourNumber
92
; INCREMENT AND DECREMENT OPERATORS
Example:
#include <stdio.h> Output:
int main() ++a = 11
{ --b = 99
int a = 10, b = 100; ++c = 11.500000
float c = 10.5, d = 100.5; ++d = 99.500000
printf("++a = %d \n", ++a);
printf("--b = %d \n", --b);
printf("++c = %f \n", ++c);
printf("--d = %f \n", --d);
return 0;
}
93
ASSIGNMENT OPERATORS
▪ Assignment Operators
• Assign value to an variable and denoted by =
• Binary operator, operates on two operands
• Have Two Values – L-Value and R-Value.
• Also be used for logical operations
ASSIGNMENT OPERATORS
= a=b a=b
+= a += b a = a+b
-= a -= b a = a-b
*= a *= b a = a*b
/= a /= b a = a/b
%= a %= b a = a%b
Comma
Comma (,) as Separator and Operator
• Separator -While declaration multiple variables and providing
multiple arguments in a function
• Operator-assign multiple values to a variable
struct student
{
char name[20],
int roll;
}*ptr
Bitwise Operator
Bitwise Operator
• Used in bit level programming
• Manipulate bits of an integer expression
• Logical, shift and complement are three types
Bitwise Operator
Example:
#include <stdio.h>
main()
{
unsigned int a = 60;
unsigned int b = 13;
int c = 0; Line 1 - Value of c is 12
c = a & b;
printf("Line 1 - Value of c is %d\n", c );
Line 2 –Value of c is 61
c = a | b; Line 3 - Value of c is 49
printf("Line 2 - Value of c is %d\n", c ); Line 4 - Value of c is -61
c = a ^ b; Line 5 - Value of c is 240
printf("Line 3 - Value of c is %d\n", c );
c = ~a; Line 6 - Value of c is 15
printf("Line 4 - Value of c is %d\n", c );
c = a << 2;
printf("Line 5 - Value of c is %d\n", c );
c = a >> 2;
printf("Line 6 - Value of c is %d\n", c );
}
Sizeof Operator
• calculate the size of data
type or variables.
• sizeof operator can be
nested.
• sizeof operator will return
the size in integer format.
#include <stdio.h>
int main()
{
int integerVar;
Output
printf("Size of char = %d\n", sizeof(char));
printf("Size of int = %d\n", sizeof(integerVar)); Size of char = 1
printf("Size of expression (3+2.5) = %d\n", Size of int = 4
sizeof(3 + 2.5)); s return 0; Size of expression
} (3+2.5) = 8
❖ Relational and Logical Operators
❖ Conditional Operators
❖ Operator Precedence
❖ Pre / Post increment operators
Relational Operators
Operator Meaning
< Is less than
<= Is less than or equal to
> Is greater than
>= Is greater than or equal to
== Is equal to
!= Is not equal to
102
Relational Operators
❖ General form
expression1 relational-operator expression2
4. x!=1 false 0
5. z==3 true 1
103
Relational Operators
Operators Meaning
&& Logical AND
|| Logical OR
! Logical NOT
❖ Example:
x<y && z!=5
❖ An expression which combines two or more relational
expressions is called as a logical expression or a compound
relational expression.
105
Logical Operators
107
Conditional Operators (Ternary operator)
Example:
#include<stdio.h>
main()
{
int a,b,c;
printf(“\nGive two numbers:”);
scanf(“%d%d”,&a,&b); /*a=15, b=10*/
c = (a > b) ? a : b;
printf(“\nThe Biggest value: %d”,c);
}
Output:
The Biggest value: 15
Operator Precedence
❖ Example:
( 3 * 5 ) + ( 4 / 2) – ( 1 * 2)
15 + 2 - 2
15
Operator Precedence
Pre / Post increment operator
• Syntax:
(pre)++var_name; (pre)--var_name;
(Or)
var_name++ (post); var_name -- (Post);
• Examples:
++a, --total, a--, i--
Increment and Decrement Operators Contd…
S.
No Operator type Operator Description
Pre Value of x is incremented by 1 before assigning it
1 Increment ++x to variable x.
Output
1, 2, 3,
Increment and Decrement Operators Contd…
Program execution:
Output
1, 2,
Increment and Decrement Operators Contd…
Program execution:
1
1
9
• This equation is equal to :
if (condition)
expression 1
else
expression 2
1
2
0
• Working of Ternary Operator
The ternary operator is an operator that takes three arguments.
The first argument is a comparison argument, the second is the
result upon a true comparison, and the third is the result upon a
false comparison.
<expression-1> ? <expression-2> : <expression-3> ;
Expression-1 ⇨ Conditional statement
Expression-2 ⇨ It will execute if condition in Expression-1
evaluate as TRUE.
Expression-3 ⇨ It will execute if Condition in Expression-1
evaluate as FALSE.
1
2
1
Example :
max = (a>b) ? a : b ;
This is equivalent to,
if (a>b) max = a;
else max=b;
Note - Ternary operator is right associative.
1
2
2
Examples
#include <stdio.h>
int main()
{
int x=1, y ;
y = ( x ==1 ? 2 : 0 ) ;
printf("x value is %d\n", x);
printf("y value is %d", y);
}
Output :
x value is 1
y value is 2
1
2
3
Example
#include<stdio.h>
int main( )
{
int num;
printf("Enter the Number : ");
scanf("%d",&num);
(num%2==0)?printf("Even\n"):printf("Odd");
}
Output
Enter the Number : 10
Even
1
2
4
Example
#include<stdio.h>
int main( )
{
int age;
printf(" Please Enter your age here: \n ");
scanf(" %d ", &age);
(age >= 18) ? printf(" You are eligible to Vote ") : printf(" You are not eligible to
Vote ");
return 0;
}
Output
Please Enter your age here: 19
You are eligible to Vote
1
2
5
Examples
#include<stdio.h>
int main( )
{
int a, b, max;
printf("Enter a and b: ");
scanf("%d%d", &a, &b);
max = a > b ? a : b;
printf("Largest of the two numbers = %d\n", max);
return 0;
}
Output
Enter a and b: 10 20
Largest of the two numbers = 20 12
6
Assignment Operator
• Uses the assignment operator (=)
• General syntax: variable_name = expression
• Assignment Operator is binary operator which operates on two
operands
• Assignment Operator have Two Values – L-Value and R-Value
• Operator = copies R-Value into L-Value
• Left of = is called L-value, must be a modifiable variable
• Right of = is called R-value, can be any expression
12
7
12
8
• L-Value stands for left value
• L-Value of Expressions refer to a memory locations
• In any assignment statement L-Value of Expression
must be a container(i.e. must have ability to hold the
data)
• Variable is the only container in C programming
thus L Value must be any Variable.
• L Value cannot be a Constant, Function or any of
the available data type in C.
12
9
Diagram Showing L-Value of Expression :
13
0
L-Value and R-Value of Expression Contd...
#include<stdio.h>
int main( )
{
int num;
num=5;
return(0);
}
Example of L-Value Expression
#include<stdio.h>
int main( )
{
int num;
5 = num; //Error
return(0);
}
L-value cannot be a Constant
#include<stdio.h> int main( )
{
const num;
13
1
#include<stdio.h>
#define MAX 20
int main( )
{
MAX = 20; //Error
return(0);
}
L-value cannot be a MACRO
#include<stdio.h>
enum {JAN,FEB,MARCH};
int main( )
{
13
3
Examples of R-Value of Expression
Variable Constant
Function Macro
Enum Constant Any other data type
13
4
References