⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 chapter 4 variables and constants -- valvano.htm

📁 介绍了在嵌入式系统中如何用c来设计嵌入式软件
💻 HTM
📖 第 1 页 / 共 5 页
字号:
block. In C local variables must be defined at the beginning of a block. The 
following example is proper C++ code, but results in a syntax error in 
C.</FONT></P>
<DIR>
<P><CODE>void sub(void){ int x;&nbsp;&nbsp;/* a valid local variable declaration 
*/<BR>&nbsp;&nbsp;&nbsp;&nbsp;x=1;<BR>&nbsp;&nbsp;&nbsp;&nbsp;int 
y;&nbsp;&nbsp;&nbsp;/* This declaration is improper 
*/<BR>&nbsp;&nbsp;&nbsp;&nbsp;y=2;<BR>}</CODE></P></DIR>
<ADDRESS>Listing 4-13: Example showing an illegal local variable 
declaration</ADDRESS>
<P>&nbsp;</P>
<P><B><I><FONT face=Helvetica,Arial><A 
name=DECLARATIONS></A>Declarations</FONT></I></B></P>
<P><FONT face="Times New Roman,Times">Unlike BASIC and FORTRAN, which will 
automatically declare variables when they are first used, every variable in C 
must be declared first. This may seem unnecessary, but when we consider how much 
time is spent debugging BASIC and FORTRAN programs simply because misspelled 
variable names are not caught for us, it becomes obvious that the time spent 
declaring variables beforehand is time well spent. Declarations also force us to 
consider the precision (8-bit, 16-bit etc.) and format (unsigned vs. signed) of 
each variable.</FONT></P>
<P><FONT face="Times New Roman,Times">As we saw in <A 
href="http://www.ece.utexas.edu/~valvano/embed/chap1/chap1.htm">Chapter 1</A>, 
describing a variable involves two actions. The first action is declaring its 
type and the second action is defining it in memory (reserving a place for it). 
Although both of these may be involved, we refer to the C construct that 
accomplishes them as a <I>declaration</I>. As we saw above, if the declaration 
is preceded by <B>extern</B> it only declares the type of the variables, without 
reserving space for them. In such cases, the definition must exist in another 
source file. Failure to do so, will result in an unresolved reference error at 
link time.</FONT></P>
<P><FONT face="Times New Roman,Times">Table 4-1 contains examples of legitimate 
variable declarations. Notice that the declarations are introduced by one or 
type keywords that states the data type of the variables listed. The keyword 
<B>char</B> declares 8-bit values, <B>int</B> declares 16-bit values, 
<B>short</B> declares 16-bit values and <B>long</B> declares 32-bit values. 
Unless the modifier <B>unsigned</B> is present, the variables declared by these 
statements are assumed by the compiler to contain signed values. You could add 
the keyword <B>signed</B> before the data type to clarify its type.</FONT></P>
<P><FONT face="Times New Roman,Times">When more than one variable is being 
declared, they are written as a list with the individual names separated by 
commas. Each declaration is terminated with a semicolon as are all simple C 
statements.</FONT></P>
<P><FONT face="Times New Roman,Times">
<TABLE cellSpacing=0 cellPadding=0 width=511 bgColor=white border=0>
  <TBODY>
  <TR>
    <TD><FONT face="Times New Roman,Times" size=2>Declaration</FONT></TD>
    <TD><FONT face="Times New Roman,Times" size=2>Comment</FONT></TD>
    <TD><FONT face="Times New Roman,Times" size=2>Range</FONT></TD></TR>
  <TR>
    <TD><CODE><FONT face="Times New Roman,Times">unsigned char 
      uc;</FONT></CODE></TD>
    <TD><FONT face="Times New Roman,Times" size=2>8-bit unsigned 
    number</FONT></TD>
    <TD><FONT face="Times New Roman,Times" size=2>0 to +255</FONT></TD></TR>
  <TR>
    <TD><CODE><FONT face="Times New Roman,Times">char 
    c1,c2,c3;</FONT></CODE></TD>
    <TD><FONT face="Times New Roman,Times" size=2>three 8-bit signed 
      numbers</FONT></TD>
    <TD><FONT face="Times New Roman,Times" size=2>-128 to +127</FONT></TD></TR>
  <TR>
    <TD><CODE><FONT face="Times New Roman,Times">unsigned int 
    ui;</FONT></CODE></TD>
    <TD><FONT face="Times New Roman,Times" size=2>16-bit unsigned 
    number</FONT></TD>
    <TD><FONT face="Times New Roman,Times" size=2>0 to +65535</FONT></TD></TR>
  <TR>
    <TD><CODE><FONT face="Times New Roman,Times">int i1,i2;</FONT></CODE></TD>
    <TD><FONT face="Times New Roman,Times" size=2>two 16-bit signed 
      numbers</FONT></TD>
    <TD><FONT face="Times New Roman,Times" size=2>-32768 to 
