0% found this document useful (0 votes)
21 views56 pages

Module 1 Continued

PPT to learn C programming

Uploaded by

Parth Bhad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views56 pages

Module 1 Continued

PPT to learn C programming

Uploaded by

Parth Bhad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 56

DAY – 2

BASICS OF C
What will you learn?
 Storage Classes
 Type Casting
 Operators and Types
 Operators Precedence and Associativity
TYPE CONVERSION
Type conversion means converting from one data type to
another. Type Conversion is done when the expression has
variables of different data types.

'C' programming provides two types of type casting


operations:

1. Implicit Type Conversion


2. Explicit Type Conversion
IMPLICIT TYPE
CONVERSION
 When the types of the operands in an expression are
different, then the C automatically converts one type to
another. This is known as Implicit Type Conversion.

 To evaluate the expression, the data type is promoted from


the lower type to higher type in the hierarchy of data types.
This is known as Promotion.
 For example,
float x;
int y = 3;
x=y;
Now, x = 3.0,
CONTINUED.,
 But if we convert the higher data type to the lower data type then

the data has been lost. In this case demotion takes place. When

demotion occurs, the data has been lost.


 For example, if we want to convert the float type to int type then the

fractional part has been lost. Similarly, if we convert the double type to float

type then some precision positions are lost.


 Example:

float x = 10.56;

int y = x;

Here, while assigning the value of x into y, y can store only 10 and the fractional

part has been lost. But this conversion taken place without the knowledge of the

programmer, even the complier won’t show the warning or error.


EXAMPLE FOR IMPLICIT TYPE
CONVERSION():
//C Program for implicit type conversion

#include<stdio.h>

#include<conio.h> Output: Double value:4150.120000

void main() Integer value:4150

double value=4150.12; // Static Variable Declaration

int number; // Variable Declaration

clrscr();

printf(“Double value :%lf”,value);

number=value; //Convert double value into integer

printf(“Integer value :%d”,number);

getch();

}
EXPLICIT TYPE
CONVERSION
Type Casting is also known as Force Conversion. Type
casting is done when a higher data type is converted to lower
data type. But this conversion is under the control of the programmer,
not under the control of compiler.

Example:
float salary = 10000.00
int sal;
sal = (int)salary;
When the floating point numbers are converted to integers then
the decimal points are truncated. Therefore, the data is lost. So in order
to avoid such data loss always convert the lower data type to higher
data type but not the vice versa.
EXAMPLE FOR EXPLICIT TYPE
CONVERSION():
//C Program for explicit type conversion

#include<stdio.h>

#include<conio.h> Output: integer value:99

void main() Character value: a

int number=99; // Static Variable Declaration

char value;

clrscr();

printf(“Integer value :%d”,number);

value=(char)a; //Convert integer value into character variable

printf(“Character value :%c”,value);

getch();

}
OPERATORS OF C
An Operator is a symbol that performs some mathematical or logical
operation. In C there are different categories of Operators:

1. Arithmetic ( +, -, *, /, %)

2. Relational ( <, >, <=, >=, ==, != )

3. Logical (&&, ||, ! )

4. Assignment (=, +=, -=, *=, /=, %=)

5. Unary (++, --)

6. Bitwise ( &, |, ~, <<, >>, ^)

7. Conditional (? : )

8. Special Operators
ARITHMETIC
OPERATORS
An arithmetic operator performs mathematical operations such as Addition,
subtraction, multiplication, division and modulo division are the arithmetic operations
that are supported by C and there are represented with the symbols +, -, *, /, %

Syntax:
respectively.
operand1 arithmetic_operator operand2

Assume variable A holds 10 and variable B holds 20, then:


Operator Description Example

+ Adds two operands A + B will give 30

- Subtracts second operand from the first A - B will give -10

* Multiplies both operands A * B will give 200

/ Divides numerator by denominator B / A will give 2


Modulus Operator and remainder of after an
% integer division
B % A will give 0
//#include
C Program <stdio.h>
to demonstrate the working of arithmetic operators

