SlideShare a Scribd company logo
Programming in
Arduino
DATA TYPES IN C REFERS TO AN EXTENSIVE SYSTEM USED
FOR DECLARING VARIABLES OR FUNCTIONS OF DIFFERENT
TYPES. THE TYPE OF A VARIABLE DETERMINES HOW MUCH
SPACE IT OCCUPIES IN THE STORAGE AND HOW THE BIT
PATTERN STORED IS INTERPRETED.
Data Types
void Boolean char Unsigned
char
byte int Unsigned int word
long Unsigned
long
short float double array String-char
array
String-
object
Void
The void keyword is used only in
function declarations. It indicates
that the function is expected to
return no information to the
function from which it was called.
Example
Void Loop ( )
{
// rest of the code
}
Boolean
A Boolean holds one of two
values, true or false. Each
Boolean variable occupies
one byte of memory.
Example
boolean val = false ; // declaration of
variable with type boolean and initialize it
with false
boolean state = true ; // declaration of
variable with type boolean and initialize it
with false
Char
A data type that takes up one byte
of memory that stores a character
value. Character literals are written
in single quotes like this: 'A' and
for multiple characters, strings use
double quotes: "ABC".
Example
Char chr_a = ‘a’ ;//declaration of variable with type char and initialize it with
character a
Char chr_c = 97 ;//declaration of variable with type char and initialize it with
character 97
• However, characters are stored as numbers. You
can see the specific encoding in the ASCII chart.
This means that it is possible to do arithmetic
operations on characters, in which the ASCII value
of the character is used. For example, 'A' + 1 has
the value 66, since the ASCII value of the capital
letter A is 65.
ASCII Char Table
Unsigned Char
Unsigned char is an unsigned data
type that occupies one byte of
memory. The unsigned char data
type encodes numbers from 0 to 255.
Example
Unsigned Char chr_y = 121 ; //
declaration of variable with type
Unsigned char and initialize it with
character y
Byte
A byte stores an 8-bit unsigned
number, from 0 to 255.
Example
byte m = 25 ;//declaration of variable
with type byte and initialize it with 25
Int
Integers are the primary data-type
for number storage. int stores a
16-bit (2byte) value. This yields a
range of -32,768 to 32,767
(minimum value of -2^15 and a
maximum value of (2^15) - 1).
Example
int counter = 32 ;// declaration of
variable with type int and initialize it with
32
The int size varies from board to
board. On the Arduino Due, for
example, an int stores a 32-bit (4-
byte) value. This yields a range of -2 ,
147,483,648 to 2,147,483,647
(minimum value of -2^31 and a
maximum value of (2^31) - 1).
Unsigned int
Unsigned ints (unsigned integers) are
the same as int in the way that they
store a 2 byte value. Instead of storing
negative numbers, however, they
only store positive values, yielding a
useful range of 0 to 65,535 (2^16) - 1).
The Due stores a 4 byte (32bit) value,
ranging from 0 to 4,294,967,295
(2^32 - 1).
Example
Unsigned int counter= 60 ; //
declaration of variable with type
unsigned int and initialize it with 60
Word
On the Uno and other ATMEGA
based boards, a word stores a 16-
bit unsigned number. On the Due
and Zero, it stores a 32-bit
unsigned number.
Example
word w = 1000 ;//declaration of
variable with type word and initialize it
with 1000
Long
Long variables are extended size
variables for number storage, and
store 32 bits (4 bytes), from
2,147,483,648 to 2,147,483,647.
Example
Long velocity= 102346 ;//declaration of
variable with type Long and initialize it
with 102346
Unsigned Long
Unsigned long variables are
extended size variables for
number storage and store 32 bits
(4 bytes). Unlike standard longs,
unsigned longs will not store
negative numbers, making their
range from 0 to 4,294,967,295
(2^32 - 1).
Example
Unsigned Long velocity = 101006 ;//
declaration of variable with type Unsigned
Long and initialize it with 101006
Short
A short is a 16-bit data-type. On
all Arduinos (ATMega and ARM
based), a short stores a 16-bit (2-
byte) value. This yields a range of
-32,768 to 32,767 (minimum
value of -2 ^15 and a maximum
value of (2^15) - 1).
Example
short val= 13 ;//declaration of variable
with type short and initialize it with 13
Float
Data type for floating-point number
is a number that has a decimal point.
Floatingpoint numbers are often
used to approximate the analog and
continuous values because they
have greater resolution than
integers.
Floating-point numbers can be as
large as 3.4028235E+38 and as low
as 3.4028235E+38 . They are stored
as 32 bits (4 bytes) of information
Example
float num = 1.352;//declaration of
variable with type float and initialize it
with 1.352
Double
On the Uno and other ATMEGA
based boards, Double precision
floating-point number occupies
four bytes. That is, the double
impl ementation is exactly the
same as the float, with no gain in
precision. On the Arduino Due,
doubles have 8-byte (64 bit)
precision.
Example
double num = 45.352 ;// declaration of
variable with type double and initialize it
with 45.352
Variable & Constant
Variables in C programming
language, which Arduino uses,
have a property called scope. A
scope is a region of the program
and there are three places
where variables can be declared.
They are:
Inside a function or a block, which is called
local variables.
In the definition of function parameters,
which is called formal parameters.
Outside of all functions, which is called
global variables.
Local Variables
Variables that are declared inside
a function or block are local
variables. They can be used only
by the statements that are inside
that function or block of code.
Local variables are not known to
function outside their own.
Following is the example using
local variables:
Example
Void setup ()
{
}
Void loop ()
{
int x , y ;
int z ; Local variable declaration
x= 0;
y=0; actual initialization z=10;
}
Global Variables
Global variables are defined outside
of all the functions, usually at the
top of the program. The global
variables will hold their value
throughout the life-time of your
program.
A global variable can be accessed by
any function. That is, a global
variable is available for use
throughout your entire program
after its declaration.
The following example uses global
and local variables:
Example
Int T , S ;
float c =0 ; Global variable declaration
Void setup ()
{
}
Void loop ()
{
int x , y ;
int z ; Local variable declaration
x= 0;
y=0; actual initialization z=10;
}
Operators
An operator is a symbol that
tells the compiler to perform
specific mathematical or logical
functions. C language is rich in
built-in operators and provides
the following types of
operators:
• Arithmetic Operators
• Comparison Operators
• Boolean Operators
• Bitwise Operators
• Compound Operators
Arithmetic Operators
void loop ()
{
int a=9,b=4,c;
c=a+b; c=a-b;
c=a*b; c=a/b; c=a%b;
}
Operator name Operator
simple
Description Example
assignment
operator
= Stores the value to the right of the equal
sign in the variable to the left of the equal
sign.
A=B
addition + Adds two operands A + B will give 30
subtraction - Subtracts second operand from the first A - B will give -10
multiplication * Multiply both operands A * B will give 200
division / Divide numerator by denominator B / A will give 2
modulo % Modulus Operator and remainder of after
an integer division
B % A will give 0
Reults
a+b=13 a-b=5 a*b=36 a/b=2
a/b=2
Remainder when a divided by b=1
Comparison Operators
Operator name Operator simple Description Example
equal to = = Checks if the value of two operands is equal or
not, if yes then condition becomes true.
(A == B) is not true
not equal to ! = Checks if the value of two operands is equal or
not, if values are not equal then condition
becomes true.
(A != B) is true
less than < Checks if the value of left operand is less than
the value of right operand, if yes then condition
becomes true.
(A < B) is true
greater than > Checks if the value of left operand is greater
than the value of right operand, if yes then
condition becomes true.
(A > B) is not true
less than or equal
to
< = Checks if the value of left operand is less than or
equal to the value of right operand, if yes then
condition becomes true.
(A <= B) is true
greater than
or equal to
> = Checks if the value of left operand is greater
than or equal to the value of right operand, if
yes then condition becomes true.
(A >= B) is not true
Example
void loop () {
int a=9,b=4 bool c = false;
if(a==b)
c=true;
else
c=false;
if(a!=b)
c=true;
else
c=false;
if(a<b)
c=true;
else
c=false;
if(a>b)
c=true;
else
c=false;
if(a<=b)
c=true;
else
c=false;
if(a>=b)
c=true;
else
c=false;
}
Result
c=false c=true c= false
c=true c= false c= false
Boolean Operators
Assume variable A holds 10 and variable B holds 20 then
Operator name Operator
simple
Description Example
and && Called Logical AND operator. If both the
operands are non-zero then then condition
becomes true.
(A && B) is true
or || Called Logical OR Operator. If any of the two
operands is non-zero then then condition
becomes true.
(A || B) is true
not ! Called Logical NOT Operator. Use to reverses
the logical state of its operand. If a condition is
true then
Logical NOT operator will make false.
!(A && B) is false
void loop ()
{
int a=9,b=4 bool
c = false;
if((a>b)&& (b<a))
c=true;
else
c=false;
if((a==b)|| (b<a))
c=true;
else
c=false;
if( !(a==b)&& (b<a))
c=true;
else
c=false;
}
Result: c=true c=true c= true
Bitwise Operators
void loop ()
{
int a=10,b=20
int c = 0;
c= a & b ;
c= a | b ;
c= a ^ b ;
c= a ~ b ;
c= a << b ;
c= a >> b ;
}
Result:
c=12 c=61
c= 49 c=-60
c=240 c=15
Operator
name
Operator
simple
Description Example
and & Binary AND Operator copies a bit to the
result if it exists in both operands.
(A & B) will give 12
which is 0000 1100
or | Binary OR Operator copies a bit if it exists in
either operand.
(A | B) will give 61
which is 0011 1101
xor ^ Binary XOR Operator copies the bit if it is set
in one operand but not both.
(A ^ B) will give 49
which is 0011
0001
not ~ Binary Ones Complement Operator is unary
and has the effect of 'flipping' bits.
(~A ) will give -60
which is 1100 0011
shift left << Binary Left Shift Operator. The left operands
value is moved left by the number of bits
specified by the right operand.
A << 2 will give
240 which is 1111
0000
shift right >> Binary Right Shift Operator. The left
operands value is moved right by the number
of bits specified by the right operand.
A >> 2 will give 15
which is 0000
1111
Assume variable A holds 10 and variable B holds 20 then
Compound OperatorsAssume variable A holds 10 and variable B holds 20 then
Operator name Operator simple Description Example
increment ++ Increment operator, increases integer value by one A++ will give 11
decrement -- Decrement operator, decreases integer value by one A-- will give 9
compound addition += Add AND assignment operator. It adds right operand to the left operand and
assign the result to left operand
B += A is
equivalent to B = B+ A
compound
subtraction
- = Subtract AND assignment operator. It subtracts right operand from the left
operand and assign the result to left operand
B -= A is
equivalent to B =
B - A
compound
multiplication
*= Multiply AND assignment operator. It multiplies right operand with the left
operand and assign the result to left operand
B*= A is
equivalent to B =
B* A
compound division /= Divide AND assignment operator. It divides left operand with the right
operand and assign the result to left operand
B /= A is
equivalent to B =
B / A
compound
modulo
%= Modulus AND assignment operator. It takes modulus using two operands and
assign the result to left operand
B %= A is equivalent to B = B % A
compound bitwise or |= bitwise inclusive OR and assignment operator A |= 2 is same as
A = A | 2
compound
bitwise and
&= Bitwise AND assignment operator A &= 2 is same as
A = A & 2
Example
void loop ()
{
int a=10,b=20 int c =0;
a++; a--;
b+=a; b-=a;
b*=a; b/=a;
a%=b; a|=b;
a&=b;
}
Result
a=11
a=9
b=30
b=10
b=200
b=2
a=0
a=61
a=12
Thank you.
Niket Chandrawanshi
Contact: +91-7415045756
niket.chandrawanshi@outlook.com
To know more: http://www.niketchandrawanshi.me/

