Variables
Syntax
Uninitialised Variable Declaration | |
---|---|
1 |
|
Initialised Variable Declaration | |
---|---|
1 |
|
Variables store the data used in a program. As C is a statically typed language, the type is the property of the variable. This means that once a variable is declared with a particular data type, the variable can only stores data of that particular type. If you are more familiar with Python or JavaScript, this might need getting used to.
Each variable has four attributes shown below.
Variable Attributes
- Name: This is the identifier to identify the variable. C uses lexical scoping.
- Type: This dictates the kind of values that can be stored in the variable.
- Address: This is where the variable is located in memory.
A counterpart in Python is
id(x)
. Unfortunately, it cannot be done in JavaScript. For now, we will skip this until we discuss pointers. - Value: The value stored in the variable.
In our example MileToKm.c
program, we have declared two variables called miles
and kms
.
Both of these have a type of float
as you can see from the declaration: float miles, kms;
.
There are other data types available. For instance, if you want to declare an integer variable, you can do so as follows.
Variable Declaration
Unitialised Variables | |
---|---|
1 2 3 |
|
Initialised Variables | |
---|---|
1 2 3 |
|
Variable Names
User defined identifiers in C must follow the rules below.
Variable Name
- Contains only uppercase (
A-Z
), lowercase (a-z
), digits (0-9
) and underscores (_
). - Cannot begin with digits.
- Cannot be one of the reserved keywords.
NOTE: Some compilers accept $
as part of the variable name.
But this depends on the compiler being used.
These rules are strict.
Also note that names are case-sensitive.
Therefore, x
is different from X
.
You should also follow the good practice when naming variables.
Good Practice
- Avoid standard identifiers.
- Use
camelCase
for variables.
auto |
else |
long |
switch |
break |
enum |
register |
typedef |
case |
extern |
return |
union |
char |
float |
short |
unsigned |
const |
for |
signed |
void |
continue |
goto |
sizeof |
volatile |
default |
if |
static |
while |
do |
int |
struct |
_Packed |
double |
printf |
scanf |
main |
sin |
cos |
tan |
floor |
ceil |
abs |
pow |
log |
sqrt |
NOTE: This lists are severely incomplete and depend on the built-in functions you intend to use.
Quick Quiz
Which of the following identifiers are valid identifiers?
-
3students
-
_X123
-
maxEntries
-
$value
-
this_IS_a_long_name
-
_
-
joe's
-
ice cream
Which of the following identifiers are valid identifiers?
-
3students
-
_X123
-
maxEntries
-
$value
-
this_IS_a_long_name
-
_
-
joe's
-
ice cream
Common Mistakes
There are several common mistakes with respect to variable declaration in C. As we have stated before and we will keep on reminding you again, the most common one is an uninitialised variable. To make the example concrete, we will use the following example below.
Variable Initialisation
InitVariable.c | |
---|---|
1 2 3 4 5 6 |
|
InitVariable.c | |
---|---|
1 2 3 4 5 6 |
|
Unfortunately, such problem cannot be reproduced in the replit below. However, you can still see the warning message from the compiler as follows:
- Click on "Shell" tab.
- Compile with GCC using
gcc -Wall main.c
.
If you have done the command gcc -Wall main.c
correctly, you should see the following message.
Uninitialised Variable Warning Message | |
---|---|
1 2 3 4 5 |
|
The flag -Wall
is used to activate all warnings.
It should be read as "double-u - all".
The "W" stands for warning.
In short, it tells the compiler to give out all warnings.
Another possible "mistakes" are redundant initialisation. It is not so much a problem since the behaviour of the program will still be as expected. In fact, in some online sources, you may see that it is a preferred practice to guard against possible error.
Common Mistakes
Redundant by Assignment | |
---|---|
1 2 |
|
Redundant by User Input | |
---|---|
1 2 |
|
Data Types
As we mentioned, every variable must be declared with a data type. This determines the values that the variable can hold as well as the operations that can be performed on the data.
Primitive Data Types
Type | Size | Usage | Examples | Py | JS |
---|---|---|---|---|---|
int |
4 bytes | Whole numbers | Max: 231-1, Min: -231 | int |
number |
float |
4 bytes | Real numbers | 12.34f , 1.5e-2f 1 |
float |
number |
double |
8 bytes | Real numbers | 12.34 , 1.5e-2 |
float |
number |
char |
1 byte | Characters | 'A' , 'z' , '\n' , ' ' , '2' |
str |
string |
DataTypes.c | |
---|---|
1 2 3 4 5 6 7 8 |
|
ReplIt Warning Message
Note that the use of %d
emits warnings from the compiler.
But this warnings are likely to be system dependent.
Plus, a warning is not an error so the program will still run.
To avoid the warning, we should use %lu
instead.
We need to emphasise here that char
is not a string.
In C, the use of a pair of single quotes is different from the use of the pair of double quotes.
In both JavaScript and Python, a string can use either kind of quotes.
However, in C, double quotes are strictly for string (or rather, char
array) and single quotes are strictly for char
.
In particular, there can be only one character enclosed in a pair of single quotes in C.
Note, however, that escape character is permitted and not counted.
C is often considered to be a strongly typed language where each variable have to be declared with a type. Afterwards, the variable can only hold the data of that type. This is in contrast to weakly typed language where a variable may hold different data type at different time.
Unfortunately, for the primitive data types above, C allows for implicit type conversion. As such, examples is hard to come by. The following video explains this strongly vs weakly (or loosely) typed languages.
Simplified Explanation
The above explanation is simplified and there are many subtleties here. Some even have different definitions. Other aspects include static vs dynamic type checking, safe type checking, type conversions, etc.
We also often compare the strength rather than categorise them cleanly. For instance, although Java, Pascal and C are all considered strongly typed languages. However, Java and Pascal are more strongly typed than C.
-
C uses suffix
f
to differentiate the number with double. However, we are not really concerned about this since the type will actually depend on the variable itself in the end. ↩