0% found this document useful (0 votes)
39 views42 pages

Day 2

The document discusses various topics in C programming including data types, input/output statements, type casting, operators, and operator precedence. It describes the different data types in C like char, int, float, etc. and their sizes and ranges. It also explains input functions like scanf() and output functions like printf() along with examples. The document further discusses implicit and explicit type conversions and arithmetic, relational, logical, and assignment operators supported in C.

Uploaded by

khitcsec3
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)
39 views42 pages

Day 2

The document discusses various topics in C programming including data types, input/output statements, type casting, operators, and operator precedence. It describes the different data types in C like char, int, float, etc. and their sizes and ranges. It also explains input functions like scanf() and output functions like printf() along with examples. The document further discusses implicit and explicit type conversions and arithmetic, relational, logical, and assignment operators supported in C.

Uploaded by

khitcsec3
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/ 42

DAY – 2

BASICS OF C
What will you learn?
 Data Types
 Storage Classes
 Type Casting
 Operators and Types
 Operators Precedence and Associativity
DATA TYPES
BASIC DATA TYPES IN C
Data types are used to specify two things to the compiler:

1. How much memory has to be allocated for an identifier to store


the data

2. What type of data item has been stored in an identifier


Data type Size in bytes Range Format specifier
char 1 -128 to + 127 %c

unsigned char 1 0 to 255 %c

signed char 1 -128 to + 127 %c

Int 2 -32768 to +32767 %d

unsigned int 2 0 to 65535 %u

signed int 2 -32768 to +32767 %d

short int 2 -32768 to +32767 %d

unsigned short int 2 0 to 65535 %d

signed short int 2 -32768 to +32767 %d

long int 4 -2147483648 to +2147483647 %ld

unsigned long int 4 0 to 424967295 %lu

signed long int 4 -2147483648 to +2147483647 %ld

float 4 3.4 E-38 to +3.4E+38 %f

double 8 1.7E-308 to +1.7E+308 %lf

long double 10 3.4E-4932 to 1.1E+4932 %Lf


INPUT AND OUTPUT STATEMENTS IN C
OUTPUT Statement:

 The printf ( ) function is used to display the information required by the


user and also prints the values of the variables.

 The syntax of the printf ( ) function can be given as

printf (“CONTROL STRING”, Variables list);

 This function accepts two arguments or parameters – control string and


variable list, where variable list is list of variables to be displayed and
these are displayed as formatted in the control string.

 Control string may also contain text, captions, identifiers or any other
text that is to be readable.
EXAMPLES:
int x;

 To input a value for x from Keyboard:

scanf(“%d”, &x);

 To display the value of x onto Screen:

printf(“%d”, x);
EXAMPLES:
float x;

 To input a value for x from Keyboard:

scanf(“%f”, &x);

 To display the value of x onto Screen:

printf(“%f”, x);
EXAMPLES:
 It is possible to read and write multiple variables at the same time
char x;
float y;
int z;

 To input a value for x from Keyboard:

scanf(“%c %f %d”, &x, &y, &z);

 To display the value of x onto Screen:

printf(“%c %f %d”, x, y, z);


EXAMPLES:
char x;

 To input a value for x from Keyboard:

scanf(“%c”, &x);

 To display the value of x onto Screen:

printf(“%c”, x);
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.


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.
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
Addition, subtraction, multiplication, division and modulo division are the
arithmetic operations that are supported by C and there are represented with
the symbols +, -, *, /, % respectively.

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
PRIORITY OF ARITHMETIC
OPERATORS
 High Priority: * / %
 Low Priority: + -

 2+3–5*2
 2 + 3 – 10
 5 – 10
 -5
RELATIONAL OPERATORS
Relational Operator is also called Comparison operator used to
compare two values. Relational expressions return true or false depending
upon the relation between the two values.

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.
EXAMPLE:
main()
{
int a=10,b=20,c=30,d,e;
d=a>b;
e=b<=c;
printf(“%d %d”,d,e);
getch();
}
Output:-
01
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
LOGICAL OPERATORS (CONTD.)
Truth Table for AND:

Cond A Cond B A && B


True True True
True False False
False True False Truth Table for NOT:
False False False
Cond A ! (Cond A)
Truth Table for OR: True False
False True
Cond A Cond B A || B
True True True
True False True
False True True
False False False
ASSIGNMENT OPERATOR

Assignment operator are used to assign the value or


an expression or a value of a variable to another variable

Expression

a
ASSIGNMENT OPERATORS
OPERATOR Expression DESCRIPTION

= 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
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.
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 operators accepts the operands to be integers but treat them as a


sequence of bits and then performs the operations, results are generated as
integers that are usable to the users.
Operator Description
& Bitwise AND
| Bitwise OR
~ Bitwise NOT (Complement)
<< Bitwise Left Shift
>> Bitwise Right Shift
^ Bitwise XOR
CONDITIONAL OPERATOR
 This is ternary operator used to perform some action based on some
condition.

 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.

 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 ;


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)))
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: Storage Memory Initial


Scope Life
a=10 a=10 a=10 Class Unit Value

Auto Stack Garbage Local End of Block


REGISTER STORAGE CLASS
#include<stdio.h> void main()
void subfun() {
{ 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