2.2 The Characters Used

The C language uses a standard set of characters. Numeric digits are used to represent numbers and alphabetic characters, digits and the underscore character are used in variable names. Any character can be part of a character string and most mathematical functions are performed using the same characters seen on calculators.

Variables are data storage areas for C programs (see XXX). All variable names must start with a letter or an underscore character (_), but can be followed by any number of letters, digits, or underscore characters. You cannot use spaces or other special characters (question mark, exclamation point, etc.) within variable names.

Table 2-1 shows some valid and invalid variable names, illustrating the rules mentioned above.

Table 2-1. Examples of valid and invalid variable names

Valid variable Invalid variable Reason
myVariable my variable Contains a space
room4 4room Starts with a number
last_name LastName? Has illegal character (?)
_okName ok-name Has illegal character (-)

Note: Characters are case sensitive in C, therefore, myVariable, myvariable, and MYVARIABLE are all different variables.

The following list of words are called reserved words for the C language. They all have special meaning within the C language, and the compiler will therefore not allow their use for naming variables or functions.

auto  default  float  register  struct  volatile 
break  do  for  return  switch  while 
case  double  goto  short  typedef   
char  else  if  signed  union   
const  enum  int  sizeof  unsigned   
continue  extern  long  static  void   

Note: Some implementations reserve asm and fortran as well but they're not part of the standard.

C99 adds the following reserved keywords, although not strictly required by C89 compilers, it's wise to avoid these in newer code.

inline  restrict  _Bool  _Complex  _Imaginary   

C++ adds the following reserved keywords, although not strictly required, it's also wise to avoid these in C code as well.

and  const_cast  namespace  public  typename 
and_eq  delete  new  reinterpret_cast  using 
asm  dynamic_cast  not  static_cast  virtual 
bitand  explicit  not_eq  template  wchar_t 
bitor  export  operator  this  xor 
bool  false  or  throw  xor_eq 
catch  friend  or_eq  true   
class  inline  private  try   
compl  mutable  protected  typeid   

2.2.1 Special Characters

The following non-letter and non-digit characters are part of C. While covered in more depth through out the book, this list serves as a quick reference.

Table 2-2. Special characters

Characters Usage
{ } Used to delimit blocks of code
( ) Used to delimit the parameter list in a function declaration, definition, or call
[ ] Used to delimit the index of an array variable
< > Used to delimit the name of standard header files
> Greater-than comparison operator
< Less-than comparison operator
== is-equal-to comparison operator
= Assignment operator
>= Greater-than or equal-to comparison operator
<= Less-than or equal-to comparison operator
!= Is-not-equal-to comparison operator
! Logical not operator
~ Bitwise complement operator
/* Used to begin a comment
*/ Used to end a comment
' ' Used to delimit a single character
" " Used to delimit a string of characters; used to delimit the name of a local header file
* Multiplication operator and pointer dereference operator and pointer in declarations
% Modulus operator
/ Division operator
+ Addition operator
- Subtraction or negation operator
++ Increment by one
-- Decrement by one
^ Bitwise exclusiveor operator
| Bitwise or operator
|| Logical or operator
& Bitwise andoperator; address-of operator
>> Bit shift right operator
<< Bit shift left operator
? : Conditional expression operator
# Used to introduce a preprocessor directive
\ Used to introduce a special character
; Used to end each statement
+= Used to perform an addition and assign the result
-= Used to perform a subtraction and assign the result
*= Used to perform a multiplication and assign the result
/= Used to perform a division and assign the result
%= Used to perform a modulo and assign the result
^= Used to perform an exclusive OR and assign the result
&= Used to perform an AND and assign the result

This, and other documents, can be downloaded from http://opencbp.sourceforge.net/.

For questions about the Open C Book Project, read the documentation.