SlideShare a Scribd company logo
C-Language
Basics
C - Language
It is developed by Dennis Ritchie, in the year 1972 at AT & T
Bell Labs, California.
C is a Middle level language , that contains certain additional
features that allows it to be used at a lower level , acting as
bridge between machine language and the high level languages.
general –purpose structured programming language.
Instructions of C language consists of terms that are very
closely same to algebraic expressions, consisting of certain
English words such as if, else, for ,do, while, etc.
Assembly Language : ADD A,B
To learn any language, we must know the basic concepts of
that language. These concepts are known as tokens.
There are 6 tokens of c-language:
1. Character set
2. Keywords
3. Identifiers
4. Data Types
5. Operators
6. Statements
Tokens of C
Character set :
 All keyboard keys and some extra characters that are not present on the
keyboard. For example, the symbols used in playing cards.
 There are total 256 characters.( 0 to 255)
 Every character has a fixed and unique value. This value is known as
ASCII value.
 For example, the ASCII VALUE of:
 A = 65 to Z=90
 a = 97 to z = 122
 0 = 48 to 9 = 57
Tokens of C
Keywords :
 Standard pre-defined words.
 Always written in lowercase alphabets.
 Its meaning cannot be changed.
 It cannot be use as a variable name.
 There are 32 keywords in C and 48 in C++.
 E.g., if, else, break, continue, return, for, while, do, etc.
Tokens of C
Keywords :
Tokens of C
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
Identifiers :
 User defined words.
 Can be written in lowercase, UPPERCASE or MixedCase alphabets.
 Its value is not changed.
 It cannot be use as a variable name.
 Value is assigned using #define statement.
 E.g., #define MAX 100
#define Pi 3.1415
Tokens of C
Data Types
Pre-defined User-defined Derived
Pointer
Numeric Non-numeric Enumerated Array Structures Union
Integer Real char
int float
long double
long double
Tokens of C
Pre-defined data types
Data
type
Size Format
Specification
Range Description
char 1b %c -128 to +127
Stores a single
character in single
quotes.
e.g., char c = ‘M’;
int 2b %d -32768 to +32767
Stores a value
without decimal point
and with optional +/-
sign.
long 4b %ld
-2147483648 to
+2147483647
Same as int, with
large range and size.
Pre-defined data types
Data type Size Format
Specification
Range Description
float 4b %f
-3.4e-38 to 3.4e+38
e = 10 to the
power…
-3.4 X 10-38
Stores a value with
decimal point and with
optional +/- sign. It stores
a number with a decimal
precision of 6 decimal
places.
E.g., float x = 6.75;
Will store 6.750000 in x.
double 8b %lf
-1.7e-308 to
1.7e+308
Same as float, with large
range and size.
long
double
10b %LF
-3.4e-4932 to
1.1e+4932
Hybrid of long and
double.
Operators :
 There are 3 categories of operators.
 Unary
 Binary
 Ternary
 Based on above 3 categories, there are 8 types of operators.
1. Arithmetic 2. Relational
3. Logical 4. Conditional
5. Assignment 6. Increment / Decrement
7. Bitwise 8. Special
 Every operator consists of Priority and Associativity.
Tokens of C
Arithmetic Operators (+, -, *, /, %) :
Priority : 1st : *, /, %
2nd : +, -
Associativity : Left to Right.
It means, if 2 same priority operators are present in an expression, then
Left to Right, which ever comes first will be executed first.
Normal Division / : It gives QUOTIENT as final answer. E.g., A = 5 / 2 = 2
Modulo Division % : It gives REMAINDER as final answer. E.g., A = 5 % 2 = 1
float z = 4.5 % 1.5 ;…error
Arithmetic Operators (+, -, *, /, %)
Integer Arithmetic :
1. int / int = int(Always) e.g., 5 / 2 = 2
2. int / float = float e.g., 5 / 2.0 = 2.500000
3. float / int = float e.g., 5.0 / 2 = 2.500000
4. float / float = float e.g., 5.0 / 2.0 = 2.500000
Rule 1: If numerator is less than denominator and both are int, then
answer of Normal Division is always 0.(2/5 = 0)
Rule 2: If numerator is less than denominator and both are int, then
answer of Modulo Division is always Numerator. (2%5 = 2)
Arithmetic Operators (+, -, *, /, %)
e.g., int A = 4 + 8 / 3 * 9 % 5 – 3;
= 4 + 2 * 9 % 5 – 3;
= 4 + 18 % 5 – 3;
= 4 + 3 – 3;
= 7 - 3;
= 4
What is the output of following expressions:
int x = 5 - 4 / 3 * 10 % 6 * 2 + 9;
int y = 81 % 15 * 20 / 3 – 9 + 10;
Arithmetic Operators (+, -, *, /, %)
Priority : All are on same priority
Associativity : Left to Right.
= = : Comparison operator
!= : Not Equal to
The answer of these operators is always TRUE or FALSE, YES or NO,
1 or 0.
e.g., int A = 7 < 9; = ?
Relational Operators (< , <=, >, >=, = =, !=)
These operator works on decimal number system and gives answer in
binary(0/1). It works on Truth Table.
Every non-zero number is considered as 1.
Logical AND(&&) : If any input is zero, output is zero.
Logical OR( || ) : If any input is one, output is one.
Logical NOT( ! ) : Output is inverse of input.
Priority :
1st : Logical NOT (Unary)
2nd : Logical AND
3rd : Logical OR
Associativity : Left to Right
Logical Operators (&&, ||, !)
e.g.,
int a = -25 && 9 = 1 && 1 = 1
int b = -25 || 0 = 1 || 0 = 1
int c = !25 = !1 = 0
Logical Operators (&&, ||, !)
It is a decision control operator. Its syntax is:
False
(Test condition) ? Statement – A : Statement – B;
True
Statement C;
Test condition is true, Statement A  C
Test condition is false, Statement B  C
Associativity : Right to Left
It is the only ternary operator.
Conditional Operator (?:)
Conditional Operator (?:)
e.g.,
int Age;
(Age >=18) ? printf(“You can vote.”):printf(“You cannot vote.”);
printf(“ Stop”);
Output:
Let A = 21…You can vote. Stop
Let A = 15…You cannot vote. Stop
Conditional Operator (?:)
e.g.,
int A, B, C;
B = (A >=10) ? (A <= 20) ? 50 : 30 : 20;
C = A + B;
printf(“ A=%d, B=%d, C=%d”, A, B, C);
Output:
A B C
15 50 65
30 30 60
8 20 28
Associativity : Right to Left… int c = 10; // 10 is assigned to c
NOTE: It is never used with any control statement. It will always give
incorrect result, because the condition will always be TRUE.
Its priority is lowest than the operators above it.
e.g., In statement, c = a + b,
‘+’ operator is executed first and then the ‘=‘ operator is executed.
Similarly for relational, logical and conditional operators.
Assignment Operators ( = )
These are Unary operators.
It is of two types:
1. Pre Increment/Decrement ( ++a / --a) a = a + 1
2. Post Increment/Decrement ( a++ / a--)
Associativity : Right to Left
Priority : 1st : Pre Increment/Decrement ( ++a / --a)
2nd : Post Increment/Decrement ( a++ / a--) Lower than ‘=‘
Increment/ Decrement Operators ( ++ / -- )
e.g.,
int a = 2, b = 3, c, d;
c = a++ * b++;
d = ++a * ++b;
c = a++ * b++; Since the operator is post and its priority is lower than
assignment operator, so compiler will solve the ‘*’ and ‘=‘ operators first.
c = a * b = 2 * 3 = 6….c = 6, a++…a = 3, b++…b = 4
d = ++a * ++b ; Since the operator is Pre, its priority is Highest, so
d = 4 * 5 = 20…d = 20, ++a… a = 4, ++b…b = 5
FINAL ANSWER: a = 4, b = 5, c = 6, d = 20
Increment/ Decrement Operators ( ++ / -- )
Solve : int a = 12, b = 9, c, d, e;
c = ++a % b--;
d = a + b * c++;
e = a++ + --b + c + d--;
What is the final value of a, b, c, d, e ? Harshali : a = 14 b = 8 c=5 d=44 e = 70
c = 13 % 9…c = 4, a = 13, b = 8
d = 13 + 8 * 4 = 13 + 32 = 45… d= 45, a = 13, b = 8, c = 5
e = 13 + 7 + 5 + 45 = 70… a = 14, b = 7, c = 5, d = 44, e = 70
Increment/ Decrement Operators ( ++ / -- )
These operator works on binary number system and gives answer in
decimal number system.
Every non-zero number is converted into binary first and then the
operations are performed.
Associativity : Left to Right
Priority : 1st : Bitwise Compliment ( ~ ) (Unary)
2nd : Bitwise Left Shift ( << )
3rd : Bitwise Right Shift ( >> )
4th : Bitwise And ( & )
5th : Bitwise XOR ( ^ )
6th : Bitwise OR ( | )
Bitwise Operators (&, |, ~, <<, >>, ^)
e.g., int a = 10; printf(“ %d”, ~a);
0 000 1010 = a in binary.
When you apply NOT operator It flips all the bits.
1 111 0101 is now equal to a.
Since we declared a as signed the first bit will be defining the sign,
0 for +; 1 for - sign.
And, since 2s complement is used to represent negative numbers in binary we are
going to be finding the number like this:
When the first bit is 1, meaning negative, think 1s as zero; 0s as one (except the first
bit). Then find the number and add 1.
Lets do it together: 1 111 0101 we will think 1s zero, zeros as 1. (First bit stays there)
1 000 1010, so this equals 10. You add 1, then you get 11. Then you put negative
sign. So you get -11.
Bitwise Compliment ( ~ )
e.g., int a = 15;
~a = ?
check the stored number, if it is a positive number, add 1 to it and convert
the result from positive to negative…
a = 15 + 1 = 16…-16 ( Put a negative sign before final answer)
Int b = -15; ~b ?
check the stored number, if it is a negative number, subtract 1 from it and
convert the result from negative to positive…
b = 15 -1 =14…
Bitwise Compliment ( ~ )
For performing this operation, convert the decimal number in binary and
represent it in nearest 8-bit representation. E.g.,
int x = 12 << 1;
Binary of 12 = 1100
8-bit representation of 12 = 0000 11000
(shift, ‘0’, and pad a ‘0’ to the right).
Collect the number, ignoring 1st 3 0s. = 11000.
Convert it to decimal = 24
+ 23
= 16 + 8 = 24
RULE : If we left shift a number once, then the answer is double of last
input.(Or the result is obtained by multiplying the last input by 2).
What is the values of int y = 12 << 4; ?
Bitwise Left Shift (<<)
For performing this operation, convert the decimal number in binary and
represent it in nearest 8-bit representation. E.g.,
int x = 12 >> 1;
Binary of 12 = 1100
8-bit representation of 12 = 0000 1100
(shift, rightmost bit and , and pad a ‘0’ to the left). 000001100
Collect the number, ignoring 1st 5 0s. = 110.
Convert it to decimal = 22
+ 21
= 4 + 2 = 6
RULE : If we right shift a number once, then the answer is half of last
input.(Or the result is obtained by dividing the last input by 2).
What is the values of int y = 22 >> 3; ?
Bitwise Right Shift (>>)
For performing this operation, convert the decimal number in binary and
represent it in nearest 8-bit representation. E.g.,
int x = 12 & 7;
Binary of 12 = 1100
Binary of 7 = 0111
0100 (Result)
Convert result in decimal = 0100 = 22
= 4
Bitwise AND ( & )
For performing this operation, convert the decimal number in binary and
represent it in nearest 8-bit representation. E.g.,
Bitwise XOR : If inputs are same, output is zero.
int x = 12 ^ 7;
Binary of 12 = 1100
Binary of 7 = 0111
1011 (Result)
Convert result in decimal = 1011 = 23
+ 21
+ 20
= 8 + 2 + 1
= 11
Bitwise XOR ( ^ )
For performing this operation, convert the decimal number in binary and
represent it in nearest 8-bit representation. E.g.,
int x = 12 | 7;
Binary of 12 = 1100
Binary of 7 = 0111
1111 (Result)
Convert result in decimal = 1111 = 23
+ 22
+ 21
+ 20
= 8 + 4 + 2 + 1
= 15
Bitwise OR ( | )
These operator are special purpose operators.
, : Comma operator (Separator)
. : Dot / Period operator
.*, -> : Memory dereferencing operators
sizeof( ) : It returns the size of data type or variable passed to it. E.g.,
int x = sizeof(float); will store 4 in x.
int y = sizeof(y); will store 2 in y.
Special Operators (, , . , .*, ->, sizeof( ) )
Statements :
There are 4 types of statements used in any C-language program.
1. Pre-processor statements.
2. Type declaration statements.
3. Input-output statements.
4. Arithmetic / Control statements.
Every c-language program starts with main( ) function.
Tokens of C
1. Pre-processor statements :
 These statements are prefixed by ‘#’.
 These statements are not compiled.
 The main pre-processor statements that are commonly used are:
 File Inclusion. ( #include < > )
 Identifier / Macro substitution ( #define )
2. Type declaration statements :
These statements are the very first statements of any c-program.
Using these statements, we inform the compiler about the number of
variables and the type of data that they are going to hold. E.g.,
int a, b, c;
float x, y;
char ch;
Statements in C- language
3. Input-Output statements :
 For input, we use scanf( ) function, whose definition is stored in stdio.h
file.
 For output, we use printf( ) function, whose definition is stored in stdio.h
file. E.g.,
int x;
float y;
printf(“ Enter the value of x and y = “);
scanf(“ %d %f”, &x, &y);
printf(“ Value of x = %d, Value of y = %d”, x, y);
4. Arithmetic / Control statements :
A statement that consists of arithmetic operators are known as an
Arithmetic statement.
Statements in C- language
Escape Sequences
Sometimes, it is necessary to use characters which cannot be typed or has special
meaning in C programming. For example: newline(enter), tab, question mark etc. In
order to use these characters, escape sequence is used.
For example: n is used for newline. The backslash (  ) causes "escape" from the
normal way the characters are interpreted by the compiler.
Escape Sequences Character
b Backspace
f Form feed
n Newline
r Return
t Horizontal tab
v Vertical tab
 Backslash
' Single quotation mark
" Double quotation mark
? Question mark
0 (zero) Null character
Comments
There are 2 different ways to give comments in any program.
1. Multi – line comments ( c- style)
2. Single – line comments ( c++ - style)
1. Multi – line comments ( c- style) : This is written as follows:
/ * This is the method of using a
multi-line comment in any c-
program. These sentences are not compiled. */
2. Single – line comments ( c- style) : This is written as follows:
// This is the method of using a
// multi-line comment in any c-
// program. These sentences are not compiled.
Sample Program
Output
Sample Program
I have made a very small change in the program and now check the
output.
Output
Sample Program
I only want to print up to 2 decimal places. Check the last printf( )
statement and then check the output.
Output
Exercise
Write a program to:
1. Print area and circumference of a circle.
2. Print area and perimeter of a rectangle.
3. Convert temperature in Celsius to Fahrenheit.
4. Print area of triangle using Heron’s formula.
5. Print Average of 3 numbers.
6. Print sum of individual digits of a 5-figit number.
7. Print reverse of a 5-digit number.
8. To swap 2 values using 3 variables.
9. To swap 2 values using 2 variables.
10. To print distance between 2 points.

More Related Content

What's hot (19)

PPTX
C programming language tutorial
javaTpoint s
 
PPTX
What is c
Nitesh Saitwal
 
PPTX
C Programming basics
Jitin Pillai
 
PPT
C program
AJAL A J
 
PDF
12 computer science_notes_ch01_overview_of_cpp
sharvivek
 
PDF
C Programming
Adil Jafri
 
PPTX
C Programming Unit-2
Vikram Nandini
 
PPTX
C Language (All Concept)
sachindane
 
PPS
C programming session 01
Dushmanta Nath
 
PPT
Getting started with c++
K Durga Prasad
 
PDF
Constants Variables Datatypes by Mrs. Sowmya Jyothi
SowmyaJyothi3
 
PPT
Declaration of variables
Maria Stella Solon
 
PDF
C programming_MSBTE_Diploma_Pranoti Doke
Pranoti Doke
 
PDF
Tutorial on c language programming
Sudheer Kiran
 
PPT
C program compiler presentation
Rigvendra Kumar Vardhan
 
PPTX
C Programming Unit-1
Vikram Nandini
 
PPT
Basics of c++
Huba Akhtar
 
PDF
C programming language
Mahmoud Eladawi
 
C programming language tutorial
javaTpoint s
 
What is c
Nitesh Saitwal
 
C Programming basics
Jitin Pillai
 
C program
AJAL A J
 
12 computer science_notes_ch01_overview_of_cpp
sharvivek
 
C Programming
Adil Jafri
 
C Programming Unit-2
Vikram Nandini
 
C Language (All Concept)
sachindane
 
C programming session 01
Dushmanta Nath
 
Getting started with c++
K Durga Prasad
 
Constants Variables Datatypes by Mrs. Sowmya Jyothi
SowmyaJyothi3
 
Declaration of variables
Maria Stella Solon
 
C programming_MSBTE_Diploma_Pranoti Doke
Pranoti Doke
 
Tutorial on c language programming
Sudheer Kiran
 
C program compiler presentation
Rigvendra Kumar Vardhan
 
C Programming Unit-1
Vikram Nandini
 
Basics of c++
Huba Akhtar
 
C programming language
Mahmoud Eladawi
 

Similar to C language basics (20)

PPT
C Sharp Jn (2)
jahanullah
 
PPT
C Sharp Jn (2)
guest58c84c
 
PPS
C programming session 02
Dushmanta Nath
 
PPT
Operator & Expression in c++
bajiajugal
 
PDF
ICP - Lecture 5
Hassaan Rahman
 
PPTX
C++ revision add on till now
AmAn Singh
 
PPTX
C++ revision add on till now
AmAn Singh
 
PPTX
C Token’s
Tarun Sharma
 
PDF
Module 2_PPT_P1 POP Notes module 2 fdfd.pdf
anilcsbs
 
PPT
Operators
Kamran
 
PPTX
Operators and expressions in C++
Neeru Mittal
 
PPTX
Operators inc c language
Tanmay Modi
 
PPTX
Operators and expressions in c language
tanmaymodi4
 
PPTX
introduction to c programming and C History.pptx
ManojKhadilkar1
 
PPT
6 operators-in-c
Rohit Shrivastava
 
PPT
6 operators-in-c
Rohit Shrivastava
 
PPTX
OPERATORS-PYTHON.pptx ALL OPERATORS ARITHMATIC AND LOGICAL
NagarathnaRajur2
 
PPTX
3. C_OperatorsExpressions on c languyage.pptx
iramulittihad
 
PDF
Types of Operators in C
Thesis Scientist Private Limited
 
PPTX
Programming presentation
Fiaz Khokhar
 
C Sharp Jn (2)
jahanullah
 
C Sharp Jn (2)
guest58c84c
 
C programming session 02
Dushmanta Nath
 
Operator & Expression in c++
bajiajugal
 
ICP - Lecture 5
Hassaan Rahman
 
C++ revision add on till now
AmAn Singh
 
C++ revision add on till now
AmAn Singh
 
C Token’s
Tarun Sharma
 
Module 2_PPT_P1 POP Notes module 2 fdfd.pdf
anilcsbs
 
Operators
Kamran
 
Operators and expressions in C++
Neeru Mittal
 
Operators inc c language
Tanmay Modi
 
Operators and expressions in c language
tanmaymodi4
 
introduction to c programming and C History.pptx
ManojKhadilkar1
 
6 operators-in-c
Rohit Shrivastava
 
6 operators-in-c
Rohit Shrivastava
 
OPERATORS-PYTHON.pptx ALL OPERATORS ARITHMATIC AND LOGICAL
NagarathnaRajur2
 
3. C_OperatorsExpressions on c languyage.pptx
iramulittihad
 
Types of Operators in C
Thesis Scientist Private Limited
 
Programming presentation
Fiaz Khokhar
 
Ad

Recently uploaded (20)

PPTX
Heart Bleed Bug - A case study (Course: Cryptography and Network Security)
Adri Jovin
 
PDF
Water Design_Manual_2005. KENYA FOR WASTER SUPPLY AND SEWERAGE
DancanNgutuku
 
PDF
Unified_Cloud_Comm_Presentation anil singh ppt
anilsingh298751
 
PPTX
Solar Thermal Energy System Seminar.pptx
Gpc Purapuza
 
PDF
monopile foundation seminar topic for civil engineering students
Ahina5
 
PPTX
Break Statement in Programming with 6 Real Examples
manojpoojary2004
 
PPTX
UNIT DAA PPT cover all topics 2021 regulation
archu26
 
DOCX
8th International Conference on Electrical Engineering (ELEN 2025)
elelijjournal653
 
PDF
PORTFOLIO Golam Kibria Khan — architect with a passion for thoughtful design...
MasumKhan59
 
PPTX
原版一样(Acadia毕业证书)加拿大阿卡迪亚大学毕业证办理方法
Taqyea
 
PDF
MAD Unit - 2 Activity and Fragment Management in Android (Diploma IT)
JappanMavani
 
PDF
Ethics and Trustworthy AI in Healthcare – Governing Sensitive Data, Profiling...
AlqualsaDIResearchGr
 
PPT
Oxygen Co2 Transport in the Lungs(Exchange og gases)
SUNDERLINSHIBUD
 
PDF
Statistical Data Analysis Using SPSS Software
shrikrishna kesharwani
 
PDF
UNIT-4-FEEDBACK AMPLIFIERS AND OSCILLATORS (1).pdf
Sridhar191373
 
PPTX
The Role of Information Technology in Environmental Protectio....pptx
nallamillisriram
 
PDF
MAD Unit - 1 Introduction of Android IT Department
JappanMavani
 
PDF
POWER PLANT ENGINEERING (R17A0326).pdf..
haneefachosa123
 
PDF
GTU Civil Engineering All Semester Syllabus.pdf
Vimal Bhojani
 
PPTX
Pharmaceuticals and fine chemicals.pptxx
jaypa242004
 
Heart Bleed Bug - A case study (Course: Cryptography and Network Security)
Adri Jovin
 
Water Design_Manual_2005. KENYA FOR WASTER SUPPLY AND SEWERAGE
DancanNgutuku
 
Unified_Cloud_Comm_Presentation anil singh ppt
anilsingh298751
 
Solar Thermal Energy System Seminar.pptx
Gpc Purapuza
 
monopile foundation seminar topic for civil engineering students
Ahina5
 
Break Statement in Programming with 6 Real Examples
manojpoojary2004
 
UNIT DAA PPT cover all topics 2021 regulation
archu26
 
8th International Conference on Electrical Engineering (ELEN 2025)
elelijjournal653
 
PORTFOLIO Golam Kibria Khan — architect with a passion for thoughtful design...
MasumKhan59
 
原版一样(Acadia毕业证书)加拿大阿卡迪亚大学毕业证办理方法
Taqyea
 
MAD Unit - 2 Activity and Fragment Management in Android (Diploma IT)
JappanMavani
 
Ethics and Trustworthy AI in Healthcare – Governing Sensitive Data, Profiling...
AlqualsaDIResearchGr
 
Oxygen Co2 Transport in the Lungs(Exchange og gases)
SUNDERLINSHIBUD
 
Statistical Data Analysis Using SPSS Software
shrikrishna kesharwani
 
UNIT-4-FEEDBACK AMPLIFIERS AND OSCILLATORS (1).pdf
Sridhar191373
 
The Role of Information Technology in Environmental Protectio....pptx
nallamillisriram
 
MAD Unit - 1 Introduction of Android IT Department
JappanMavani
 
POWER PLANT ENGINEERING (R17A0326).pdf..
haneefachosa123
 
GTU Civil Engineering All Semester Syllabus.pdf
Vimal Bhojani
 
Pharmaceuticals and fine chemicals.pptxx
jaypa242004
 
Ad

C language basics

  • 2. C - Language It is developed by Dennis Ritchie, in the year 1972 at AT & T Bell Labs, California. C is a Middle level language , that contains certain additional features that allows it to be used at a lower level , acting as bridge between machine language and the high level languages. general –purpose structured programming language. Instructions of C language consists of terms that are very closely same to algebraic expressions, consisting of certain English words such as if, else, for ,do, while, etc. Assembly Language : ADD A,B
  • 3. To learn any language, we must know the basic concepts of that language. These concepts are known as tokens. There are 6 tokens of c-language: 1. Character set 2. Keywords 3. Identifiers 4. Data Types 5. Operators 6. Statements Tokens of C
  • 4. Character set :  All keyboard keys and some extra characters that are not present on the keyboard. For example, the symbols used in playing cards.  There are total 256 characters.( 0 to 255)  Every character has a fixed and unique value. This value is known as ASCII value.  For example, the ASCII VALUE of:  A = 65 to Z=90  a = 97 to z = 122  0 = 48 to 9 = 57 Tokens of C
  • 5. Keywords :  Standard pre-defined words.  Always written in lowercase alphabets.  Its meaning cannot be changed.  It cannot be use as a variable name.  There are 32 keywords in C and 48 in C++.  E.g., if, else, break, continue, return, for, while, do, etc. Tokens of C
  • 6. Keywords : Tokens of C 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
  • 7. Identifiers :  User defined words.  Can be written in lowercase, UPPERCASE or MixedCase alphabets.  Its value is not changed.  It cannot be use as a variable name.  Value is assigned using #define statement.  E.g., #define MAX 100 #define Pi 3.1415 Tokens of C
  • 8. Data Types Pre-defined User-defined Derived Pointer Numeric Non-numeric Enumerated Array Structures Union Integer Real char int float long double long double Tokens of C
  • 9. Pre-defined data types Data type Size Format Specification Range Description char 1b %c -128 to +127 Stores a single character in single quotes. e.g., char c = ‘M’; int 2b %d -32768 to +32767 Stores a value without decimal point and with optional +/- sign. long 4b %ld -2147483648 to +2147483647 Same as int, with large range and size.
  • 10. Pre-defined data types Data type Size Format Specification Range Description float 4b %f -3.4e-38 to 3.4e+38 e = 10 to the power… -3.4 X 10-38 Stores a value with decimal point and with optional +/- sign. It stores a number with a decimal precision of 6 decimal places. E.g., float x = 6.75; Will store 6.750000 in x. double 8b %lf -1.7e-308 to 1.7e+308 Same as float, with large range and size. long double 10b %LF -3.4e-4932 to 1.1e+4932 Hybrid of long and double.
  • 11. Operators :  There are 3 categories of operators.  Unary  Binary  Ternary  Based on above 3 categories, there are 8 types of operators. 1. Arithmetic 2. Relational 3. Logical 4. Conditional 5. Assignment 6. Increment / Decrement 7. Bitwise 8. Special  Every operator consists of Priority and Associativity. Tokens of C
  • 12. Arithmetic Operators (+, -, *, /, %) : Priority : 1st : *, /, % 2nd : +, - Associativity : Left to Right. It means, if 2 same priority operators are present in an expression, then Left to Right, which ever comes first will be executed first. Normal Division / : It gives QUOTIENT as final answer. E.g., A = 5 / 2 = 2 Modulo Division % : It gives REMAINDER as final answer. E.g., A = 5 % 2 = 1 float z = 4.5 % 1.5 ;…error Arithmetic Operators (+, -, *, /, %)
  • 13. Integer Arithmetic : 1. int / int = int(Always) e.g., 5 / 2 = 2 2. int / float = float e.g., 5 / 2.0 = 2.500000 3. float / int = float e.g., 5.0 / 2 = 2.500000 4. float / float = float e.g., 5.0 / 2.0 = 2.500000 Rule 1: If numerator is less than denominator and both are int, then answer of Normal Division is always 0.(2/5 = 0) Rule 2: If numerator is less than denominator and both are int, then answer of Modulo Division is always Numerator. (2%5 = 2) Arithmetic Operators (+, -, *, /, %)
  • 14. e.g., int A = 4 + 8 / 3 * 9 % 5 – 3; = 4 + 2 * 9 % 5 – 3; = 4 + 18 % 5 – 3; = 4 + 3 – 3; = 7 - 3; = 4 What is the output of following expressions: int x = 5 - 4 / 3 * 10 % 6 * 2 + 9; int y = 81 % 15 * 20 / 3 – 9 + 10; Arithmetic Operators (+, -, *, /, %)
  • 15. Priority : All are on same priority Associativity : Left to Right. = = : Comparison operator != : Not Equal to The answer of these operators is always TRUE or FALSE, YES or NO, 1 or 0. e.g., int A = 7 < 9; = ? Relational Operators (< , <=, >, >=, = =, !=)
  • 16. These operator works on decimal number system and gives answer in binary(0/1). It works on Truth Table. Every non-zero number is considered as 1. Logical AND(&&) : If any input is zero, output is zero. Logical OR( || ) : If any input is one, output is one. Logical NOT( ! ) : Output is inverse of input. Priority : 1st : Logical NOT (Unary) 2nd : Logical AND 3rd : Logical OR Associativity : Left to Right Logical Operators (&&, ||, !)
  • 17. e.g., int a = -25 && 9 = 1 && 1 = 1 int b = -25 || 0 = 1 || 0 = 1 int c = !25 = !1 = 0 Logical Operators (&&, ||, !)
  • 18. It is a decision control operator. Its syntax is: False (Test condition) ? Statement – A : Statement – B; True Statement C; Test condition is true, Statement A  C Test condition is false, Statement B  C Associativity : Right to Left It is the only ternary operator. Conditional Operator (?:)
  • 19. Conditional Operator (?:) e.g., int Age; (Age >=18) ? printf(“You can vote.”):printf(“You cannot vote.”); printf(“ Stop”); Output: Let A = 21…You can vote. Stop Let A = 15…You cannot vote. Stop
  • 20. Conditional Operator (?:) e.g., int A, B, C; B = (A >=10) ? (A <= 20) ? 50 : 30 : 20; C = A + B; printf(“ A=%d, B=%d, C=%d”, A, B, C); Output: A B C 15 50 65 30 30 60 8 20 28
  • 21. Associativity : Right to Left… int c = 10; // 10 is assigned to c NOTE: It is never used with any control statement. It will always give incorrect result, because the condition will always be TRUE. Its priority is lowest than the operators above it. e.g., In statement, c = a + b, ‘+’ operator is executed first and then the ‘=‘ operator is executed. Similarly for relational, logical and conditional operators. Assignment Operators ( = )
  • 22. These are Unary operators. It is of two types: 1. Pre Increment/Decrement ( ++a / --a) a = a + 1 2. Post Increment/Decrement ( a++ / a--) Associativity : Right to Left Priority : 1st : Pre Increment/Decrement ( ++a / --a) 2nd : Post Increment/Decrement ( a++ / a--) Lower than ‘=‘ Increment/ Decrement Operators ( ++ / -- )
  • 23. e.g., int a = 2, b = 3, c, d; c = a++ * b++; d = ++a * ++b; c = a++ * b++; Since the operator is post and its priority is lower than assignment operator, so compiler will solve the ‘*’ and ‘=‘ operators first. c = a * b = 2 * 3 = 6….c = 6, a++…a = 3, b++…b = 4 d = ++a * ++b ; Since the operator is Pre, its priority is Highest, so d = 4 * 5 = 20…d = 20, ++a… a = 4, ++b…b = 5 FINAL ANSWER: a = 4, b = 5, c = 6, d = 20 Increment/ Decrement Operators ( ++ / -- )
  • 24. Solve : int a = 12, b = 9, c, d, e; c = ++a % b--; d = a + b * c++; e = a++ + --b + c + d--; What is the final value of a, b, c, d, e ? Harshali : a = 14 b = 8 c=5 d=44 e = 70 c = 13 % 9…c = 4, a = 13, b = 8 d = 13 + 8 * 4 = 13 + 32 = 45… d= 45, a = 13, b = 8, c = 5 e = 13 + 7 + 5 + 45 = 70… a = 14, b = 7, c = 5, d = 44, e = 70 Increment/ Decrement Operators ( ++ / -- )
  • 25. These operator works on binary number system and gives answer in decimal number system. Every non-zero number is converted into binary first and then the operations are performed. Associativity : Left to Right Priority : 1st : Bitwise Compliment ( ~ ) (Unary) 2nd : Bitwise Left Shift ( << ) 3rd : Bitwise Right Shift ( >> ) 4th : Bitwise And ( & ) 5th : Bitwise XOR ( ^ ) 6th : Bitwise OR ( | ) Bitwise Operators (&, |, ~, <<, >>, ^)
  • 26. e.g., int a = 10; printf(“ %d”, ~a); 0 000 1010 = a in binary. When you apply NOT operator It flips all the bits. 1 111 0101 is now equal to a. Since we declared a as signed the first bit will be defining the sign, 0 for +; 1 for - sign. And, since 2s complement is used to represent negative numbers in binary we are going to be finding the number like this: When the first bit is 1, meaning negative, think 1s as zero; 0s as one (except the first bit). Then find the number and add 1. Lets do it together: 1 111 0101 we will think 1s zero, zeros as 1. (First bit stays there) 1 000 1010, so this equals 10. You add 1, then you get 11. Then you put negative sign. So you get -11. Bitwise Compliment ( ~ )
  • 27. e.g., int a = 15; ~a = ? check the stored number, if it is a positive number, add 1 to it and convert the result from positive to negative… a = 15 + 1 = 16…-16 ( Put a negative sign before final answer) Int b = -15; ~b ? check the stored number, if it is a negative number, subtract 1 from it and convert the result from negative to positive… b = 15 -1 =14… Bitwise Compliment ( ~ )
  • 28. For performing this operation, convert the decimal number in binary and represent it in nearest 8-bit representation. E.g., int x = 12 << 1; Binary of 12 = 1100 8-bit representation of 12 = 0000 11000 (shift, ‘0’, and pad a ‘0’ to the right). Collect the number, ignoring 1st 3 0s. = 11000. Convert it to decimal = 24 + 23 = 16 + 8 = 24 RULE : If we left shift a number once, then the answer is double of last input.(Or the result is obtained by multiplying the last input by 2). What is the values of int y = 12 << 4; ? Bitwise Left Shift (<<)
  • 29. For performing this operation, convert the decimal number in binary and represent it in nearest 8-bit representation. E.g., int x = 12 >> 1; Binary of 12 = 1100 8-bit representation of 12 = 0000 1100 (shift, rightmost bit and , and pad a ‘0’ to the left). 000001100 Collect the number, ignoring 1st 5 0s. = 110. Convert it to decimal = 22 + 21 = 4 + 2 = 6 RULE : If we right shift a number once, then the answer is half of last input.(Or the result is obtained by dividing the last input by 2). What is the values of int y = 22 >> 3; ? Bitwise Right Shift (>>)
  • 30. For performing this operation, convert the decimal number in binary and represent it in nearest 8-bit representation. E.g., int x = 12 & 7; Binary of 12 = 1100 Binary of 7 = 0111 0100 (Result) Convert result in decimal = 0100 = 22 = 4 Bitwise AND ( & )
  • 31. For performing this operation, convert the decimal number in binary and represent it in nearest 8-bit representation. E.g., Bitwise XOR : If inputs are same, output is zero. int x = 12 ^ 7; Binary of 12 = 1100 Binary of 7 = 0111 1011 (Result) Convert result in decimal = 1011 = 23 + 21 + 20 = 8 + 2 + 1 = 11 Bitwise XOR ( ^ )
  • 32. For performing this operation, convert the decimal number in binary and represent it in nearest 8-bit representation. E.g., int x = 12 | 7; Binary of 12 = 1100 Binary of 7 = 0111 1111 (Result) Convert result in decimal = 1111 = 23 + 22 + 21 + 20 = 8 + 4 + 2 + 1 = 15 Bitwise OR ( | )
  • 33. These operator are special purpose operators. , : Comma operator (Separator) . : Dot / Period operator .*, -> : Memory dereferencing operators sizeof( ) : It returns the size of data type or variable passed to it. E.g., int x = sizeof(float); will store 4 in x. int y = sizeof(y); will store 2 in y. Special Operators (, , . , .*, ->, sizeof( ) )
  • 34. Statements : There are 4 types of statements used in any C-language program. 1. Pre-processor statements. 2. Type declaration statements. 3. Input-output statements. 4. Arithmetic / Control statements. Every c-language program starts with main( ) function. Tokens of C
  • 35. 1. Pre-processor statements :  These statements are prefixed by ‘#’.  These statements are not compiled.  The main pre-processor statements that are commonly used are:  File Inclusion. ( #include < > )  Identifier / Macro substitution ( #define ) 2. Type declaration statements : These statements are the very first statements of any c-program. Using these statements, we inform the compiler about the number of variables and the type of data that they are going to hold. E.g., int a, b, c; float x, y; char ch; Statements in C- language
  • 36. 3. Input-Output statements :  For input, we use scanf( ) function, whose definition is stored in stdio.h file.  For output, we use printf( ) function, whose definition is stored in stdio.h file. E.g., int x; float y; printf(“ Enter the value of x and y = “); scanf(“ %d %f”, &x, &y); printf(“ Value of x = %d, Value of y = %d”, x, y); 4. Arithmetic / Control statements : A statement that consists of arithmetic operators are known as an Arithmetic statement. Statements in C- language
  • 37. Escape Sequences Sometimes, it is necessary to use characters which cannot be typed or has special meaning in C programming. For example: newline(enter), tab, question mark etc. In order to use these characters, escape sequence is used. For example: n is used for newline. The backslash ( ) causes "escape" from the normal way the characters are interpreted by the compiler. Escape Sequences Character b Backspace f Form feed n Newline r Return t Horizontal tab v Vertical tab Backslash ' Single quotation mark " Double quotation mark ? Question mark 0 (zero) Null character
  • 38. Comments There are 2 different ways to give comments in any program. 1. Multi – line comments ( c- style) 2. Single – line comments ( c++ - style) 1. Multi – line comments ( c- style) : This is written as follows: / * This is the method of using a multi-line comment in any c- program. These sentences are not compiled. */ 2. Single – line comments ( c- style) : This is written as follows: // This is the method of using a // multi-line comment in any c- // program. These sentences are not compiled.
  • 41. Sample Program I have made a very small change in the program and now check the output.
  • 43. Sample Program I only want to print up to 2 decimal places. Check the last printf( ) statement and then check the output.
  • 45. Exercise Write a program to: 1. Print area and circumference of a circle. 2. Print area and perimeter of a rectangle. 3. Convert temperature in Celsius to Fahrenheit. 4. Print area of triangle using Heron’s formula. 5. Print Average of 3 numbers. 6. Print sum of individual digits of a 5-figit number. 7. Print reverse of a 5-digit number. 8. To swap 2 values using 3 variables. 9. To swap 2 values using 2 variables. 10. To print distance between 2 points.