+32767</FONT></TD></TR>
  <TR>
    <TD><CODE><FONT face="Times New Roman,Times">unsigned short 
      us;</FONT></CODE></TD>
    <TD><FONT face="Times New Roman,Times" size=2>16-bit unsigned 
    number</FONT></TD>
    <TD><FONT face="Times New Roman,Times" size=2>0 to +65535</FONT></TD></TR>
  <TR>
    <TD><CODE><FONT face="Times New Roman,Times">short s1,s2;</FONT></CODE></TD>
    <TD><FONT face="Times New Roman,Times" size=2>two 16-bit signed 
      numbers</FONT></TD>
    <TD><FONT face="Times New Roman,Times" size=2>-32768 to 
+32767</FONT></TD></TR>
  <TR>
    <TD><CODE><FONT face="Times New Roman,Times">long 
      l1,l2,l3,l4;</FONT></CODE></TD>
    <TD><FONT face="Times New Roman,Times" size=2>four signed 32 bit 
      integers</FONT></TD>
    <TD><FONT face="Times New Roman,Times" size=2>-2147483648L to 
      2147483647L</FONT></TD></TR>
  <TR>
    <TD><CODE><FONT face="Times New Roman,Times">float f1,f2;</FONT></CODE></TD>
    <TD><FONT face="Times New Roman,Times" size=2>two 32-bit floating 
      numbers</FONT></TD>
    <TD>not recommended</TD></TR>
  <TR>
    <TD><CODE><FONT face="Times New Roman,Times">double 
d1,d2;</FONT></CODE></TD>
    <TD><FONT face="Times New Roman,Times" size=2>two 64-bit floating 
      numbers</FONT></TD>
    <TD>not recommended</TD></TR></TBODY></TABLE></FONT></P>
<ADDRESS>Table 4-1: Variable Declarations</ADDRESS>
<ADDRESS>&nbsp;</ADDRESS>
<P><FONT face="Times New Roman,Times">ICC11 version 4 does not support long 
integers, and ICC12 does not support unsigned long integers. ICC11 and ICC12 
compilers allow the <B>register</B> modifier for automatic variables, but the 
compilers still define the register locals on the stack. The keywords <B>char 
int short long</B> specifies the precision of the variable. The following tables 
shows the available modifiers for variables.</FONT></P>
<P>
<TABLE cellSpacing=0 width=521 border=0>
  <TBODY>
  <TR>
    <TD vAlign=center width="16%" bgColor=white><FONT 
      face="Times New Roman,Times">Modifier</FONT></TD>
    <TD vAlign=center width="84%" bgColor=white><FONT 
      face="Times New Roman,Times">Comment</FONT></TD></TR>
  <TR>
    <TD vAlign=center width="16%" bgColor=white><CODE>auto</CODE></TD>
    <TD vAlign=center width="84%" bgColor=white><FONT 
      face="Times New Roman,Times">automatic, allocated on the 
stack</FONT></TD></TR>
  <TR>
    <TD vAlign=center width="16%" bgColor=white><CODE>extern</CODE></TD>
    <TD vAlign=center width="84%" bgColor=white><FONT 
      face="Times New Roman,Times">defined in some other program</FONT> 
  file</TD></TR>
  <TR>
    <TD vAlign=center width="16%" bgColor=white><CODE>static</CODE></TD>
    <TD vAlign=center width="84%" bgColor=white><FONT 
      face="Times New Roman,Times">permanently allocated </FONT></TD></TR>
  <TR>
    <TD width="16%" bgColor=white><CODE>register</CODE></TD>
    <TD width="84%" bgColor=white>attempt to implement an automatic using a 
      register instead of on the stack</TD></TR></TBODY></TABLE></P>
