Lesson 03·C Programming

Variables and Keywords

Building blocks of C data storage and language syntax

Character Set

Alphabets

  • Uppercase: A – Z
  • Lowercase: a – z

Digits

  • Numeric digits: 0 – 9

Special Characters

  • ~ ! @ # $ % ^ & * ( ) _ + { } [ ] - < > , . / ? \ | : ; " '

White Spaces

  • Horizontal tab
  • Carriage return
  • New line
  • Form feed

Identifiers

An Identifier is the name of a variable, function, or array that is made up from a combination of alphabets, digits, and underscore.

Variables & Naming Rules

A Variable is a name given to a memory location where data can be stored and manipulated during program execution.

  • The first character must be an alphabet (letter) or an underscore.
  • Keywords are reserved and cannot be used as variable names.
  • White spaces and blanks are not allowed in variable names.
  • C is case-sensitive (UPPERCASE and lowercase letters are treated distinctly).
  • Only the underscore (_) special symbol is permitted between characters.
  • The identifier length may be up to 31 characters (traditionally the first 8 characters were significant to early compilers).

Local Variables

Local variables are declared within the body of a function and can only be accessed within that function block:

C Example
void main() {
    int a, b, c;  // Local to main()
}

void func() {
    int x, y, z;  // Local to func()
}
Note
Here a, b, and c are local to void main() and cannot be accessed inside func(). Likewise, x, y, and z are strictly local to func().

Global Variables

A global variable is declared outside of any program function, usually at the beginning of the program file after preprocessor directives:

C Example
int a, b, c;  // Global variables

void main() {
    // Can access a, b, c
}

void func1() {
    // Can also access a, b, c
}
Note
Here a, b, and c are global variables, and they can be accessed throughout the entire program.

Keywords in C

Keywords are system-defined reserved identifiers with fixed meanings that cannot be altered or used as variable names.

  • Keywords are system-defined identifiers.
  • All keywords have fixed meanings that do not change.
  • White spaces are not allowed within keywords.
  • A keyword may not be used as an identifier.
  • It is strongly recommended that keywords always be written in lowercase letters.
The 32 Standard C Keywords
int
float
double
long
short
signed
unsigned
const
if
else
switch
break
default
do
while
for
register
extern
static
struct
typedef
enum
return
sizeof
goto
union
auto
case
void
char
continue
volatile

Escape Sequence Characters (Backslash Constants)

C supports escape sequences used to perform formatting and special tasks, also known as 'Backslash characters':

\n
New line — Moves cursor to beginning of next line
\b
Backspace — Moves cursor back one space
\t
Horizontal Tab — Advances to next tab stop
\f
Form feed — Advances to next page/form