|
C Tutorial 3. Variables - Local, global, static
- Declaration, Initialisation, Scope and naming conventions.
By John McGuinn. T223
|
||
|
|
T223 'C' Tutorial - VariablesC Home page | ' C ' Books | Student Software | Tutorial 2. Outputting text | Tutorial 3. Variables | Tutorial 5. Controlling program flow in plain English | Tutorial 6 Branching & Looping | Tutorial 90.' C ' Error Messages | Tutorial 99. Quick reference | ASCII Codes | Download files | |
|
| Btm HEADER | ||
Contents |
|
|
|
| integers | Whole numbers e.g. 0, 1, 99, 12345 |
| floating point numbers | Numbers that have a fractional part, they have a decimal point e.g. 12.543, 0.5, 1.00. Also referred too as real numbers. |
| characters | A single character e.g. a, Z, 3, # etc. Note: Characters are stored as a number that is the ASCII code for that character. See ASCII codes |
| strings | Strings can't be declared as a string. They are regarded as an array of characters, and are dealt with later. |
|
Some of these data categories are split into more than one data type as per table 3.1 below. 'C' Numeric data types. |
|
| Variable Type | Keyword | Range | Storage in Bytes |
| Character | char | -127 to 127 |
|
| Unsigned character | unsigned char | 0 to 255 |
|
|
|
|||
| Integer | int | -32,768 to 32,767 |
|
| Unsigned integer | unsigned int | 0 to 65,535 |
|
|
|
|||
| Short integer | short | -32,768 to 32,767 |
|
| Unsigned short integer | unsigned short | 0 to 65,535 |
|
|
|
|||
| Long integer | long | -2,147,483,648 to 2,147,483,647 |
|
| Unsigned long integer | unsigned long | 0 to 4,294,967,295 |
|
|
|
|||
| Single precision floating point | float | 1.2E-38 to 3.4E38, approx. range precision = 7 digits. |
|
| Double precision floating point | double | 2.2E-308 to 1.8E308, approx. range precision = 19 digits. |
|
|
The main variable types that you will use most are char,
int, float. |
|||
Global or external variableDeclared before the start of the main( ) function |
Scope: In the small
programs that are used in these tutorials the scope is
any where in the program, this includes main and all the
other functions. staticWhen static is used with a global variable it
indicates that the variable is local to the current file.
This is more advanced and static global variables are not used
in these beginners tutorials. |
Local variableDeclared within a function including the main() function. |
The declaration is placed after the { start brace
of any function including main, and before any function
statements. someFunctionName( )
{
int aVariable;
/* rest of function code */
}
staticIf the programmer would like the value of the variable to be
remembered when the function is revisited then that variable
must be declared as static. E.g. static int aVariable =
1; |
Local, local variableDeclared within the { } braces of a block of code. |
The declaration is placed after the { (start
brace of the block of code) and before any statements in
that block. Scope: of a local local variable is limited to the braces of the portion of code it is declared in. Life Span:Local local variables are destroyed when the end } brace is reached. |
| Operator | Shorthand | Equivalent more readable statement. | Assume that num is assigned 7 before the execution of the code in any of the following lines. Then the value of num after execution of that line is |
|---|---|---|---|
| ++ | ++num | num = num + 1 | 8 |
| num++ | |||
| -- | --num | num = num - 1 | 6 |
| num-- | |||
| += | num += 3 | num = num + 3 | 10 |
| -= | num -= 3 | num = num - 3 | 4 |
| *= | num *= 3 | num = num * 3 | 21 |
| /= | num /= 3 | num = num / 3 | 2 |
| %= | num %= 3 | num = num % 3 | 1 Note the remainder of 7 / 3 |
|
Exercise 1a.
You can check your paper answer as you complete the following computer exercises 1b - 1e. Exercise
1b. Error Undefined symbol void main()
{
clrscr();
cprintf("value of variable num1 is %i\n\r",num1);
}
The following error message should appear. The path
will display where the file is on your drive. The line number
may be different. Exercise 1b cont. incorrect
results
If you have corrected the Undefined symbol error correctly then the program should run but because num1 has not been initialised the result of trying to display the contents of num1 may produce funny or incorrect results. The result will depend on whatever ''old data'' is in that memory location at the time of execution. The result of running my program is You could correct this by initialising the num1 variable in
the declaration line to the required value of 4 (step C).
See solution exercise 1e and f
Exercise 1g Errors. Replace the number used in the initialise with 32768, Note
the program will execute OK but the result is wrong. 32768
is not within the allowable range. Change to 32767 which is within the range will work.
Change to 32,768 will bring up an error, make a note of it and add it to your error list. Take note of the position of the comma and the result produced. Correct the code to a workable number. num1 = 32,767 Note that this produces a different error to the similar one you did in the declaration line. I.e. a wrong result is obtained this time. Experiment with both the short cut and the longer equivalents
shown in table 2, to fully understand the workings of the code.
|
|||||||||||||||
Times Table ProjectContinuation of the Times Table Project from tutorial 2 Open up file t01ex10d.c to obtain this code. main()
{
clrscr();
cprintf (" 7 times table\r\n\r\n");
cprintf (" 1 x 7 = %i\r\n", 1 * 7) ;
cprintf (" 2 x 7 = %i\r\n", 2 * 7) ;
cprintf ("10 x 7 = %i\r\n", 10 * 7) ;
}
If I wanted to write a times tableother than 7,I would have to
rewrite the entire table for each times table I required. If I
use a variable I would have to rewrite the program but to change
from 7 times to any other number I would only have to make one
change. Look at the above code and think of where you can use
a variable to do this.
Exercise 2a. main()
{
clrscr();
cprintf (" 7 times table\r\n\r\n");
cprintf (" 1 x 7 = %i\r\n", 1 * 7) ;
}
Declare an integer variable named number, and initialise it to 7. Look at the above code and think of where you can use this variable. It's fairly obvious that you are going to use the variable in the 3 places that 7 appears in the above code. The very last 7 is easy to do, you simply replace the 7 by number. Do this now then save, compile, link and run. Check that you get the correct result. Exercise 2b. If you think you can do it, then have a go, or you may like to read the following clues first. Clues:
Exercise 2c. Exercise 2d. Exercise 2e. 1 x 8 = 8 Note the first number in each row, the number is incremented by 1. You can now use a second variable in place of 1,2,3 etc and increment this variable. Call this variable index. You have a choice of initialising this variable to either 0 or 1. Depending on whether you select 0 or 1 will determine where you place the increment code in relation to the line. /* Possible position of the increment code */
cprintf (" 1 x %i = %i\r\n" , number, 1 * number) ;
/*Alternative position of the increment code */
For this exercise you should
Go to solution 2e. If you did not change the 1 for index in the calculation, leave your answer as it is, you will see why you need to change this after completion of the next exercise. Exercise 2f. Copy the 2 lines that contain the increment code and the cprintf
code. Then paste (Ctrl v) the code say 4 times more
If you did not change the 1 in the calculation then the answer will be 8, ( 1 * 8 )in each line. Correct by changing the 1 to index in all the lines and check the result after execution. To increase the size of the table all you need to do is to keep on copying the 2 lines of code. But in a later lesson you will learn how we can use a repeat structure that repeats the 2 lines of code index++ ;
cprintf (" %i x %i = %i\r\n" , index, number, index * number) ;
as many times as required, without having to copy and paste those
lines time and time again as you have done in this exercise.
Go to solution 2f. |
Solutions to ExercisesSolutions to Exercise 1b.void main()
{
int num1;
clrscr();
cprintf("value of variable num1 is %i\n\r",num1);
}
Return to exercise 1b Solutions to Exercise 1cd. void main()
{
int num1 = 4 ;
clrscr() ;
cprintf("value of variable num1 is %i\n\r",num1) ;
num1++ ;
cprintf("value of variable num1 is %i\n\r",num1) ;
}
Return to exercise 1cd.Solutions to Exercise 1ef. void main()
{
int num1 = 4 ;
clrscr() ;
cprintf("value of variable num1 is %i\n\r",num1) ;
num1++ ;
cprintf("value of variable num1 is %i\n\r",num1) ;
num1 += 22 ;
cprintf("value of variable num1 is %i\n\r",num1) ;
}
Return to exercise 1ef. Solutions to Exercise 2a. main()
{
int number = 7;
clrscr();
cprintf (" 7 times table\r\n\r\n");
cprintf (" 1 x 7 = %i\r\n", 1 * number) ;
}
Return to exercise 2a. Solutions to Exercise 2b. main()
{
int number = 7;
clrscr();
cprintf (" %i times table\r\n\r\n",number);
cprintf (" 1 x 7 = %i\r\n", 1 * number) ;
}
Return to exercise 2b.Solutions to Exercise 2c. main()
{
int number = 7;
clrscr();
cprintf (" %i times table\r\n\r\n",number);
cprintf (" 1 x %i = %i\r\n" , number, 1 * number) ;
}
Return to exercise 2c.Solutions to Exercise 2ef. main()
{
int number = 8, index = 0;
clrscr();
cprintf (" %i times table\r\n\r\n",number);
index++ ;
cprintf (" %i x %i = %i\r\n" , index, number, index * number) ;
}
Return to exercise 2ef.| Top of Page | |
| Top of Page |
More courses for Open University students by John McGuinn.
M206 Smalltalk Programming
Home Page | HTML and Web Design tutorial for
T171, TT
Other Sites to Visit
Leeds guide my home town City of Leeds information
Tenerife guide San Marino apartments to rent Relax in the sun Tenerife information.
Retro Clothing retailers of Men's directional fashion clothes
Snakewatches wholesalers of Rope watches
Leeds my home town
Relax in the sun.
Benidorm Tenerife San Marino apartments to rent
Copyright © John G McGuinn 2001-03