void main()
{
int a=9,b=4;
printf("Addition of a, b is : %d\n", a+b);
//13
printf("Subtraction of a, b is : %d\n", a-b);
//5
printf("Multiplication of a, b is : %d\n", a*b);
//36
printf("Division of a, b is : %d\n", a/b);
//2
printf("Modulus of a, b is : %d\n", a%b);
//1
}
PRIORITY OF ARITHMETIC
OPERATORS
 High Priority: * / %
 Low Priority: + -

 2+3–5*2
 2 + 3 – 10
 5 – 10
 -5
RELATIONAL
OPERATORS
Relational operators are used to compare the
relationship between two operands.

Syntax: exp1 relational_operator exp2

 The value of a relational expression is either one or


zero.

 It is one if the specified relation is true and zero if the


relation is false.

 Relational operators are used by if , while and for


statements.
RELATIONAL
OPERATORS
Assume variable A holds 10 and variable B holds 20, then:

Operation Operator Description Test Result


Checks if the values of two operands are equal or
Equal to == not, if yes then condition becomes true. (A == B) is not true.

Checks if the values of two operands are equal or


Not equal not, if values are not equal then condition
!= becomes true. (A != B) is true.
to

Checks if the value of left operand is greater than


the value of right operand, if yes then condition
Greater becomes true.
> (A > B) is not true.
than
RELATIONAL
OPERATORS
Operation Operator Description Test Result
Checks if the value of left operand is
Less less than the value of right operand, if
< (A < B) is true.
than yes then condition becomes true.

Checks if the value of left operand is


Greater greater than or equal to the value of
than or >= right operand, if yes then condition (A >= B) is not true.
equal to becomes true.
Checks if the value of left operand is
Less less than or equal to the value of right
than or <= operand, if yes then condition becomes (A <= B) is true.
equal to true.
// C Program to demonstrate the working of relational operators
#include <stdio.h>
void main()
{
int a = 5, b = 5, c = 10;
printf("%d == %d = %d \n", a, b, a == b); // 1
printf("%d == %d = %d \n", a, c, a == c); // 0
printf("%d > %d = %d \n", a, b, a > b); // 0
printf("%d > %d = %d \n", a, c, a > c); // 0
printf("%d < %d = %d \n", a, b, a < b); // 0
printf("%d < %d = %d \n", a, c, a < c); // 1
printf("%d != %d = %d \n", a, b, a != b); // 0
printf("%d != %d = %d \n", a, c, a != c); // 1
printf("%d >= %d = %d \n", a, b, a >= b); // 1
printf("%d >= %d = %d \n", a, c, a >= c); // 0
printf("%d <= %d = %d \n", a, b, a <= b); // 1
printf("%d <= %d = %d \n", a, c, a <= c); // 1
}
LOGICAL OPERATORS
 Logical operators used to test more than one condition and
make decision. Yields a value either one or zero.

Syntax: operand1 logical_operator operand2


(or)

logical_operator operand

Example: (x<y) && (x= = 8)


LOGICAL OPERATORS
These set of Operators are used to combine two or more
relational expressions into a single compound expression
Operator Description
&& Logical AND
|| Logical OR
! Logical NOT

Logical AND (&&): This Operator gives TRUE Only when the all
the conditions in the expression evaluates to TRUE
Logical OR (||): This operator gives TRUE if at least any one of the
condition is evaluated to TRUE
Logical NOT (!): This Operator acts as Negation. It makes TRUE as
FALSE and FALSE as TRUE
// C Program to demonstrate the working of logical operators

#include <stdio.h>
void main()
{
int a = 5, b = 5, c = 10, result;
result = (a == b) && (c > b);
printf("(a == b) && (c > b) equals to %d \n", result); // 1
result = (a == b) && (c < b);
printf("(a == b) && (c < b) equals to %d \n", result); // 0
result = (a == b) || (c < b);
printf("(a == b) || (c < b) equals to %d \n", result); // 1
result = (a != b) || (c < b);
printf("(a != b) || (c < b) equals to %d \n", result); // 0
result = !(a != b);
printf("!(a == b) equals to %d \n", result); // 1
result = !(a == b);
printf("!(a == b) equals to %d \n", result); // 0
}
ASSIGNMENT OPERATOR
 Assignment operator are used to assign the value