More Related Content

What's hot (19)

Chap 4 c++
Chap 4 c++
Venkateswarlu Vuggam
 
Csharp4 operators and_casts
Csharp4 operators and_casts
Abed Bukhari
 
Getting started with c++
Getting started with c++
K Durga Prasad
 
2.overview of c#
2.overview of c#
Raghu nath
 
Fpga 06-data-types-system-tasks-compiler-directives
Fpga 06-data-types-system-tasks-compiler-directives
Malik Tauqir Hasan
 
Developer’s viewpoint on swift programming language
Developer’s viewpoint on swift programming language
Azilen Technologies Pvt. Ltd.
 
Functional Programming in C#
Functional Programming in C#
Giorgio Zoppi
 
Giorgio zoppi cpp11concurrency
Giorgio zoppi cpp11concurrency
Giorgio Zoppi
 
Verilog Lecture4 2014
Verilog Lecture4 2014
Béo Tú
 
12 computer science_notes_ch01_overview_of_cpp
12 computer science_notes_ch01_overview_of_cpp
sharvivek
 
C++ decision making
C++ decision making
Zohaib Ahmed
 
Cpu-fundamental of C
Cpu-fundamental of C
Suchit Patel
 
Operators in java
Operators in java
Then Murugeshwari
 
C programming part2
C programming part2
Keroles karam khalil
 
