Previous | Home | Next |
C constants
Constants also called a literals. Constant refer to fixed values that the program may not alter. constant can be any of the basic data types. C have a two type of constant:
- Numeric constant
- Character constant
C constant is showing in the figure:
A variable is a meaningful name of data storage location in computer memory. When using a variable you refer to memory address of computer.
Types of variables In C variable are divided in two types
- Global variable: global variable is a variable defined outside any function block. Global variable is also called external variable.
- Local variable: local variable is a variable defined inside a function block. Local variable is also called automatic variables.
Variable Declaration Examples
Single declarations
int age; float amount; char last;
Multiple declarationsYou can repeat the the declaration line with a different variable on each line .
int age; int hrNumber;
But the normal way is to separate the variables by a comma. Although not required the names are easier to read if a space follows the comma.
int age, HouseNumber, quality; float distance, Discount; char first, second;
Scope of variables
The scope of a variable depends on where it is declared.
- The area of the program where that variable is valid, i.e. the parts of the program that have access to that variable. This is determined by the location of the declaration.
- The life span of that variable, i.e. the length of time that the variable remains in memory.
Variables can be declared as
- Prior to the start of the main( ) function.
- Within the main function, after the opening {
- Within a function after the opening {
- Within a block of code, after the {
- Global or external variable Declared before the start of the main( ) function.
Static Variable
When static is used with a global variable it indicates that the variable is local to the current file. If 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.
Example
static int aVariable = 1;
In the example the initialisation to 1 occurs only on the first execution of this line of code, it is disregarded on further executions of the line.
static has a different meaning when used with global variables.
%c | The character format specifier. |
%d | The integer format specifier. |
%i | The integer format specifier (same as %d). |
%f | The floating-point format specifier. |
%e | The scientific notation format specifier. |
%E | The scientific notation format specifier. |
%g | Uses %f or %e, whichever result is shorter. |
%G | Uses %f or %E, whichever result is shorter. |
%o | The unsigned octal format specifier. |
%s | The string format specifier. |
%u | The unsigned integer format specifier. |
%x | The unsigned hexadecimal format specifier. |
%X | The unsigned hexadecimal format specifier. |
%p | Displays the corresponding argument that is a pointer. |
%n | Records the number of characters written so far. |
%% | Outputs a percent sign. |
Previous | Home | Next |