or an expression or a value of a variable to another variable.
 Assignment Operator is denoted by =
Syntax: variable = expression;

Expression

a
TYPES OF ASSIGNMENT
Single Assignment Ex: a = 10;

Multiple Assignment Ex: a=b=c=0;

Compound Assignment Ex: c = a + b;


ASSIGNMENT
OPERATORS
OPERATO Expressio DESCRIPTION
R n
= a=b it assigns the value of variable b to a

a=b*2+3 it assigns the value of the expression


b*2+3 to a
+= a += b it is equivalent to a = a+b
-= a -= b it is equivalent to a = a-b
*= a *= b it is equivalent to a = a*b
/= a /=b it is equivalent to a = a/b
%= a %=b it is equivalent to a = a%b
// C Program to demonstrate the working of assignment
operators

#include <stdio.h>
void main()
{
int a = 5, c;
c = a;
printf("c = %d \n", c); // 5
c += a; // c = c+a
printf("c = %d \n", c); // 10
c -= a; // c = c-a
printf("c = %d \n", c); // 5
c *= a; // c = c*a
printf("c = %d \n", c); // 25
c /= a; // c = c/a
printf("c = %d \n", c); // 5
c %= a; // c = c%a
printf("c = %d \n", c); // 0
}
INCREMENT AND
DECREMENT
 Increment operator is denoted with the symbol ++. This
operator is used to increase the value of variable by 1.

 Increment operator has two variations:

1. Pre – Increment

Under this, ++ operator is prefixed with the variable name i.e.


++m. This means, first increment is done and the
incremented value is assigned to m.

2. Post – Increment

Under this, ++ operator is post fixed with the variable name


i.e., m++. This means that the assignment of incremented value to
m is postponed until the next statement.
INCREMENT (CONTD.)
 Suppose m = 5, y = ++m //prefix notation

In this case, the values of m and y would be 6.

 Suppose m = 5, y = m++ // post fix notation

In this case, the value of m would be 6 but the value of y


would be 5 only.

 Decrement Operator is denoted with the symbol --.

This operator is used to decrease the value of a variable


by 1. Its usage and purpose is similar to the increment operator
except that this operator decrements instead of increment.
// C Program to demonstrate the working of increment and
decrement operators

#include <stdio.h>
void main()
{
int a = 10, b = 100;

printf("++a = %d \n", ++a); //11


printf("--b = %d \n", --b); // 99
printf("a++ = %d \n", a++); // 11
printf("b-- = %d \n", b--); // 99

}
BITWISE OPERATORS
 Bitwise Operators are those that perform operations at the bit level.

 These operators include bitwise AND, bitwise OR, bitwise XOR, and shift

operators.

 Bitwise operator may not be applied for float and double.

 Manipulates the data which is in binary form.

Syntax: operand1 bitwise_operator operand2


Operator Description
& Bitwise AND
| Bitwise OR
~ Bitwise NOT (Complement)
<< Bitwise Left Shift
>> Bitwise Right Shift
^ Bitwise XOR
A B A&B A|B A^B
0 0 0 0 0
0 1 0 1 1
1 0 0 1 1
1 1 1 1 0

 Examples:
& Bitwise AND 0110 & 0011  0010
| Bitwise OR 0110 | 0011  0111
^ Bitwise XOR 0110 ^ 0011  0101
<< Left shift 01101110 << 2 
10111000
>> Right shift 01101110 >> 3 
00001101
~ One's complement ~0011  1100

 Don't confuse bitwise & | with logical && ||


 >> is a binary operator that requires two integral operands. the first
one is value to be shifted, the second one specifies number of bits to
be shifted.
 The general form is as follows:
variable >> expression;
 When bits are shifted right, the bits at the rightmost end are deleted.
 Shift right operator divides by a power of 2. I.e. a>>n results in