Control Structures in Python
Control Structures in Python
Sumit Satam
 
L8
L8
lksoo
 
[Apostila] programação arduíno brian w. evans
[Apostila] programação arduíno brian w. evans
Web-Desegner
 
VHDL- data types
VHDL- data types
VandanaPagar1
 
arduino
arduino
guesta10525
 
Csharp4 operators and_casts
Csharp4 operators and_casts
Abed Bukhari
 
Getting started with c++
Getting started with c++
K Durga Prasad
 
2.overview of c#
2.overview of c#
Raghu nath
 
Fpga 06-data-types-system-tasks-compiler-directives
Fpga 06-data-types-system-tasks-compiler-directives
Malik Tauqir Hasan
 
Developer’s viewpoint on swift programming language
Developer’s viewpoint on swift programming language
Azilen Technologies Pvt. Ltd.
 
Functional Programming in C#
Functional Programming in C#
Giorgio Zoppi
 
Giorgio zoppi cpp11concurrency
Giorgio zoppi cpp11concurrency
Giorgio Zoppi
 
Verilog Lecture4 2014
Verilog Lecture4 2014
Béo Tú
 
12 computer science_notes_ch01_overview_of_cpp
12 computer science_notes_ch01_overview_of_cpp
sharvivek
 
C++ decision making
C++ decision making
Zohaib Ahmed
 
