0% found this document useful (0 votes)
117 views30 pages

2.lecture 1-Basics and Precedence

Uploaded by

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

2.lecture 1-Basics and Precedence

Uploaded by

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

Character Set

• The character set of C represents alphabet, digit or


any symbol used to represent information.
Types Character Set

Uppercase Alphabets A, B, C, … Y, Z

Lowercase Alphabets a, b, c, … y, z

Digits 0, 1, 2, 3, … 9
~‘!@#%^&*()_-+=|\{} []
Special Symbols
:;"'<>,.?/
White spaces Single space, tab, new line.
©LPU CSE101 C Programming
Token
• Smallest unit in a program/statement.
• It makes the compiler understand what is written in the
program.
• Example: main, printf , name,), etc.
• Tokens are broadly classified as:
– Identifiers
– Keywords
– Constants
– Variables
– Strings
– Operators
– Special character

©LPU CSE101 C Programming


Identifiers
• So to identify things we have some name
given to them .
• Identifiers are the fundamental building blocks
of a program
• Used to give names to variables, functions,
constant, and user defined data.
• They are user-defined names and consist of a
sequence of letters and digits

©LPU CSE101 C Programming


Rules for naming an Identifier
1. An identifier name is any combination of 1 to 31
alphabets, digits or underscores.

2. The first character in the identifier name must be an


alphabet or underscore.

3. No blanks or special symbol other than an


underscore can be used in an identifier name.

4. Keywords are not allowed to be used as identifiers.

©LPU CSE101 C Programming


Some Identifiers
Tool_spanner;
both are different
tool_spanner;
FORMULA1;
engine_1;

Wrong identifiers name


1_engine;
break;
@car-roof;
©LPU CSE101 C Programming
Keywords
• Keywords are the reserved words whose
meaning has already been explained to the C
compiler.
• We cannot use these keywords as variables.
• Each keyword is meant to perform a specific
function in a C program.
• There are 32 keywords in C language.
• All keywords are written in lowercase only

Eg: The name of person can never be home, eat,


sleep, run, etc because these words have some
predefined meaning to perform some task.

©LPU CSE101 C Programming


List of C Keywords
auto double int struct

break else long switch

case enum register typedef

char extern return union

const float short unsigned

continue for signed void

default goto sizeof volatile

do if static while

©LPU CSE101 C Programming


Data Types
• Data type means the type of value a variable
will have.
• It also defines memory space for a particular
variable in computer.
• The type of value of variable can be alphabets
or numbers.
• The numbers can be further divided as the
integer or rational number.

©LPU CSE101 C Programming


Classification of Data Types
• In C data type is broadly classified as:
– Basic data types
– Derived data types
– User defined data types

©LPU CSE101 C Programming


Derived Data
Type
• Pointers
• Functions
• Array
Basic Data Type User Defined
• Integer Data Type
• Character • Structure
• Float • Union
• Double • Enumeration

Data
Type
©LPU CSE101 C Programming
List of Data Types
Type Size Minimal range
(bytes)
char 1 -128 to 127
unsigned char 1 0 to 255
int 2 or 4 -32768 to 32767
unsigned int 2 or 4 0 to 65535
short int 2 -32768 to 32767
unsigned short int 2 0 to 65535
long int 4 -2147483648 to 2147483647
unsigned long int 4 0 to 4294967295
float 4 3.4e-38 to 3.4e+38 with 6 digits of
precision
double 8 1.7e-308 to 1.7e+308 with 15 digits
of precision
long double 10 3.4e-4932 to 1.1e+4932 with 20
digits of precision

©LPU CSE101 C Programming


Constants
• The entity which do not change throughout
the execution are called constants.
• Types of constants:
– Integer constant
– Character constant
– Floating point constants
– String constants
Name of person remains same through out
the life, example: Amit, Shubnam, etc.

©LPU CSE101 C Programming


Variables
• Variable is an entity which may change.
• Variable is used to hold result and reserve
memory for the data.
datatype variable_name;
The naming of variable is done by following
the same rules of identifier naming.