a/2n, where n is number of bits to be shifted.
Example: a=8;
b=a>>1; // assigns 4 after shift right operation
 << is a binary operator that requires two integral operands. the first
one is value to be shifted, the second one specifies number of bits to
be shifted.
 The general form is as follows:
variable << expression;
 When bits are shifted left, the bits at the leftmost end are deleted.
Example: a=8;
b=a<<1; // assigns 16 after left shift operation
 Shift left operator multiply by a power of 2, a<<n results in a*2n,
where n is number of bits to be shifted.
// C Program to demonstrate the working of Bitwise
operators

#include <stdio.h>

void main()
{
int m = 10,n = 20 ;
printf("AND_opr value = %d\n",m&n ); //0
printf("OR_opr value = %d\n",m|n ); //30
printf("NOT_opr value = %d\n",~m); //-11
printf("XOR_opr value = %d\n",m^n ); //30
printf("left_shift value = %d\n", m << 1); //20
printf("right_shift value = %d\n", m >> 1); //5
}
CONDITIONAL
OPERATOR
 The conditional operator first evaluates an expression for a true or false
value and then executes one of the two given statements depending
upon the result of the evaluation of condition.

 It is also known as ternary operator, as it operates on three


operands.

Syntax:

condition ? Expression 1: expression 2;

 If the condition is evaluated to true then the expression1 is executed,


otherwise expression 2 is executed.

Example: Big = a > b ? a : b ;

Advantage: It reduces the line of code when the condition and


statements are small.
EXAMPLE FOR TERNARY
OPERATOR():
//C Program to find largest among two numbers using ternary
operator.

#include<stdio.h>

#include<conio.h>

void main() output: m is greater than n that is 5


{

int m=5,n=4;

clrscr();

(m>n)?printf(“m is greater than n that is %d”,m)

:printf(“n is greater than m that is %d”,n);

getch();

}
SPECIAL OPERATORS
 There are some special operators available in C such as,

1.Comma operator

2.Sizeof operator

3.Member selection operators (. and ->)

4.Pointer operators(& and *)


COMMA OPERATOR
 It doesn’t operate on data but allows more than one expression to
appear on the same line.

 The comma operator ‘ , ’ has the lowest priority among all


the operators available in C.

 This operator is used to separate the related expressions.

 The expressions separated by comma operator need not be


included within the parenthesis.

Example: c= (a=5, b=10, a+b);

This expression first assign 5 to a, then assigns 10 to b and finally


(5+10) to c.
SIZEOF() OPERATOR
 It returns number of bytes the operand occupies.
Operand may be a variable, a constant, or a data type
qualifier.

 Syntax:

sizeof(operand);

 Example:

y=sizeof(int); //Here the value of y will be 2 bytes.


EXAMPLE FOR
SIZEOF():
//C Program to implement sizeof() function.

#include<stdio.h>

#include<conio.h>

void main()

int a,c; //Declaring an integer type variables

float b;

clrscr(); //To clear the screen of the console output

a=sizeof(char);

c=sizeof(b);

printf(“The sizeof character is %d”,a);

printf(“The sizeof float is %d”,c);

getch();

}
MEMBER OPERATORS
 These are used to access members of structures and
unions.

 Example:

Var.member1; // when var is a structures


variable.

Var->members2; // when var is a pointer to structures


variable.
POINTS TO REMEMBERED ON
OPERATORS
 Unary operator: It requires only one operand.

 Binary operator: It requires two operands like a+b, a-b, a*c etc.

 Logical operator: Used to compare two or more operands.


Operands may be variable constants or expression.

 Assignment operator: Used to combine the results of two or more


statements.

 Conditional operator: Checks the condition and executes the


statement depending on the condition.

 Bitwise operator: Used to manipulate the data at bit level. It


operates on integer only. It may not be applied to float or real.
PRECEDENCE AND
ASSOCIATIVITY OF OPERATORS
 Precedence determines the order in which different operators are

