In C Variables can be declared at the start of any block of code, but most are found at the start of each function. Variables may be defined at the start of a block (between the braces {and}), usually this is at the start of a function body, but it may also be at the start of another type of block.

Data Recovery PRO DATA DOCTOR

Home | Order Online | Downloads | Contact Us | Software Knowledgebase

it | es | pt | fr | de | jp | kr | cn | ru | nl | gr


Using Variables

In C, a variable must be declared before it can be used. Variables can be declared at the start of any block of code, but most are found at the start of each function. Most local variables are created when the function is called, and are destroyed on return from that function.

To use variables in your C programs, you must know the following rules when giving the name to variables in C:

  • The name can contain letters, digits, and the underscore character (_).
  • The first character of the name must be a letter. The underscore is also a legal first character, but its use is not recommended.
  • C is case sensitive therefore the variable name num is Different from Num.
  • C keywords can't be used as variable names. A keyword is a word that is part of the C language.

The following list contains some examples of legal and illegal C variable names:

 

Variable Name

Legal or Not

Num

Legal

Ttpt2_t2p

Legal

 Tt pt

Illegal: Space is not allowed

_1990_tax

Legal but not advised

Jack_phone#

Illegal: Contains the illegal character #

Case

Illegal: Is a C keyword

1book

Illegal: First character is a digit

The first new thing that stands out is the first line of the body of main():

int num = 10;

This line defines a variable named 'num' of type int and initializes it with the value 10. This might also have been written as:

int num; /* define uninitialized variable 'num' */

/* and after all variable definitions: */

num = 10; /* assigns value 10 to variable 'num' */

Variables may be defined at the start of a block (between the braces {and}), usually this is at the start of a function body, but it may also be at the start of another type of block.

Variables that are defined at the beginning of a block default to the 'auto' status. This means that they only exist during the execution of the block. When the function execution begins, the variables will be created but their contents will be undefined. When the function returns, the variables will be destroyed. The definition could also have been written as:

auto int num = 10;

Since the definition with or without the auto keyword is completely equivalent, the auto keyword is obviously rather redundant.

However, sometimes this is not what you want. Suppose you want a function to keep count of how many times it is called. If the variable would be destroyed every time the function returns, this would not be possible.

Therefore it is possible to give the variable what is called static duration, which means it will stay intact during the whole execution of the program. For example:

static int num = 10;

This initializes the variable num to 10 at the beginning of the program execution. From then on the value will remain untouched; the variable will not be re-initialized if the function is called multiple times.

Sometimes it is not sufficient that the variable will be accessible from one function only or it might not be convenient to pass the value via a parameter to all other functions that need it.

Sample Chapters from book DATA RECOVERY WITH AND WITHOUT PROGRAMMING by Author Tarun Tyagi
Data Recovery with & without Programming


Publishers of the Book
Number of Pages
ISBN
Price of the Book


BPB Publications, New Delhi, India
540
81-7656-922-4
$69.00 (Including Shipping Charges, Cost of Book and Other expenses, Free Source Code CD included with the Book)





Buy Data recovery Book

Previous page

page 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20

 
 

page 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37

 
 

page 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54

 
 

page 55 | 56 | 57 | 58 | 59 | 60

Next page

© Copyright 2002-2005 DataDoctor.Biz

Home | Contact us | Downloads | Services | Terms and conditions | Site map

Website Data Recovery | recuperación de Datos | Récupération de données | Datenrettung | Recupero dati | データ復旧 | 데이터 복구 | 数据恢复 | Восстановление данных | De terugwinning van gegevens | Ανάκτηση δεδομένων
Sitemap Site map1 2 3 4 | Spanish1 2 3 | French1 2 3 | German1 2 3 | Italian1 2 3 | Portuguese1 2 3 | Japanese1 2 3 | Korean1 2 3 | Chinese1 2 3 | Russian1 2 3 | Dutch1 2 3 | Greek1 2 3
Data Recovery Book English | Spanish | French | German | Italian | Portuguese | Japanese | Korean | Chinese | Russian | Dutch | Greek