Eg. What is your hobby?


The answer could be reading, dancing, drawing, etc.
So the answer to such questions may change during the life
time of the person

©LPU CSE101 C Programming


Rules for naming a Variable
1. An variable name is any combination of 1 to 31
alphabets, digits or underscores.

2. The first character in the variable name must be an


alphabet or underscore.

3. No blanks or special symbol other than an


underscore can be used in an variable name.

4. Keywords are not allowed to be used as variables.

©LPU CSE101 C Programming


Expressions
• Expressions are the statements or the instruction
given to computer to perform some operation.
• Every expression results in some value that can
be stored in a variable.
• Following are few example of expressions in
program:
– Expression to calculate speed of a car.
• Speed=distance/time
– Expression to find similarity of two things.
• c=value1>value2

©LPU CSE101 C Programming


• Expressions in C are basically operators acting on operands.
• An operand is an entity on which operation is to be performed.
Example: addition of two numbers, 5+8, these numbers will be
operands.

• An operator specifies the operation to be applied on operands.


Example: The addition, subtraction, etc will be operators

• Expressions are made of one or more operands.


• Statements like :
a = b + c,
++z
300 > (8 * k)

©LPU CSE101 C Programming


Operators
• Operator is the symbol which performs some
operations on the operands.

5+5=10 + and = are the operator and


5 and 10 are operands

©LPU CSE101 C Programming


Types of Operators
• Types of operators are:
1. Arithmetic operator
2. Unary operator
3. Relational operator
4. Logical operator
5. Assignment operator
6. Conditional operator
7. Bitwise operator
8. Special operator

©LPU CSE101 C Programming


Description of Operators

Arithmetic Operators
These are binary operators i.e. expression requires two operands
Operator Description Example (a=4 and b=2)
+ Addition of two operands a+b=6
- Subtraction of two operands a–b=2
* Multiplication of two operands a*b=8
/ Division of two operands a/b=2
% Modulus gives the remainder a%b=0
after division of two operands

©LPU CSE101 C Programming


Unary Operator
These operator requires only one operand.
Operator Description Example(count=1)
+ unary plus is used to show +count; value is 1
positive value
- unary minus negates the value of -count; value is -1
operand
++ Increment operator is used to ++count; value is 2
increase the operand value by 1 count++; value is 2
-- Decrement operator is used to --count; value is 1
decrease the operand value by 1 count--; value is 1

++count increments count by 1 and then uses its value as the value of the
expression. This is known a prefix operator.
count++ uses count as the value of the expression and then increments
count by 1. This is known as postfix operator.

©LPU CSE101 C Programming


Relational Operator
It compares two operands depending upon the their relation.
Expression generates zero(false) or nonzero(true) value.
Operator Description Example (a=10
and b=20)
< less than, checks if the value of left operand is less (a < b) value is 1(true)
than the value of right operand, if yes then condition
becomes true.
<= less than or equal to, checks if the value of left (a <= b) value is 1
operand is less than or equal to the value of right (true).
operand, if yes then condition becomes true.
> greater than, checks if the value of left operand is (a > b) value is 0 (not
greater than the value of right operand, if yes then true).
condition becomes true.
>= greater than or equal to, checks if the value of left (a >= b) value is 1
operand is greater than or equal to the value of right (true).
operand, if yes then condition becomes true.
== equality ,checks if the value of two operands is equal (a == b) value is 0
or not, if yes then condition becomes true. (not true).
!= inequality, checks if the value of two operands is equal (a != b) value is 1
or not, if values are not equal then condition becomes (true).
©LPU CSE101 C Programming
true.
Logical Operator
It checks the logical relationship between two expressions
and the result is zero( false) or nonzero(true).

Operator Description Example


&& Logical AND operator. If both the operands are (5>3 && 5<10) value is
true then condition becomes true. 1 (true).
|| Logical OR Operator. If any of the two (5>3 || 5<2) value is 1
operands is true then condition becomes true. (true).
! Logical NOT Operator. Use to reverses the !(8==8) value is 0
logical state of its operand. If a condition is (false).
true then Logical NOT operator will make
false.

©LPU CSE101 C Programming


Assignment Operator
They are used to assign the result of an expression on right side to a
variable on left side.
Operator Description Example(a=4 and b=2)
+= a=a+b a+=b; a=a+b = 6
-= a=a-b a-=b; a=a-b = 2
*= a=a*b a*=b; a=a*b = 8
/= a=a/b a/=b; a=a/b = 2
%= a=a%b a%=b; a=a%b = 0
>>= a=a>>b a=00000100 >> 2 = 00010000
<<= a=a<<b A=00000100 << 2 = 00000001
&= a=a&b (a=0100, b=0010) a&=b; a=a&b = 0000
|= a=a|b (a=0100, b=0010) a|=b; a=a|b =0110
^= a=a^b (a=0100, b=0010) a^=b; a=a^b = 0110

©LPU CSE101 C Programming


Conditional Operator
Conditional operator contains condition followed by two
statements. If the condition is true the first statement is
executed otherwise the second statement.
It is also called as ternary operator because it requires three
operands.

Operator Description Example


?: conditional expression, (a>b)? “a is greater”: “b is
Condition? Expression1: Expression2 greater”

©LPU CSE101 C Programming


Bitwise Operator
A bitwise operator works on each bit of data.
Logical Table Operator Description Example(a=1
a b a&b a|b a^b and b=0)
& bitwise AND a&b=0
0 0 0 0 0
0 1 0 1 1
| bitwise OR a| b = 1

1 1 1 1 0 ^ bitwise XOR a^b=1


1 0 0 1 1 ~ bitwise one’s ~a = 0, ~b=1
complement
<< bitwise left shift, 1101 << 1 = 1010
indicates the bits are
to be shifted to the
left.
>> bitwise right shift, 1101 >> 1 = 0110
indicates the bits are
to be shifted to the
right.
©LPU CSE101 C Programming
Some Special Operators

Operator Description Example


, comma operator, can be used to link int a, b, x;
the related expressions together
sizeof () sizeof operator to find the size of an int a; sizeof(a)=2
object.
type Cast operator, to change the data float x= 12.5;
type of the variable int a;
a = (int) x; value of a is 12.

©LPU CSE101 C Programming


Precedence of Operators
• The precedence of operators determine a rank
for the operators. The higher an operator's
precedence or priority, the higher “binding”it
has on the operands.

Example: So how the expression a * b + c will be interpreted?


(a * b) + c or a * (b + c),
here the first interpretation is the one that is used because the multiplication
operator has higher precedence than addition.

©LPU CSE101 C Programming


Associativity of Operators
• Associativity tell us the order in which several operators with
equal precedence are computed or processed in two
directions, either from left to right or vice-versa.

Example: In the expression


a * b / c,
since multiplication and division have the same precedence we must use the
associativity to determine the grouping. These operators are left associative
which means they are grouped left to right as if the expression was
(a * b) / c.

©LPU CSE101 C Programming


Operator Associativity Type
() [] . -> ++(postfix) - - (postfix) left to right Highest
+ - ++ -- ! & * ~ sizeof (type) right to left Unary
* / % left to right multiplicative
+ - left to right additive
<< >> left to right shifting
< <= > >= left to right relational
== != left to right equality
& left to right bitwise AND
^ left to right bitwise OR
| left to right bitwise OR
&& left to right logical AND
|| left to right logical OR
?: right to left conditional
= += -= *= /= &= |= ^= <<= >>= %= right to left assignment
, left to right comma
©LPU CSE101 C Programming
Parts of a C++ Program
comment
// sample C++ program
preprocessor
#include <iostream> directive
which namespace
using namespace std; to use

int main() beginning of


function named main
{ beginning of
block for main
output
cout << "Hello, there!"; statement
return 0; string
literal
} end of block
for main
send 0 to
operating system

2-30
©LPU CSE101 C Programming

You might also like