evaluated in an expression.

 Associativity determines the order in which operators with same

precedence are executed.

1. Left to Right Associativity evaluates starting on the left and moving to


the right

2. Right to Left Associativity evaluates starting on the right and moving


to the left
 While evaluating expressions, precedence is applied before Associativity.
LEFT TO RIGHT
ASSOCIATIVITY:
The following shows an example of left to right Associativity:
3*8/4%4*5
As all operators of the expressions are of same level
precedence then Associativity determines how the sub
expressions are grouped together as follows:
((((3 * 8) / 4) % 4) * 5)
(((24 / 4) % 4) * 5)
((6 % 4) * 5)
(2 * 5)
10
The value of the expression is 10.
RIGHT TO LEFT
ASSOCIATIVITY:
The following shows an example of right to left Associativity:

a += b *= c -= 5

As all operators of the expressions are of same level of precedence


and the assignment operators follow right to left Associativity then the
sub expressions are grouped as follows:

Therefore the given expression is evaluated as (a += (b *= (c -= 5)))

Which is expanded as (a += (b *= (c = c – 5)))

(a += (b = b * (c = c – 5)))

(a = a + (b = b * (c = c – 5)))
Example program to illustrate operator precedence
Example program to illustrate operator precedence (contd…)
STORAGE
CLASSES
STORAGE CLASSES
Storage Classes are used to describe about the features of a
variable/function.
Those features include:
1. Storage class of a variable determines the storage area
2. Storage class of a variable how long the variable exists – life
time of the variable
3. Storage class of a variable specifies the scope of the
variable
4. Storage class specifies the default value of a variable
C STORAGE CLASSES
C language supports the four storage classes:

1. Automatic

2. Register

3. Static

4. Extern

The general syntax for specifying the storage class of a


variable can be given as follows:

<storage_class> <data_type>
<variable_Name>;
STORAGE CLASSES
SUMMARY
Storage Memory
Initial Value Scope Life
Class Unit

Auto Stack Garbage Local End of Block

Register CPU Register Garbage Local End of Block

Data Local or Till the program


Static Zero
Segment Global ends

Data Till the program


Extern Zero Global
Segment ends
AUTO STORAGE
CLASS
#include<stdio.h> void main()
void subfun() {
{ int i;
auto int a = 10; subfun();
printf(“a=%d”,a); subfun();
a++; subfun();
}
}

Output: Storag Memor Initial


Scope Life
a=10 a=10 a=10 e Class y Unit Value
Garba End of
Auto Stack Local
ge Block
REGISTER STORAGE
CLASS
#include<stdio.h>
void subfun()
void main()
{
{ int i;
register int a = 10; subfun();
printf(“a=%d”,a); subfun();
a++; subfun();
} }

Output:
a=10 a=10 a=10 Storage Memory Initial
Scope Life
Class Unit Value
CPU
Register Garbage Local End of Block
Register
STATIC STORAGE
CLASS
#include<stdio.h>
void main()
static int a = 10;
{
void subfun()
int i;
{
subfun();
printf(“a=%d”, a);
subfun();
a++;
subfun();
}
}

Storage Memory Initial


Output: Scope Life
Class Unit Value
a=10 a=11 a=12
Data Local or Till the program
Static Zero
Segment Global ends
EXTERN STORAGE
CLASS
1. In large projects, it is common to decompose the problem into
modules, each module kept in separate source files.

2. The decomposed source files are compiled separately and linked


together to form a single unit.

3. It may require that a single variable is to be shared among the


multiple source files; this can be achieved by creating a variable
with the storage class extern.

4. A variable declared with extern storage class has file scope that
have the properties of the static storage class.

5. External variables are generally declared with the global scope.


An extern variable must be declared in all the source files
those reference it, but it can be define only in one of them. It is
an error if two external variables are initialized.

Program1.c Program2.c
Definition File Reference Source File
#include<stdio.h> #include<stdio.h>
int a; #include “program1.c”
int main( ) extern int a;
{ int main ( )
…………….. {
…………….. ……………
} …………….
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy