Вы находитесь на странице: 1из 6

C Variables

Defining Variables
A variable is a meaningful name of data storage location in computer memory. When using a variable you refer to memory address of computer.

Naming Variables
The name of variable can be called identifier or variable name in a friendly way. It has to follow these rules:
y

The name can contain letters, digits and the underscore but the first letter has to be a letter or the underscore. Be avoided underscore as the first letter because it can be clashed with standard system variables. The length of name can be up to 247 characters long in Visual C++ but 31 characters are usually adequate. Keywords cannot be used as a variable name.

Of course, the variable name should be meaningful to the programming context.

Declaring Variables
To declare a variable you specify its name and kind of data type it can store. The variable declaration always ends with a semicolon, for example:
1 int counter; 2 char ch;

You can declare variables at any point of your program before using it. The best practice suggests that you should declare your variables closest to their first point of use so the source code is easier to maintain. In C programming language, declaring a variable is also defining a variable.

Initializing Variables
You can also initialize a variable when you declare it, for example:
1 int x = 10; 2 char ch = 'a';

Storage of Variables
Each variable has its own lifetime (the length of time the variable can be accessible) or storage duration. When you declare your variable you implicitly assign it a lifetime. Let take a look at this source code example:
01 #include <stdio.h> 02 03 int global_variable = 10;// global variable 04 05 void func(); 06 07 void main() 08 { 09 10 11 12 13 14 15 16 } } int i; // test static variable for(i = 0; i < 5 ; i++) { func(); printf("after %d call \n",i);

17 void func() 18 { 19 20 21 22 23 24 25 } int local_variable = 10; static int counter = 0;// static variable counter++; printf("func is called %d time(s)\n",counter);

Explanations
global_variable is a global variable. It is visible and accessible to all functions. It has static life time (static means variable is retained until the program executes). It is suggested that we should avoid using global variable because it is difficult to maintain, and we dont know exactly the state of global variable because any functions can change it at any time of program execution. The local_variable and i are only existed until function completed. We cannot access it anymore. In this case it is call automatic lifetimes.

In case you want a local variable has static lifetimes, you can use static keyword like counter variable in func(). It retains until program executes even when the func() completed.

extern and register keywords


You can use extern keyword when declaring a variable or function to imply that the variable is implemented elsewhere and it will be implement later on. register keyword is used when you want a variable which is accessed many time and required fast memory access. Be noted that, declaring a variable with register keyword acts as a directive. It means it does not guarantee the allocation of a register for storing values.

Scope of Variables
You can define variable in a block of code which specifies by {and} braces. The variable has the scope inside block it is declared.

What are variables


Variables in C are memory locations that are given names and can be assigned values. We use variables to store data in memory for later use. There are 2 basic kinds of variables in C which are numeric and character.

Numeric variables
Numeric variables can either be integer values or they can be Real values. Integer values are whole numbers without a fraction part or decimal point in them. Real numbers can have a decimal point in them.

Character variables
Character variables are letters of the alphabet as well as all characters on the ASCII chart and even the numbers 0 - 9. Characters must always be put between single quotes. A number put between single quotes is not the same thing as a number without them.

What are constants


The difference between variables and constants is that variables can change their value at any time but constants can never change their value. Constants can be useful for items such as Pi or the charge on an electron. Using constants can stop you from changing the value of an item by mistake.

Declaring variables
To declare a variable we first put the type of variable and then give the variable a name. The following is a table of the names of the types of variables as well as their ranges: Name Type Range int Numeric - Integer -32 768 to 32 767 short Numeric - Integer -32 768 to 32 767 long Numeric - Integer -2 147 483 648 to 2 147 483 647 float Numeric - Real 1.2 X 10-38 to 3.4 X 1038 double Numeric - Real 2.2 X 10-308 to 1.8 X 10308 char Character All ASCII characters You can name a variable anything you like as long as it includes only letters, numbers or underscores and does not start with a number. It is a good idea to keep your variable names less than 32 characters long to save time on typing them out and for compiler compatibility reasons. Variables must always be declared at the top before any other commands are used. Now let's declare an integer variable called a and a character variable called b.
int main() { int a; char b; return 0; }

You can declare more than one variable at the same time in the following way:
int main() { int a,b,c; return 0; }

To declare a constant all you have to do it put the word const in front of a normal variable declaration and make assign a value to it.
int main() { const float pi = 3.14; return 0; }

Signed and unsigned variables


The difference between signed and unsigned variables is that signed variables can be either negative or positive but unsigned variables can only be positive. By using an unsigned variable

you can increase the maximum positive range. When you declare a variable in the normal way it is automatically a signed variable. To declare an unsigned variable you just put the word unsigned before your variable declaration or signed for a signed variable although there is no reason to declare a variable as signed since they already are.
int main() { unsigned int a; signed int b; return 0; }

Using variables in calculations


To assign a value to a variable you use the equals sign.
int main() { int a; char b; a = 3; b = 'H'; return 0; }

There are a few different operators that can be used when performing calculations which are listed in the following table: Operator Operation + Addition Subtraction * Multiplication / Division % Modulus(Remainder of integer division) To perform a calculation you need to have a variable to put the answer into. You can also use both variables and normal numbers in calculations.
int main() { int a,b; a = 5; b = a + 3; a = a - 3; return 0; }

Reading and printing variables

You can read a variable from the keyboard with the scanf command and print a variable with the printf command.
#include<stdio.h> int main() { int a; scanf("%d",&a); a = a * 2; printf("The answer is %d",a); return 0; }

The %d is for reading or printing integer values and there are others as shown in the following table: %d or %i int %c char %f float %lf double %s string

Вам также может понравиться