C Programming Tutorial 5 – More on C Variables
Variables are crucial to programming. We need to learn what and how to use them. In this tutorial we will learn all the basics of using c variables.
Do you still remember this?
We already discussed few things about variables in the previous lesson. We have learned how to assign an integer to a variable x and display it. We also learned that we need to declare a variable before we can use it in our c programs. We are now going in more in depth in working with variables.
Identifiers:
As we see in the figure above, we used x as our variable to store the phrase “baked mac”. The variable x is also called an identifier.
Naming Variables
There are several rules that we have to remember when naming our own identifier.
- It can only contain letters, number digits, and underscore
- It should not begin with a number
- It cannot contain spaces
- It is case sensitive. This means that variables like X and x are completely different.
- We cannot use any C language keywords as variable names.
C language keywords:
Here are the keywords that we cannot use as our variable names.
asm, auto, break, case, catch, char, class, const, const_cast, continue, default, do, double, dynamic_cast, else, enum, explicit, export, extern, false, float, for, delete, friend, goto, if, inline, int, long, mutable, namespace, new, operator, private, protected, public, register, reinterpret_cast, return, short, signed, sizeof, static, static_cast, struct, switch, template, this, throw, true, try, typedef, typeid, typename, union, unsigned, using, void, volatile, wchar_t, while, bool, virtual
Some compilers can also contain some more keywords other than the ones listed above.
Correct Naming Examples:
int num1; int user_age; int x; int diff;
Wrong Naming Examples:
int break; //this is a c keyword int 1stname; //this begins with a number digit int #x; //this one uses a special character int average grade; //use of space is not allowed
Declaring Variables
We have to declare all the variables that we want to use before we can actually use it in our c programs. So knowing how to declare them properly is a big deal.
int num1; int user_age; int x; int diff;
The code above declares 3 different variables. As we see, all of them are declared as int. We can also group them making a single statement that is the same as the above statements.
int num1, user_age, x, diff;
C Data Types
When we program we may encounter different types of data that we have to store in a variable. It can be an integer, floating point, character, or string.
| Data Type | C Data Type | Example data | Identifier |
| Integer | int | -9, 22, 0 12322,-2230 | %d |
| Floating Point | float | 5.539, 9.0, -23.4223, +2.2223 | %f |
| Character | char | S, w, %, 8 | %c |
| String | char | hello, world, 223hi, Mr. Chicoi | %s |
How to classify data types?
Integer (int)- this is used if the data provided is either negative numbers or whole numbers(0,1,2,3,…)
Float (float)- this is used if the data provided contains decimal places
Character (char)- this is used if the data given contains a single character.
String (char)- this used if the data given contains both numbers, letters, spaces, and special characters.
After learning the different type of data that we can use to a c language, we must also learn when and how to use them. Lets take a look at the following problem.
Create a program that will collect the user’s age, gender, name, and address. Use M or F for gender.
Example output:
age: 19 gender [M/F]: M name: chicoi address: #23 space ave Unknown universe, Missing galaxy 2231
To solve this problem we must first analyze the data types.
We are asked to collect the following information from the user:
- Age
- Gender (M/F)
- Name
- Address
Age can be declared as int because we don’t actually need ages like 7.5 or 18.2. a whole number is fine.
For the Gender we can make use of char because we just need a single character.
For the Name and the Address we should make use of string because it may contain numbers, spaces, special characters, and letters.
Usage
Declaring Integers:
int age;
Declaring Single Character:
char gender;
Declaring String
char name[30], address[50];
As you see the only difference of declaring a string and declaring a char is that the after the variable name it has square brackets with numbers in between. This is the one that determine how long your string will be. The number represent the number of characters you can use as input. Meaning, if you want to store a name that is 20 characters long, you must declare the variable like this:
char name[21];
More about strings here.
Related posts:
- C Programming Tutorial 2 – Intro to Variables
- C Programming Tutorial 3 – Arithmetic Operators
- C Programming Examples – Comparing 2 numbers – Simple If Else if Else problem
- C Programming Tutorial 4 – Accepting Input
- C Programming Examples – Factorial using Loop Statement
If you have any questions please feel free to discuss it on our discussion board
www.itechsociety.com/forum/topic/c-lesson-5-%E2%80%93-more-on-c-variables
This entry was posted in C/C#/C++, Lessons, Programming, Tutorials and tagged identifiers, variables. Bookmark the permalink.




[...] depth tutorial about variables here. C Programming Tutorial 2 – Intro to Variables, 5.0 out of 5 based on 2 [...]