Cpu-fundamental of C
Cpu-fundamental of C
Suchit Patel
 
Control Structures in Python
Control Structures in Python
Sumit Satam
 
[Apostila] programação arduíno brian w. evans
[Apostila] programação arduíno brian w. evans
Web-Desegner
 

Similar to Programming in Arduino (Part 1) (20)

C language Unit 2 Slides, UPTU C language
C language Unit 2 Slides, UPTU C language
Anurag University Hyderabad
 
C Session 2.pptx for engninering students
C Session 2.pptx for engninering students
nraj02252
 
Variable declaration
Variable declaration
Mark Leo Tarectecan
 
C tutorial
C tutorial
Anurag Sukhija
 
Introduction to c
Introduction to c
sunila tharagaturi
 
Token and operators
Token and operators
Samsil Arefin
 
Learn C LANGUAGE at ASIT
Learn C LANGUAGE at ASIT
ASIT
 
[YIDLUG] Programming Languages Differences, The Underlying Implementation 1 of 2
[YIDLUG] Programming Languages Differences, The Underlying Implementation 1 of 2
Yo Halb
 
CProgrammingTutorial
CProgrammingTutorial
Muthuselvam RS
 
dinoC_ppt.pptx
dinoC_ppt.pptx
DinobandhuThokdarCST
 
Module 2_PPT_P1 POP Notes module 2 fdfd.pdf
Module 2_PPT_P1 POP Notes module 2 fdfd.pdf
anilcsbs
 
Lecture 02 Programming C for Beginners 001
Lecture 02 Programming C for Beginners 001
MahmoudElsamanty
 
Chapter 2: Elementary Programming
Chapter 2: Elementary Programming
Eric Chou
 
c_tutorial_2.ppt
c_tutorial_2.ppt
gitesh_nagar
 
C language
C language
SMS2007
 
Lecture 2
Lecture 2
Mohamed Zain Allam
 
C++ Basics introduction to typecasting Webinar Slides 1
C++ Basics introduction to typecasting Webinar Slides 1
Ali Raza Jilani
 
Mesics lecture 3 c – constants and variables
Mesics lecture 3 c – constants and variables
eShikshak
 
presentation_data_types_and_operators_1513499834_241350.pptx
presentation_data_types_and_operators_1513499834_241350.pptx
KrishanPalSingh39
 
CPU
CPU
vishwakarma goverment enginnering collage
 
C Session 2.pptx for engninering students
C Session 2.pptx for engninering students
nraj02252
 
Learn C LANGUAGE at ASIT
Learn C LANGUAGE at ASIT
ASIT
 
[YIDLUG] Programming Languages Differences, The Underlying Implementation 1 of 2
[YIDLUG] Programming Languages Differences, The Underlying Implementation 1 of 2
Yo Halb
 
Module 2_PPT_P1 POP Notes module 2 fdfd.pdf
Module 2_PPT_P1 POP Notes module 2 fdfd.pdf
anilcsbs
 
Lecture 02 Programming C for Beginners 001
Lecture 02 Programming C for Beginners 001
MahmoudElsamanty
 
Chapter 2: Elementary Programming
Chapter 2: Elementary Programming
Eric Chou
 
C language
C language
SMS2007
 
C++ Basics introduction to typecasting Webinar Slides 1
C++ Basics introduction to typecasting Webinar Slides 1
Ali Raza Jilani
 
Mesics lecture 3 c – constants and variables
Mesics lecture 3 c – constants and variables
eShikshak
 