<ADDRESS>Table 4-2: Variable storage classes</ADDRESS>
<ADDRESS>&nbsp;</ADDRESS>
<P>
<TABLE cellSpacing=0 width=521 border=0>
  <TBODY>
  <TR>
    <TD vAlign=center width="16%" bgColor=white><FONT 
      face="Times New Roman,Times">Modifier</FONT></TD>
    <TD vAlign=center width="84%" bgColor=white><FONT 
      face="Times New Roman,Times">Comment</FONT></TD></TR>
  <TR>
    <TD vAlign=center width="16%" bgColor=white><CODE>volatile</CODE></TD>
    <TD vAlign=center width="84%" bgColor=white><FONT 
      face="Times New Roman,Times">can change value by means other than the 
      current program</FONT></TD></TR>
  <TR>
    <TD vAlign=center width="16%" bgColor=white><CODE>const</CODE></TD>
    <TD vAlign=center width="84%" bgColor=white><FONT 
      face="Times New Roman,Times">fixed value, defined in the source code and 
      can not be changed during execution</FONT></TD></TR>
  <TR>
    <TD vAlign=center width="16%" bgColor=white><CODE>unsigned</CODE></TD>
    <TD vAlign=center width="84%" bgColor=white><FONT 
      face="Times New Roman,Times">range starts with 0 includes only positive 
      values</FONT></TD></TR>
  <TR>
    <TD vAlign=center width="16%" bgColor=white><CODE>signed</CODE></TD>
    <TD vAlign=center width="84%" bgColor=white><FONT 
      face="Times New Roman,Times">range includes both negative and positive 
      values</FONT></TD></TR></TBODY></TABLE></P>
<ADDRESS>Table 4-3 Variable modifiers</ADDRESS>
<P>&nbsp;</P>
<P><FONT face="Times New Roman,Times">As we shall see, a similar syntax is used 
to declare pointers, arrays, and functions (<A 
href="http://www.ece.utexas.edu/~valvano/embed/chap7/chap7.htm">Chapters 7</A>, 
<A href="http://www.ece.utexas.edu/~valvano/embed/chap8/chap8.htm">8</A>, and <A 
href="http://www.ece.utexas.edu/~valvano/embed/chap10/chap10.htm">10</A>).</FONT></P>
<P><B><I><FONT face=Helvetica,Arial><A name=CHARACTER></A>Character 
Variables</FONT></I></B></P>
<P><FONT face="Times New Roman,Times">Character variables are stored as 8-bit 
quantities. When they are fetched from memory, they are always promoted 
automatically to 16-bit integers. Unsigned 8-bit values are promoted by adding 8 
zeros into the most significant bits. Signed values are promoted by coping the 
sign bit (bit7) into the 8 most significant bits.</FONT></P>
<P><FONT face="Times New Roman,Times">There is a confusion when signed and 
unsigned variables are mixed into the same expression. It is good programming 
practice to avoid such confusions. As with integers, when a signed character 
enters into an operation with an unsigned quantity, the character is interpreted 
as though it was unsigned. The result of such operations is also unsigned. When 
a signed character joins with another signed quantity, the result is also 
signed.</FONT></P>
<DIR>
<P><CODE>char x;&nbsp;&nbsp;&nbsp;/* signed 8 bit global */<BR>unsigned short 
y;&nbsp;&nbsp;&nbsp;/* unsigned signed 16 bit global */<BR>void 
sub(void){<BR>&nbsp;&nbsp;&nbsp;&nbsp;y=y+x;<BR>/* x treated as unsigned even 
though defined as signed */<BR>}</CODE></P></DIR>
<ADDRESS>Listing 4-13: An example showing the mixture of signed and unsigned 
variables</ADDRESS>
<P><FONT face="Times New Roman,Times">There is also a need to change the size of 
characters when they are stored, since they are represented in the CPU as 16-bit 
values. In this case, however, it matters not whether they are signed or 
unsigned. Obviously there is only one reasonable way to put a 16-bit quantity 
into an 8-bit location. When the high-order byte is chopped off, an error might 
occur. It is the programmer's responsibility to ensure that significant bits are 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -