Checkpoint 5.8: What does it mean to say a function as one output parameter?
5.7 C Keywords and Punctuation
}
printf("Goodbye (Mistake count = %ld\n", error);
} // Output: area of the room (unsigned long) // Notes: ...
Program 5.11. Software to calculate the area of a square room.
5.7 C Keywords and Punctuation
C has predefined tokens, called keywords, which have specific meaning in C programs, as listed in Table 5.6. This section describes keywords and punctuation.
Keyword Meaning
__asm Specify a function is written in assembly code (specific to ARM Keil™ uVision®) auto Specifies a variable as automatic (created on the stack)
break Causes the program control structure to finish case One possibility within a switch statement char Defines a number with a precision of 8 bits
const Defines parameter as constant in ROM, and defines a local parameter as fixed value continue Causes the program to go to beginning of loop
default Used in switch statement for all other cases do Used for creating program loops
double Specifies variable as double precision floating point else Alternative part of a conditional
extern Defined in another module
float Specifies variable as single precision floating point for Used for creating program loops
goto Causes program to jump to specified location if Conditional control structure
int Defines a number with a precision that will vary from compiler to compiler long Defines a number with a precision of 32 bits
register Specifies how to implement a local return Leave function
short Defines a number with a precision of 16 bits signed Specifies variable as signed (default) sizeof Built-in function returns the size of an object static Stored permanently in memory, accessed locally struct Used for creating data structures
switch Complex conditional control structure typedef Used to create new data types unsigned Always greater than or equal to zero void Used in parameter list to mean no parameter
volatile Can change implicitly outside the direct action of the software.
while Used for creating program loops
Table 5.6. Keywords have predefined meanings.
The volatile keyword disables compiler optimization, forcing the compiler to fetch a new value each time. We will use volatile when defining I/O ports because the value of ports can change outside of software action. We will also use volatile when sharing a global variable between the main program and an interrupt service routine. It is a good programming practice not to use these keywords for your variable or function names.
Punctuation marks are very important in C. It is one of the most frequent sources of errors for both beginning and experienced programmers.
Semicolons are used as statement terminators. Strange and confusing syntax errors may be generated when you forget a semicolon, so this is one of the first things to check when trying to remove syntax errors. Notice that one semicolon is placed at the end of every simple statement in Program 5.12. When executed, the function Step will output the pattern 10, 9, 5, 6 to Port D. The #define statement
Program 5.12. Semicolons are used to separate one statement from the next.
Preprocessor directives do not end with a semicolon since they are not actually part of the C language proper. Preprocessor directives begin in the first column with the # and conclude at the end of the line.
Program 5.13 will fill the array DataBuffer with data read from the input Port A. We assume in this example that Port A has been initialized as an input. Notice that semicolons are used to separate the
DataBuffer[j] = GPIO_PORTA_DATA_R;
} }
Program 5.13. Semicolons are used to separate three fields of the for statement.
Colons terminate case, and default prefixes that appear in switch statements. In Program 5.14 one output to the stepper motor produced each time the function OneStep is called. The proper stepper motor sequence is 10–9–5–6. The default case is used to restart the pattern. For both applications of the colon (goto and switch), we see that a label is created that is a potential target for a transfer of control.
Notice the use of colons in Program 5.14.
unsigned char Last=10;
void OneStep(void){
Program 5.14. Colons are also used with the switch statement.
Commas separate items that appear in lists. We can create multiple variables of the same type using commas.
unsigned short beginTime,endTime,elapsedTime;
Lists are also used with functions having multiple parameters, both when the function is defined and called. Program 5.15 adds two 16-bit signed numbers, implementing ceiling and floor. Notice the use of commas in Program 5.15.
Program 5.15. Commas separate the parameters of a function.
Lists can also be used in general expressions. Sometimes it adds clarity to a program if related variables are modified at the same place. The value of a list of expressions is always the value of the last
expression in the list. In the following example, first thetime is incremented, next thedate is decremented, and then x is set to k+2.
X = (thetime++, thedate--, k+2);
Apostrophes are used to specify character literals. Assuming the function OutChar will display a single ASCII character, Program 5.16 will display the lower case alphabet.
void Alphabet(void){ unsigned char mych;
for(mych='a'; mych<='z'; mych++){
OutChar(mych); // Print next letter }
}
Program 5.16. Apostrophes are used to specify characters.
Quotation marks are used to specify string literals. Strings are stored as a sequence of ASCII characters followed by a termination code, 0. Program 5.17 will display “Hello World” twice.
const unsigned char Msg[]= "Hello World"; // string constant void OutString(const unsigned char str[]){ int i;
i = 0;
while(str[i]){ // output until the 0 termination OutChar(str[i]); // Print next letter
i = i+1;
} }
void PrintHelloWorld(void){
OutString("Hello World");
OutString(Msg);
}
Program 5.17. Quotation marks are used to specify strings.
Braces {} are used throughout C programs. The most common application is for creating a compound statement. Each open brace { must be matched with a closing brace }. Notice the use of indenting helps to match up braces. Each time an open brace is used, the source code is tabbed over using 2 spaces. In this way, it is easy to see at a glance the brace pairs.
Square brackets enclose array dimensions (in declarations) and subscripts (in expressions).
Parentheses enclose argument lists that are associated with function declarations and calls. They are required even if there are no arguments. As with all programming languages, C uses parentheses to control the order in which expressions are evaluated. Thus, (11+3)/2 yields 7, whereas 11+3/2 yields 12.
Parentheses are very important when writing expressions.