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

Files Recovery Software
Home Contact Details Customer Support Download Demo Products  

 
 

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.

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
 
 
Data Recovery Book
 
Chapter 1 An Overview of Data Recovery
Chapter 2 Introduction of Hard Disks
Chapter 3 Logical Approach to Disks and OS
Chapter 4 Number Systems
Chapter 5 Introduction of C Programming
Chapter 6 Introduction to Computer Basics
Chapter 7 Necessary DOS Commands
Chapter 8 Disk-BIOS Functions and Interrupts Handling With C
Chapter 9 Handling Large Hard Disks
Chapter 10 Data Recovery From Corrupted Floppy
Chapter 11 Making Backups
Chapter 12 Reading and Modifying MBR with Programming
Chapter 13 Reading and Modifying DBR with Programming
Chapter 14 Programming for “Raw File” Recovery
Chapter 15 Programming for Data Wipers
Chapter 16 Developing more Utilities for Disks
Appendix Glossary of Data Recovery Terms
 
 
Pro Data Doctor

Home

Products

Contact Details

Customer Support

Download Demo

Terms and Conditions

 
Pro Data Doctor