Declaration โก๏ธ#
To create or declare a variable in C, you do
type var_name;where,
type
is the type keyword of the variable.
var_name
is the name of the variable.
Hereโs a table below of the different data types in C again but with their keyword types. Note that strings are not shown here because they are declared in a different way.
Name |
Type Keyword |
Description |
Examples |
---|---|---|---|
Integer |
|
Represents an integer. |
0, -10, 2, 150, -349 |
Float |
|
Represents a decimal number, with at most 7-8 decimal places. |
0, -10.43, 2.23420, 150.493, -349, -4230.340 |
Character |
|
Represents a single symbol. |
โaโ, โDโ, โ1โ, โ#โ, โ^โ, โ%โ, โ&โ |
Ex.
int number;
float decimal_number;
The name of the variable can only contain letters (A-Z
), numbers (0-9
), and underscores (_
). The name must also begin with a letter. It cannot have any spaces.
Good
int valid_name;
char ValidName;
float valid_name_0439;
Bad
int 92invalid_name;
char _invalid_name;
float invalid#_name&;
Tasks ๐ฏ#
Fix the following variable names so the program compiles:
#include <stdio.h>
int main() {
int cool name;
char _some_name;
float ben&jerry;
}
Solution โ
There may be mutliple valid solutions. Hereโs one solution:
#include <stdio.h> int main() { int cool_name; char some_name; float ben_and_jerry; }