presentation_data_types_and_operators_1513499834_241350.pptx
presentation_data_types_and_operators_1513499834_241350.pptx
KrishanPalSingh39
 
Ad

More from Niket Chandrawanshi (9)

How MQTT work ?
How MQTT work ?
Niket Chandrawanshi
 
Arduino Uno Pin Description
Arduino Uno Pin Description
Niket Chandrawanshi
 
What is Arduino ?
What is Arduino ?
Niket Chandrawanshi
 
37 things you should Do before Die !!!
37 things you should Do before Die !!!
Niket Chandrawanshi
 
Microsoft student partners fy14 reruitment
Microsoft student partners fy14 reruitment
Niket Chandrawanshi
 
Windows Azure Overview
Windows Azure Overview
Niket Chandrawanshi
 
Html5 Basic Structure
Html5 Basic Structure
Niket Chandrawanshi
 
TCP/IP
TCP/IP
Niket Chandrawanshi
 
VLC Visible light communication (leaders of li fi)
VLC Visible light communication (leaders of li fi)
Niket Chandrawanshi
 
Ad

Recently uploaded (20)

How_Volcanshgsgsgsgsgsgsgsggsoes_Work.pptx
How_Volcanshgsgsgsgsgsgsgsggsoes_Work.pptx
zyx10283746
 
Computer project for ai and non ai robot
Computer project for ai and non ai robot
shivaniarora32567
 
File Handling ppt.pptx shjd dbkd z bdjdb d
File Handling ppt.pptx shjd dbkd z bdjdb d
ssusere1e8b7
 
Automatic plant watering system using GSM and moisture sensors.
Automatic plant watering system using GSM and moisture sensors.
asmarajput51
 
Windows Cleaner Software By Yamicsoft.pptx
Windows Cleaner Software By Yamicsoft.pptx
yamicsoft458
 
artifical intelligence and its usage in biology
artifical intelligence and its usage in biology
chaned5
 
miiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiirs.pptx
miiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiirs.pptx
ChandanKumarMajhi4
 
最新版英国伦敦大学皇家霍洛威学院毕业证(RHUL毕业证书)原版定制
最新版英国伦敦大学皇家霍洛威学院毕业证(RHUL毕业证书)原版定制
Taqyea
 
Max Power products list 2024 compone.pdf
Max Power products list 2024 compone.pdf
jmglpa
 
最新版德国不来梅艺术学院毕业证(HfK毕业证书)原版定制
最新版德国不来梅艺术学院毕业证(HfK毕业证书)原版定制
Taqyea
 
最新版美国杜比克大学毕业证(UD毕业证书)原版定制
最新版美国杜比克大学毕业证(UD毕业证书)原版定制
taqyea
 
Custom Hardware design for image processing.pptx
Custom Hardware design for image processing.pptx
DevanshuGaur5
 
[Back2School] Timing Verification- Chapter 4
[Back2School] Timing Verification- Chapter 4
Ahmed Abdelazeem
 
Presentación estilo futurístico para powerpoint.pdf
Presentación estilo futurístico para powerpoint.pdf
SubaruKun1
 
最新版英国威尔士大学毕业证(Wales毕业证书)原版定制
最新版英国威尔士大学毕业证(Wales毕业证书)原版定制
Taqyea
 
AZ-900 Summary with all information that
AZ-900 Summary with all information that
FadiAlkanani1
 
最新版西班牙梅南德斯·佩拉尤国际大学毕业证(UIMP毕业证书)原版定制
最新版西班牙梅南德斯·佩拉尤国际大学毕业证(UIMP毕业证书)原版定制
Taqyea
 
Cocaine Use disorders.pptx research on it
Cocaine Use disorders.pptx research on it
rajanirprasadkrishna
 
Predictive Maintenance- From fixing to predicting problems
Predictive Maintenance- From fixing to predicting problems
Nabeel35708
 
最新版德国弗赖堡音乐学院毕业证(Freiburg毕业证书)原版定制
最新版德国弗赖堡音乐学院毕业证(Freiburg毕业证书)原版定制
Taqyea
 
How_Volcanshgsgsgsgsgsgsgsggsoes_Work.pptx
How_Volcanshgsgsgsgsgsgsgsggsoes_Work.pptx
zyx10283746
 
Computer project for ai and non ai robot
Computer project for ai and non ai robot
shivaniarora32567
 
File Handling ppt.pptx shjd dbkd z bdjdb d
File Handling ppt.pptx shjd dbkd z bdjdb d
ssusere1e8b7
 
Automatic plant watering system using GSM and moisture sensors.
Automatic plant watering system using GSM and moisture sensors.
asmarajput51
 
