Identifiers
These are names given to variables, functions and symbolic constants.
When determining an identifier take note of the following:
Spaces are not allowed in-between names -- if an identifier consists of two or more words we use an underscore between words or Uppercase letters to denote the beginning of a new word and for better readability.
C is case-sensitive i.e uppercase letters and lowercase letters are treated as being different (
r
is not the same asR
).A reserved word cannot be used as an identifier.
Naming conventions in programming
These are best practices or encouraged ways to name variables during coding.
Below are examples of the different ways to name a string variable First Name using different notations.
Camel case: firstName
Pascal case: FirstName
Hungarian Notation: strFirstName
Underscore: first_name
Variables and Constants
Variable – a place in memory that stores a type of data that varies during program execution.
Variable names are names given to locations in storage.
All variables used in a program are declared before compilation.
To make a variable constant you include the keyword
const
in the declaration statement.
Constant – a variable whose value cannot be changed during program execution.
Declaring variables/constants
//declaring an integer
int num;
//declaring a floating point number
float num_two;
//declaring a character
char letter;
//declaring a string
char word[5] = '\0';
//declaring and initializing a constant integer
const int num_three = 65;
NB: Declaration informs the compiler of the existence of a variable whereas initialization assigns a data value to that declared variables.
Initialization example: int a = 3
The above code snippet assigns the value of 3 (or stores 3) in the memory location named 'a'.
Data types
Every variable in a program is associated with a data type.
Data types are categorized based on how they store values in memory and the amount of memory they require.
There are:
Primary Data Types - these are fundamental data types: int
, float
, char
, double
.
Derived Data Types - these are primary data types grouped together: array, struct
, union
, pointer
Keywords
These are reserved (or pre-defined) words whose meaning has already been explained to the C compiler.
There are 32 keywords in C:
auto
, break
, case
, char
, const
, continue
, default
, do
, double
, else
, enum
, extern
, float
, for
, goto
, if
, int
, long
, register
, return
, short
, signed
, sizeof
, static
, struct
,switch
, typedef
, union
, unsigned
, void
, volatile
, while
Escape Sequences
These are sequences of characters that specify another action than their literal meanings.
They are denoted by a backslash () followed by another character.
Below are a list of escape sequences and their uses in C:
\a
– Bell Beep
\b
– Back space (brings cursor one position to the left)
\f
– Form Feed (ejects current paper and loads new one)
\n
– New line
\r
– Carriage return (brings cursor to the beginning of the line)
\t
– Horizontal tab
\v
– Vertical tab
\\
- Prints Backslash
\”
– Prints double quotations
\’
– Prints single quotations
\?
– Prints question marks
Conversion specifiers
Also termed format specifiers.
These tell the compiler the data type of the data in a variable when taking input from the user or when printing output on the screen.
Below is a list of commonly used conversion specifiers and the type of data they represent:
%d
– integer
%i
– integer
%lf
- double
%c
– character
%f
– floating point number
End of chapter sample program
The following source code is hard-coded with three variables a
, b
and c
. The program displays the values stored in these variables.
#include <stdio.h>
main()
{
int a = 12;
float b = 34.6;
char c = 'M';
printf("The integer is: %i", a);
printf("\nThe floating point number is: %f", b);
printf("\nThe character is: %c", c);
}