You are on page 1of 5

3/4/2014

C Variables

About Us

Request a Tutorial

Contact Us

Advertising

Privacy Policy

Search this website

Home Python

SQL

PL/SQL

Data Warehouse

Data Mining

PHP

Perl

JSP

Java Swing

C GET T ING ST A RT ED

Home / C Tutorial / C Variables

C Introduction Setting Up C IDE C Compilation Model C Hello World

C Variables

C FUNDA MENT A LS

C Comments C Data Types C Variables C Constants C Operators C if else C switch case C for Loop C while Loop C do while Loop C break and continue C goto

Summary: in this tutorial, you will learn about C variables that allow you to manipulate data in your program.

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

Naming variables
1/5

http://www.zentut.com/c-tutorial/c-variables/

3/4/2014

C Variables

C Array C String C Pointer C Structure C Union C Function C Recursive Function C Function Pointer C Dynamic Memory Allocation C typedef C Preprocessor C Macro

Each variable has a name, which is called variable name. The name of variable is also referred as identifier. In C, the name of a variable must follow these rules: The variable name can contain letters, digits and the underscore ( _ ) . The first character of the variable name must be a letter or underscore ( _ ). However you should avoid using underscore ( _ ) as the first letter because the it can be clashed with standard system variables. The length of the variable name can be up to 247 characters long in Visual C++ but 31 characters are usually adequate. Variable name must not be the same as C reserved words or keywords. The following table illustrates the keywords in C:

a u t o c a s e c o n s t

b r e a k c h a r

i n t

r e t u r n

r e g i s t e rs i g n e d s t a t i c s w i t c h u n i o n

c o n t i n u es h o r t s i z e o f s t r u c t

C FILE I/O

d e f a u l td o d o u b l e e l s e e n u m f l o a t g o t o e x t e r n f o r i f

C File I/O C Check File Exists C Read Text File C Write Text File

t y p e d e f v o i d u n s i g n e dw h i l e v o l a t i l e

C variables naming conventions The following are general conventions for naming variables:

C A LGORIT HMS

C Quicksort C Insertion Sort C Selection Sort C Merge Sort

Use descriptive variable name. Begins with a lowercase letter. Separate words within the variable name with underscore (_ ) e.g., m i n _ h e i g h tor mixed lower case and upper case e.g., m i n H e i g h t . Notice that C variable is case-sensitive, so the
2/5

http://www.zentut.com/c-tutorial/c-variables/

3/4/2014

C Variables

C Heapsort C Shell Sort C Bubble Sort C Binary Search

m i n _ h e i g h t and M i n _ h e i g h tare different variables.

C variables declaration
Before using a variable, you must declare it. To declare a variable, you specify its data type and its name. The variable declaration statement always ends with a semicolon ( ; ). For example:

C DA T A ST RUCT URES

C Stack C Stack Using Linked List C Queue C Linked List C Binary Search Tree C Binary Heap C AVL Tree C Red Black Tree

1 i n tc o u n t e r ; 2 c h a rc h ;

If multiple variables share the same data type, you can declare them in a single statement as follows:
1 i n tx ,y ; 2 c h a rc ,c 2 ;

When you declare a variable: C reserves a space in the memory to hold the value of the variable. The amount of memory depends on the data type associated with the variable. C also allocates spaces that associate with the variable name and a unique address. A variable can be declared at any point in the program before it is used. It is good practice to declare a variable closest to its first point of use. In C, declaring a variable is also means defining it.

C variables initialization
To make it more convenient, C allows you to initialize a variable when you declare it, e.g.:
1 i n tx=1 0 ; 2 c h a rc h=' a ' ;

It is good practice to place initialized variables on a separate line and add a descriptive comment if possible to explain why the
http://www.zentut.com/c-tutorial/c-variables/ 3/5

3/4/2014

C Variables

variable is initialized to a specific value. For example:


1 i n ts p e e d=5 0 ;/ *m i n i m u ms p e e do nt h eh i g hw a y * /

Dont do this:
1 i n ts p e e d=5 0 ,l i m i t ,x ,y ;

C variables assignments
To assign a value to a variable, you can either initialize it through the declaration or use the assignment operator ( = ) to assign it a value. See the following example:
1 2 3 4 i n tx=1 0 ; i n ty ; x=2 0 ; y=x ;

First, we declared a variable, x , and initialize its value to 1 0 . Next, we assigned 2 0to x . Then, we assigned xto y , so the value of yis 2 0 . Notice that the assignment operator (=) means assign the value of the right-hand side to the variable on the left-hand side. It does not mean equality.

register variables
It is faster to access the registers than the main memory. Therefore if you have a variable that is most frequently used and it is required fast access, you can put the variable in registers using the r e g i s t e rkeyword. For example:

1 r e g i s t e ri n ti ; 2 3 f o r ( i=0 ;i<1 0 0 0 ;i + + ) { 4 / /. . . 5 }

Notice that the r e g i s t e r keyword works like a directive i.e., it does not guarantee the allocation of a register for storing value
http://www.zentut.com/c-tutorial/c-variables/ 4/5

3/4/2014

C Variables

of the variable. It is the compilers choice.

extern variables
When you develop program in C, you typically organize the code into header files with . hextension and source code files with . cextension. In a header file, you may have the following line of code:
1 e x t e r ni n tc o u n t e r ;

Because we used the e x t e r n modifier, we are telling the compiler that there is an c o u n t e rinteger variable defined somewhere else so that the compiler does not allocate memory for the c o u n t e rvariable. This statement only declares the c o u n t e r variable. Whenever the compiler sees the following code in a source code file:
1 i n tc o u n t e r ;

It will allocate memory for the c o u n t e rvariable. This statement defines the counter variable. In this tutorial, we have introduced you to the concept of C variables including declaring, initializing and assigning variables.

Previous Tutorial:

C Enum

C Constants

Next Tutorial:

Copyright 2014 by ZenTut Website. All Rights Reserved.

http://www.zentut.com/c-tutorial/c-variables/

5/5

You might also like