Windows Cleaner Software By Yamicsoft.pptx
Windows Cleaner Software By Yamicsoft.pptx
yamicsoft458
 
artifical intelligence and its usage in biology
artifical intelligence and its usage in biology
chaned5
 
miiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiirs.pptx
miiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiirs.pptx
ChandanKumarMajhi4
 
最新版英国伦敦大学皇家霍洛威学院毕业证(RHUL毕业证书)原版定制
最新版英国伦敦大学皇家霍洛威学院毕业证(RHUL毕业证书)原版定制
Taqyea
 
Max Power products list 2024 compone.pdf
Max Power products list 2024 compone.pdf
jmglpa
 
最新版德国不来梅艺术学院毕业证(HfK毕业证书)原版定制
最新版德国不来梅艺术学院毕业证(HfK毕业证书)原版定制
Taqyea
 
最新版美国杜比克大学毕业证(UD毕业证书)原版定制
最新版美国杜比克大学毕业证(UD毕业证书)原版定制
taqyea
 
Custom Hardware design for image processing.pptx
Custom Hardware design for image processing.pptx
DevanshuGaur5
 
[Back2School] Timing Verification- Chapter 4
[Back2School] Timing Verification- Chapter 4
Ahmed Abdelazeem
 
Presentación estilo futurístico para powerpoint.pdf
Presentación estilo futurístico para powerpoint.pdf
SubaruKun1
 
最新版英国威尔士大学毕业证(Wales毕业证书)原版定制
最新版英国威尔士大学毕业证(Wales毕业证书)原版定制
Taqyea
 
AZ-900 Summary with all information that
AZ-900 Summary with all information that
FadiAlkanani1
 
最新版西班牙梅南德斯·佩拉尤国际大学毕业证(UIMP毕业证书)原版定制
最新版西班牙梅南德斯·佩拉尤国际大学毕业证(UIMP毕业证书)原版定制
Taqyea
 
Cocaine Use disorders.pptx research on it
Cocaine Use disorders.pptx research on it
rajanirprasadkrishna
 
Predictive Maintenance- From fixing to predicting problems
Predictive Maintenance- From fixing to predicting problems
Nabeel35708
 
最新版德国弗赖堡音乐学院毕业证(Freiburg毕业证书)原版定制
最新版德国弗赖堡音乐学院毕业证(Freiburg毕业证书)原版定制
Taqyea
 

