SlideShare una empresa de Scribd logo
1 de 45
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.

Más contenido relacionado

La actualidad más candente

La actualidad más candente (19)

C fundamentals
C fundamentalsC fundamentals
C fundamentals
 
C programming language tutorial
C programming language tutorial C programming language tutorial
C programming language tutorial
 
What is c
What is cWhat is c
What is c
 
C Programming basics
C Programming basicsC Programming basics
C Programming basics
 
C program
C programC program
C program
 
12 computer science_notes_ch01_overview_of_cpp
12 computer science_notes_ch01_overview_of_cpp12 computer science_notes_ch01_overview_of_cpp
12 computer science_notes_ch01_overview_of_cpp
 
C Programming
C ProgrammingC Programming
C Programming
 
C Programming Unit-2
C Programming Unit-2C Programming Unit-2
C Programming Unit-2
 
C Language (All Concept)
C Language (All Concept)C Language (All Concept)
C Language (All Concept)
 
C programming session 01
C programming session 01C programming session 01
C programming session 01
 
Getting started with c++
Getting started with c++Getting started with c++
Getting started with c++
 
Constants Variables Datatypes by Mrs. Sowmya Jyothi
Constants Variables Datatypes by Mrs. Sowmya JyothiConstants Variables Datatypes by Mrs. Sowmya Jyothi
Constants Variables Datatypes by Mrs. Sowmya Jyothi
 
Declaration of variables
Declaration of variablesDeclaration of variables
Declaration of variables
 
C programming_MSBTE_Diploma_Pranoti Doke
C programming_MSBTE_Diploma_Pranoti DokeC programming_MSBTE_Diploma_Pranoti Doke
C programming_MSBTE_Diploma_Pranoti Doke
 
Tutorial on c language programming
Tutorial on c language programmingTutorial on c language programming
Tutorial on c language programming
 
C program compiler presentation
C program compiler presentationC program compiler presentation
C program compiler presentation
 
C Programming Unit-1
C Programming Unit-1C Programming Unit-1
C Programming Unit-1
 
Basics of c++
Basics of c++Basics of c++
Basics of c++
 
C programming language
C programming languageC programming language
C programming language
 

Similar a C language basics

C Sharp Jn (2)
C Sharp Jn (2)C Sharp Jn (2)
C Sharp Jn (2)jahanullah
 
C programming session 02
C programming session 02C programming session 02
C programming session 02Dushmanta Nath
 
Operator & Expression in c++
Operator & Expression in c++Operator & Expression in c++
Operator & Expression in c++bajiajugal
 
C++ revision add on till now
C++ revision add on till nowC++ revision add on till now
C++ revision add on till nowAmAn Singh
 
C++ revision add on till now
C++ revision add on till nowC++ revision add on till now
C++ revision add on till nowAmAn Singh
 
Operators
OperatorsOperators
OperatorsKamran
 
Operators and expressions in C++
Operators and expressions in C++Operators and expressions in C++
Operators and expressions in C++Neeru Mittal
 
Operators inc c language
Operators inc c languageOperators inc c language
Operators inc c languageTanmay Modi
 
Operators and expressions in c language
Operators and expressions in c languageOperators and expressions in c language
Operators and expressions in c languagetanmaymodi4
 
introduction to c programming and C History.pptx
introduction to c programming and C History.pptxintroduction to c programming and C History.pptx
introduction to c programming and C History.pptxManojKhadilkar1
 
Programming presentation
Programming presentationProgramming presentation
Programming presentationFiaz Khokhar
 
Lecture 3 and 4.pptx
Lecture 3 and 4.pptxLecture 3 and 4.pptx
Lecture 3 and 4.pptxMAHAMASADIK
 
C Programming Introduction
C Programming IntroductionC Programming Introduction
C Programming IntroductionHoney Sharma
 

Similar a C language basics (20)

C Sharp Jn (2)
C Sharp Jn (2)C Sharp Jn (2)
C Sharp Jn (2)
 
C Sharp Jn (2)
C Sharp Jn (2)C Sharp Jn (2)
C Sharp Jn (2)
 
C programming session 02
C programming session 02C programming session 02
C programming session 02
 
Operator & Expression in c++
Operator & Expression in c++Operator & Expression in c++
Operator & Expression in c++
 
ICP - Lecture 5
ICP - Lecture 5ICP - Lecture 5
ICP - Lecture 5
 
C++ revision add on till now
C++ revision add on till nowC++ revision add on till now
C++ revision add on till now
 
C++ revision add on till now
C++ revision add on till nowC++ revision add on till now
C++ revision add on till now
 
C Token’s
C Token’sC Token’s
C Token’s
 
Operators
OperatorsOperators
Operators
 
Operators and expressions in C++
Operators and expressions in C++Operators and expressions in C++
Operators and expressions in C++
 
Operators inc c language
Operators inc c languageOperators inc c language
Operators inc c language
 
