In this tutorial you will learn about the C Variables and its application with practical example.
What is an Identifier?
Like any other programming language, we must aware of the naming conventions in C Programming Language before starting programming. An identifier is a name given to program elements such as c variables, arrays & functions. In the programming language C, an identifier is a sequence of alphabets or digits. Following rules must be kept in mind while naming an identifier.
- The first character must be an alphabet (uppercase or lowercase) or can be an underscore.
- All succeeding characters must be alphabets or digits.
- No special characters or punctuation symbols are allowed except the underscore”_”.
- No two successive underscores are allowed.
- Keywords should not be used as identifiers.
What is a keyword?
In C Programming Language there are set words that you cannot use as an identifier. These words are known as “reserved” words or “Keywords”. Keywords are standard identifiers and their meaning and purpose are predefined by the compiler.
There are a set of 32(Thirty-Two) keywords in the C programming language.
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 |
Note:-We cannot use keywords for declaring Variable Name, For Function Name, and for declaring Constant Variable
What is a variable?
Like in any other programming language, in C Programming Language Variables are identifiers used to refer to a memory location in computer memory that holds a value for that variable, this value can be changed during the execution of the program. The size of the memory block allocated and the type of the value it holds are completely dependent upon the type of variable. Naming a variable in C Programming Language is an important task.
Rules for naming a variable –
- A variable name can consist of letters, alphabets and start with underscore characters.
- The first character of the variable should always be an alphabet and cannot be a digit.
- Keywords are not allowed to use as a variable name.
- Blank spaces are not allowed in a variable name.
- C is case sensitive i.e. UPPER and lower case are significant.
- Special characters like #, $ are not allowed except the underscore.
- A variable name can be consist of 31 characters only if we declare a variable more than 1 characters compiler will ignore after 31 characters.
Recommendation:- Variable name must be readable and should be relative to its purpose.
Declaring a variable
In C Programming Language a variable must be declared before using it. Declaring variables is the way in which a C program tells the compiler about the number of variables it needs, what they are going to be named, and how much memory it will need. It is important to have knowledge about the type of variables and the size of these types.
Syntax:
1 |
<type> <variable_name>; |
Example:
Here is an example of declaring an integer, which we’ve called counter. (Note the semicolon at the end of the line; that is how your compiler separates one program statement from another.)
1 |
int counter; |
This statement means we’re declaring some space for a variable called counter, which will be used to store integer data. Note that we must specify the type of data that a variable will store.
Declaring multiple variables
Multiple variables can be declared with one statement.
Syntax:
1 |
<type> <var1,var2...varN>; |
Example:
1 |
int i,j,k; |
Initializing variables
We can also declare and assign some content to a variable at the same time.
1 |
int counter=3; |
This is called initialization.
Assigning variables
After declaring variables, you can assign a value to a variable later on using a statement like this:
1 |
counter=3; |
You can also assign a variable the value of another variable, like so:
1 |
i=j; |
Or assign multiple variables the same value with one statement:
1 |
counter= i= j= 3; |