Programming in Arduino (Part 1)

  • 1. Programming in Arduino DATA TYPES IN C REFERS TO AN EXTENSIVE SYSTEM USED FOR DECLARING VARIABLES OR FUNCTIONS OF DIFFERENT TYPES. THE TYPE OF A VARIABLE DETERMINES HOW MUCH SPACE IT OCCUPIES IN THE STORAGE AND HOW THE BIT PATTERN STORED IS INTERPRETED.
  • 2. Data Types void Boolean char Unsigned char byte int Unsigned int word long Unsigned long short float double array String-char array String- object
  • 3. Void The void keyword is used only in function declarations. It indicates that the function is expected to return no information to the function from which it was called. Example Void Loop ( ) { // rest of the code }
  • 4. Boolean A Boolean holds one of two values, true or false. Each Boolean variable occupies one byte of memory. Example boolean val = false ; // declaration of variable with type boolean and initialize it with false boolean state = true ; // declaration of variable with type boolean and initialize it with false
  • 5. Char A data type that takes up one byte of memory that stores a character value. Character literals are written in single quotes like this: 'A' and for multiple characters, strings use double quotes: "ABC". Example Char chr_a = ‘a’ ;//declaration of variable with type char and initialize it with character a Char chr_c = 97 ;//declaration of variable with type char and initialize it with character 97 • However, characters are stored as numbers. You can see the specific encoding in the ASCII chart. This means that it is possible to do arithmetic operations on characters, in which the ASCII value of the character is used. For example, 'A' + 1 has the value 66, since the ASCII value of the capital letter A is 65.
  • 7. Unsigned Char Unsigned char is an unsigned data type that occupies one byte of memory. The unsigned char data type encodes numbers from 0 to 255. Example Unsigned Char chr_y = 121 ; // declaration of variable with type Unsigned char and initialize it with character y
  • 8. Byte A byte stores an 8-bit unsigned number, from 0 to 255. Example byte m = 25 ;//declaration of variable with type byte and initialize it with 25
  • 9. Int Integers are the primary data-type for number storage. int stores a 16-bit (2byte) value. This yields a range of -32,768 to 32,767 (minimum value of -2^15 and a maximum value of (2^15) - 1). Example int counter = 32 ;// declaration of variable with type int and initialize it with 32 The int size varies from board to board. On the Arduino Due, for example, an int stores a 32-bit (4- byte) value. This yields a range of -2 , 147,483,648 to 2,147,483,647 (minimum value of -2^31 and a maximum value of (2^31) - 1).
  • 10. Unsigned int Unsigned ints (unsigned integers) are the same as int in the way that they store a 2 byte value. Instead of storing negative numbers, however, they only store positive values, yielding a useful range of 0 to 65,535 (2^16) - 1). The Due stores a 4 byte (32bit) value, ranging from 0 to 4,294,967,295 (2^32 - 1). Example Unsigned int counter= 60 ; // declaration of variable with type unsigned int and initialize it with 60
  • 11. Word On the Uno and other ATMEGA based boards, a word stores a 16- bit unsigned number. On the Due and Zero, it stores a 32-bit unsigned number. Example word w = 1000 ;//declaration of variable with type word and initialize it with 1000
  • 12. Long Long variables are extended size variables for number storage, and store 32 bits (4 bytes), from 2,147,483,648 to 2,147,483,647. Example Long velocity= 102346 ;//declaration of variable with type Long and initialize it with 102346
  • 13. Unsigned Long Unsigned long variables are extended size variables for number storage and store 32 bits (4 bytes). Unlike standard longs, unsigned longs will not store negative numbers, making their range from 0 to 4,294,967,295 (2^32 - 1). Example Unsigned Long velocity = 101006 ;// declaration of variable with type Unsigned Long and initialize it with 101006
  • 14. Short A short is a 16-bit data-type. On all Arduinos (ATMega and ARM based), a short stores a 16-bit (2- byte) value. This yields a range of -32,768 to 32,767 (minimum value of -2 ^15 and a maximum value of (2^15) - 1). Example short val= 13 ;//declaration of variable with type short and initialize it with 13
  • 15. Float Data type for floating-point number is a number that has a decimal point. Floatingpoint numbers are often used to approximate the analog and continuous values because they have greater resolution than integers. Floating-point numbers can be as large as 3.4028235E+38 and as low as 3.4028235E+38 . They are stored as 32 bits (4 bytes) of information Example float num = 1.352;//declaration of variable with type float and initialize it with 1.352
  • 16. Double On the Uno and other ATMEGA based boards, Double precision floating-point number occupies four bytes. That is, the double impl ementation is exactly the same as the float, with no gain in precision. On the Arduino Due, doubles have 8-byte (64 bit) precision. Example double num = 45.352 ;// declaration of variable with type double and initialize it with 45.352
  • 17. Variable & Constant Variables in C programming language, which Arduino uses, have a property called scope. A scope is a region of the program and there are three places where variables can be declared. They are: Inside a function or a block, which is called local variables. In the definition of function parameters, which is called formal parameters. Outside of all functions, which is called global variables.
  • 18. Local Variables Variables that are declared inside a function or block are local variables. They can be used only by the statements that are inside that function or block of code. Local variables are not known to function outside their own. Following is the example using local variables: Example Void setup () { } Void loop () { int x , y ; int z ; Local variable declaration x= 0; y=0; actual initialization z=10; }
  • 19. Global Variables Global variables are defined outside of all the functions, usually at the top of the program. The global variables will hold their value throughout the life-time of your program. A global variable can be accessed by any function. That is, a global variable is available for use throughout your entire program after its declaration. The following example uses global and local variables: Example Int T , S ; float c =0 ; Global variable declaration Void setup () { } Void loop () { int x , y ; int z ; Local variable declaration x= 0; y=0; actual initialization z=10; }
  • 20. Operators An operator is a symbol that tells the compiler to perform specific mathematical or logical functions. C language is rich in built-in operators and provides the following types of operators: • Arithmetic Operators • Comparison Operators • Boolean Operators • Bitwise Operators • Compound Operators
  • 21. Arithmetic Operators void loop () { int a=9,b=4,c; c=a+b; c=a-b; c=a*b; c=a/b; c=a%b; } Operator name Operator simple Description Example assignment operator = Stores the value to the right of the equal sign in the variable to the left of the equal sign. A=B addition + Adds two operands A + B will give 30 subtraction - Subtracts second operand from the first A - B will give -10 multiplication * Multiply both operands A * B will give 200 division / Divide numerator by denominator B / A will give 2 modulo % Modulus Operator and remainder of after an integer division B % A will give 0 Reults a+b=13 a-b=5 a*b=36 a/b=2 a/b=2 Remainder when a divided by b=1
  • 22. Comparison Operators Operator name Operator simple Description Example equal to = = Checks if the value of two operands is equal or not, if yes then condition becomes true. (A == B) is not true not equal to ! = Checks if the value of two operands is equal or not, if values are not equal then condition becomes true. (A != B) is true less than < Checks if the value of left operand is less than the value of right operand, if yes then condition becomes true. (A < B) is true greater than > Checks if the value of left operand is greater than the value of right operand, if yes then condition becomes true. (A > B) is not true less than or equal to < = Checks if the value of left operand is less than or equal to the value of right operand, if yes then condition becomes true. (A <= B) is true greater than or equal to > = Checks if the value of left operand is greater than or equal to the value of right operand, if yes then condition becomes true. (A >= B) is not true
  • 23. Example void loop () { int a=9,b=4 bool c = false; if(a==b) c=true; else c=false; if(a!=b) c=true; else c=false; if(a<b) c=true; else c=false; if(a>b) c=true; else c=false; if(a<=b) c=true; else c=false; if(a>=b) c=true; else c=false; } Result c=false c=true c= false c=true c= false c= false
  • 24. Boolean Operators Assume variable A holds 10 and variable B holds 20 then Operator name Operator simple Description Example and && Called Logical AND operator. If both the operands are non-zero then then condition becomes true. (A && B) is true or || Called Logical OR Operator. If any of the two operands is non-zero then then condition becomes true. (A || B) is true not ! Called Logical NOT Operator. Use to reverses the logical state of its operand. If a condition is true then Logical NOT operator will make false. !(A && B) is false void loop () { int a=9,b=4 bool c = false; if((a>b)&& (b<a)) c=true; else c=false; if((a==b)|| (b<a)) c=true; else c=false; if( !(a==b)&& (b<a)) c=true; else c=false; } Result: c=true c=true c= true
  • 25. Bitwise Operators void loop () { int a=10,b=20 int c = 0; c= a & b ; c= a | b ; c= a ^ b ; c= a ~ b ; c= a << b ; c= a >> b ; } Result: c=12 c=61 c= 49 c=-60 c=240 c=15 Operator name Operator simple Description Example and & Binary AND Operator copies a bit to the result if it exists in both operands. (A & B) will give 12 which is 0000 1100 or | Binary OR Operator copies a bit if it exists in either operand. (A | B) will give 61 which is 0011 1101 xor ^ Binary XOR Operator copies the bit if it is set in one operand but not both. (A ^ B) will give 49 which is 0011 0001 not ~ Binary Ones Complement Operator is unary and has the effect of 'flipping' bits. (~A ) will give -60 which is 1100 0011 shift left << Binary Left Shift Operator. The left operands value is moved left by the number of bits specified by the right operand. A << 2 will give 240 which is 1111 0000 shift right >> Binary Right Shift Operator. The left operands value is moved right by the number of bits specified by the right operand. A >> 2 will give 15 which is 0000 1111 Assume variable A holds 10 and variable B holds 20 then
  • 26. Compound OperatorsAssume variable A holds 10 and variable B holds 20 then Operator name Operator simple Description Example increment ++ Increment operator, increases integer value by one A++ will give 11 decrement -- Decrement operator, decreases integer value by one A-- will give 9 compound addition += Add AND assignment operator. It adds right operand to the left operand and assign the result to left operand B += A is equivalent to B = B+ A compound subtraction - = Subtract AND assignment operator. It subtracts right operand from the left operand and assign the result to left operand B -= A is equivalent to B = B - A compound multiplication *= Multiply AND assignment operator. It multiplies right operand with the left operand and assign the result to left operand B*= A is equivalent to B = B* A compound division /= Divide AND assignment operator. It divides left operand with the right operand and assign the result to left operand B /= A is equivalent to B = B / A compound modulo %= Modulus AND assignment operator. It takes modulus using two operands and assign the result to left operand B %= A is equivalent to B = B % A compound bitwise or |= bitwise inclusive OR and assignment operator A |= 2 is same as A = A | 2 compound bitwise and &= Bitwise AND assignment operator A &= 2 is same as A = A & 2
  • 27. Example void loop () { int a=10,b=20 int c =0; a++; a--; b+=a; b-=a; b*=a; b/=a; a%=b; a|=b; a&=b; } Result a=11 a=9 b=30 b=10 b=200 b=2 a=0 a=61 a=12
  • 28. Thank you. Niket Chandrawanshi Contact: +91-7415045756 [email protected] To know more: http://www.niketchandrawanshi.me/