Operators and expressions in c language
Operators and expressions in c languageOperators and expressions in c language
Operators and expressions in c language
 
introduction to c programming and C History.pptx
introduction to c programming and C History.pptxintroduction to c programming and C History.pptx
introduction to c programming and C History.pptx
 
6 operators-in-c
6 operators-in-c6 operators-in-c
6 operators-in-c
 
6 operators-in-c
6 operators-in-c6 operators-in-c
6 operators-in-c
 
Types of Operators in C
Types of Operators in CTypes of Operators in C
Types of Operators in C
 
Programming presentation
Programming presentationProgramming presentation
Programming presentation
 
Programming for Problem Solving
Programming for Problem SolvingProgramming for Problem Solving
Programming for Problem Solving
 
Lecture 3 and 4.pptx
Lecture 3 and 4.pptxLecture 3 and 4.pptx
Lecture 3 and 4.pptx
 
C Programming Introduction
C Programming IntroductionC Programming Introduction
C Programming Introduction
 

Último

Concrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptxConcrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptxKartikeyaDwivedi3
 
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionSachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionDr.Costas Sachpazis
 
8251 universal synchronous asynchronous receiver transmitter
8251 universal synchronous asynchronous receiver transmitter8251 universal synchronous asynchronous receiver transmitter
8251 universal synchronous asynchronous receiver transmitterShivangiSharma879191
 
Work Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvvWork Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvvLewisJB
 
Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.eptoze12
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024Mark Billinghurst
 
complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...asadnawaz62
 
welding defects observed during the welding
welding defects observed during the weldingwelding defects observed during the welding
welding defects observed during the weldingMuhammadUzairLiaqat
 
Risk Assessment For Installation of Drainage Pipes.pdf
Risk Assessment For Installation of Drainage Pipes.pdfRisk Assessment For Installation of Drainage Pipes.pdf
Risk Assessment For Installation of Drainage Pipes.pdfROCENODodongVILLACER
 
Correctly Loading Incremental Data at Scale
Correctly Loading Incremental Data at ScaleCorrectly Loading Incremental Data at Scale
Correctly Loading Incremental Data at ScaleAlluxio, Inc.
 
Indian Dairy Industry Present Status and.ppt
Indian Dairy Industry Present Status and.pptIndian Dairy Industry Present Status and.ppt
Indian Dairy Industry Present Status and.pptMadan Karki
 
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdfCCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdfAsst.prof M.Gokilavani
 
Solving The Right Triangles PowerPoint 2.ppt
Solving The Right Triangles PowerPoint 2.pptSolving The Right Triangles PowerPoint 2.ppt
Solving The Right Triangles PowerPoint 2.pptJasonTagapanGulla
 
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort servicejennyeacort
 
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfCCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfAsst.prof M.Gokilavani
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile servicerehmti665
 
Introduction to Machine Learning Unit-3 for II MECH
Introduction to Machine Learning Unit-3 for II MECHIntroduction to Machine Learning Unit-3 for II MECH
Introduction to Machine Learning Unit-3 for II MECHC Sai Kiran
 

Último (20)

Concrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptxConcrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptx
 
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionSachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
 
8251 universal synchronous asynchronous receiver transmitter
8251 universal synchronous asynchronous receiver transmitter8251 universal synchronous asynchronous receiver transmitter
8251 universal synchronous asynchronous receiver transmitter
 
Work Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvvWork Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvv
 
Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.
 
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Serviceyoung call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024
 
complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...
 
welding defects observed during the welding
welding defects observed during the weldingwelding defects observed during the welding
welding defects observed during the welding
 
Design and analysis of solar grass cutter.pdf
Design and analysis of solar grass cutter.pdfDesign and analysis of solar grass cutter.pdf
Design and analysis of solar grass cutter.pdf
 
Risk Assessment For Installation of Drainage Pipes.pdf
Risk Assessment For Installation of Drainage Pipes.pdfRisk Assessment For Installation of Drainage Pipes.pdf
Risk Assessment For Installation of Drainage Pipes.pdf
 
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
 
Correctly Loading Incremental Data at Scale
Correctly Loading Incremental Data at ScaleCorrectly Loading Incremental Data at Scale
Correctly Loading Incremental Data at Scale
 
Indian Dairy Industry Present Status and.ppt
Indian Dairy Industry Present Status and.pptIndian Dairy Industry Present Status and.ppt
Indian Dairy Industry Present Status and.ppt
 
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdfCCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
 
Solving The Right Triangles PowerPoint 2.ppt
Solving The Right Triangles PowerPoint 2.pptSolving The Right Triangles PowerPoint 2.ppt
Solving The Right Triangles PowerPoint 2.ppt
 
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
 
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfCCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile service
 
Introduction to Machine Learning Unit-3 for II MECH
Introduction to Machine Learning Unit-3 for II MECHIntroduction to Machine Learning Unit-3 for II MECH
Introduction to Machine Learning Unit-3 for II MECH
 

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.