C Variable With Examples
C variable is a sequence of program code with a name (also called its identifier). A name or identifier in C can be anything from a single letter to word. The name of variable must begin with an alphabetic letter or the underscore ‘_’ character but the other characters in the name can be chosen from the following groups:
- a .. z (any letter from a to z)
- A .. Z (any letter from A to Z)
- 0 .. 9 (any digit from 0 to 9)
An entity that may vary during program execution is called a variable. Variable names are names given to locations in memory. These locations can contain integer, real or character constants. In any language, the types of variables that it can support depend on the types of constants that it can handle.
This is because a particular type of variable can hold only the same type of constant. For example, an integer variable can hold only an integer constant, a real variable can hold only a real constant and a character variable can hold only a character constant.
The rules for constructing different types of constants are different. However, for constructing variable names of all types the same set of rules apply.
- A variable name is any combination of 1 to 31 alphabets, digits or underscores. Some compilers allow variable names whose length could be up to 247 characters. Still, it would be safer to stick to the rule of 31 characters. Do not create unnecessarily long variable names as it adds to your typing effort.
- The first character in the variable name must be an alphabet or underscore.
- No commas or blanks are allowed within a variable name.
The maximum allowable length of c variable name is 31 characters; an enormous number of variable names can be constructed using the above-mentioned rules. It is a good practice to exploit this enormous choice in naming variables by using meaningful variable names.
C Variable With Examples
Thus, if we want to calculate simple interest, it is always advisable to construct meaningful variable names like prin, roi, noy to represent Principle, Rate of interest and Number of years rather than using the variables a, b, c.
It is better that you use meaningful names for your variables even if this causes them to become long names. Also take this in mind that C is case sensitive. A variable named “COUNTER” is different from a variable named “counter”.
The declaration of variables must take place just after the opening brace of a block. For example we can declare variables for main() function as below code:
main() { int count; float sum,area; . . . }
First character in a variable name must be a letter or an underscore character. It cannot be a C programming language-reserved word (i.e. Commands and pre defined function names etc).
An example for using variables comes below:
#include<stdio.h> main() { int sum; sum=12; sum=sum+5; printf("Sum is %d",sum); system("pause"); }
General form for declaring a variable is:
Type name;
The line sum=sum+5; means: Increase value of sum by 5. We can also write this as sum+=5; in C programming language. printf function will print the following:
Sum is 17
In fact %d is the placeholder for integer variable value that its name comes after double quotes.
Common data types are:
- int integer
- long long integer
- float float number
- double long float
- char character
printf () function used in this example contains two sections. First section is a string enclosed in double quotes. It is called a format string. It determines output format for printf function. Second section is “variable list” section.
We include placeholders for each variable listed in variable list to determine its output place in final output text of printf function.
Variables
Variables are like boxes, you can put things in them. In our case, we will use them to store values. Variables are of different types, depending on the type it can store different values, for example and integer variable can hold number, while a string can hold characters (example. “Hello my name is Yaso” -21 characters, space included).
To start with, let’s use variables display information,
static void Main(string[] args) { string name; name = "Yaso"; Console.WriteLine(name); }
Press F5, run your application and see the result. If you receive an error, make sure you typed everything correctly.
How does it work?
To use a variable, we must first create it. To create (better terms would be to “declare” it), you must type the variable type, followed by the name you want the variable to have
string variable; int another_variable;
At this point, both of these variables are empty. To assign a value to a variable, type the name of variable, equal and the value you want it to hold. If it is a string, never forget to type the value between quotation marks:
variable = "hello there"; another_variable = 22;
Make sure you assign the correct type of value to the variable, or you will receive an error; In this case variable is a string so it can hold a string value, and is another variable an integer so it can hold a number.
You can name the variables however you like as long as you don’t use reserved words (like int, you can’t do in tint because it would return an error), and the name doesn’t contain some particular symbols, and the name doesn’t start with number.
Let’s make the computer ask for our name, and then greet us:
static void Main(string[] args) { Console.WriteLine("Hello, what is your name?"); string name; name = Console.ReadLine(); Console.WriteLine("Hello, " + name); Console.ReadKey(); }
What this code does is to declare a variable called name and then to assign it the value of the user’s input.
Press F5 and introduce yourself to your program.
How does it work?
Console.ReadLine() represents the users input, or what you type in the console window. You can assign that input to a variable as seen in the example above.
You can also tie together two strings using the + sign; in this case we tied together “Hello” and the variable name which is a string too. You can also do “hello ” + “there” and get “hello there”.
Making a calculator would seem to be pretty easy, and it is, but you have to remember one thing: the user input is a string; therefore you cannot assign it to an integer unless you convert it.
Making a calculator would seem to be pretty easy, and it is, but you have to remember one thing: the user input is a string; therefore you cannot assign it to an integer unless you convert it.
static void Main(string[] args) { int number1, number2; number1 = Int32.Parse(Console.ReadLine()); number2 = Int32.Parse(Console.ReadLine()); Console.WriteLine(number1.ToString() + "+" + number2.ToString() + "=" + (number1 + number2).ToString()); Console.ReadKey(); }
Press F5, and make sure you type a number and press enter, then type another number and press enter then stare at the result before pressing a key to exit. If you type anything but numbers it will return you an error so be careful.
Note that you can declare variables of the same type by separating the names with a comma (int number1, number2).
Int32.Parse() will turn what you put between the parenthesis into an integer, as long as it’s a string.