Programming Arduino Getting Started with Sketches



Yüklə 5,42 Kb.
Pdf görüntüsü
səhifə34/78
tarix22.12.2023
ölçüsü5,42 Kb.
#189286
1   ...   30   31   32   33   34   35   36   37   ...   78
Arduino SimonMonk 2011

and
and 
or
, there is the 
not
operator, written as 
!
. You will not be
surprised to learn that “not true” is false and “not false” is true.
Figure 4-1
Truth tables
You can combine these operators into Boolean expressions in your 
if
statements, as the
following example illustrates:
Other Data Types
As you have seen, the 
int
and occasionally the 
float
data types are fine for most situations;
however, some other numeric types can be useful under some circumstances. In an
Arduino sketch, the 
int
type uses 16 bits (binary digits). This allows it to represent
numbers between −32767 and 32768.
Other data types available to you are summarized in 
Table 4-1
. This table is provided
mainly for reference. You will use some of these other types as you progress through the
book.


Table 4-1
Data Types in C
One thing to consider is that if data types exceed their range, then strange things
happen. So, if you have a byte variable with 255 in it and you add 1 to it, you get 0. More
alarmingly, if you have an 
int
variable with 32767 and you add 1 to it, you will end up
with −32768.
Until you are completely comfortable with these different data types, I would
recommend sticking to 
int
, as it works for pretty much everything.
Coding Style
The C compiler does not really care about how you lay out your code. For all it cares, you
can write everything on a single line with semicolons between each statement. However,
well-laid-out, neat code is much easier to read and maintain than poorly laid-out code. In
this sense, reading code is just like reading a book: Formatting is important.
To some extent, formatting is a matter of personal taste. No one likes to think that he
has bad taste, so arguments about how code should look can become personal. It is not
unknown for programmers, on being required to do something with someone else’s code,
to start by reformatting all the code into their preferred style of presentation.


As an answer to this problem, coding standards are often laid down to encourage
everyone to lay out his or her code in the same way and adopt “good practice” when
writing programs.
The C language has a de facto standard that has evolved over the years, and this book is
generally faithful to that standard.
Indentation
In the example sketches that you have seen, you can see that we often indent the program
code from the left margin. So, for example when defining a 

Yüklə 5,42 Kb.

Dostları ilə paylaş:
1   ...   30   31   32   33   34   35   36   37   ...   78




Verilənlər bazası müəlliflik hüququ ilə müdafiə olunur ©azkurs.org 2024
rəhbərliyinə müraciət

gir | qeydiyyatdan keç
    Ana səhifə


yükləyin