In C language five foundational data types:
Modifying the Basic Types
Except type void, the basic data types may have various modifiers preceding them. A type modifier alters the meaning of the base type to more precisely fit a specific need.
The list of modifiers is shown here:
The int base type can be modified by signed, short, long, and unsigned. The char type can be
modified by unsigned and signed. You may also apply long to double.
- Character
- Integer
- floating-point
- double floating-point and valueless
- Char
- Int
- Float
- Double
- Void
Modifying the Basic Types
Except type void, the basic data types may have various modifiers preceding them. A type modifier alters the meaning of the base type to more precisely fit a specific need.
The list of modifiers is shown here:
- signed
- unsigned
- long
- short
The int base type can be modified by signed, short, long, and unsigned. The char type can be
modified by unsigned and signed. You may also apply long to double.
The below table demonstrate the value of each variable
char 8 –127 to 127
unsigned char 8 0 to 255
signed char 8 –127 to 127
int 16 or 32 –32,767 to 32,767
unsigned int 16 or 32 0 to 65,535
signed int 16 or 32 Same as int
short int 16 –32,767 to 32,767
unsigned short int 16 0 to 65,535
signed short int 16 Same as short int
long int 32 –2,147,483,647 to 2,147,483,647
signed long int 32 Same as long int
unsigned long int 32 0 to 4,294,967,295
float 32 1E–37 to 1E+37 with six digits of precision
double 64 1E–37 to 1E+37 with ten digits of precision
long double 80 1E–37 to 1E+37 with ten digits of precision
Variables
As you probably know, a variable is a named location in memory that is used to hold a value that
can be modified by the program. All variables must be declared before they can be used. The general form of a declaration is;
type variable_list;
Here, type must be a valid data type plus any modifiers, and variable_list may consist of one or
more identifier names separated by commas. Here are some declarations:
int i, j, l;
short int si;
unsigned int ui;
double balance, profit, loss;
Remember, in C the name of a variable has nothing to do with its type.
Where Variables Are Declared?
Variables can be declared in three places:
Local Variables
Variables that are declared inside a function are called local variables. In some C literature, these variables are referred to as automatic variables. This book uses the more common term local variable. Local variables can be used only by statements that are inside the block in which the variables are declared. In other words, local variables are not known outside their own code block. Remember, a block of code begins with an opening curly brace and terminates with a closing curly braces { }.
Local variables exist only while the block of code in which they are declared is executing. That is, a local variable is created upon entry into its block and destroyed upon exit. Further more, a variable declared within one code block has no bearing on or relationship to another variable with the same name declared within a different code block.
The most common code block in which local variables are declared is the function. For example consider the following two functions:
void func1(void)
{
int x;
x = 10;
}
void func2(void)
{
int x;
x = -199;
}
The integer variable x is declared twice, once in func1( ) and once in func2( ). The x in func1( ) has no bearing on or relationship to the x in func2( ). As explained, this is because each x is known only to the code within the block in which it is declared.
The C language contains the keyword auto, which you can use to declare local variables. However, since all non global variables are, by default, assumed to be auto, this keyword is virtually never used. Hence, the examples in this book will not use it.
char 8 –127 to 127
unsigned char 8 0 to 255
signed char 8 –127 to 127
int 16 or 32 –32,767 to 32,767
unsigned int 16 or 32 0 to 65,535
signed int 16 or 32 Same as int
short int 16 –32,767 to 32,767
unsigned short int 16 0 to 65,535
signed short int 16 Same as short int
long int 32 –2,147,483,647 to 2,147,483,647
signed long int 32 Same as long int
unsigned long int 32 0 to 4,294,967,295
float 32 1E–37 to 1E+37 with six digits of precision
double 64 1E–37 to 1E+37 with ten digits of precision
long double 80 1E–37 to 1E+37 with ten digits of precision
Variables
As you probably know, a variable is a named location in memory that is used to hold a value that
can be modified by the program. All variables must be declared before they can be used. The general form of a declaration is;
type variable_list;
Here, type must be a valid data type plus any modifiers, and variable_list may consist of one or
more identifier names separated by commas. Here are some declarations:
int i, j, l;
short int si;
unsigned int ui;
double balance, profit, loss;
Remember, in C the name of a variable has nothing to do with its type.
Where Variables Are Declared?
Variables can be declared in three places:
- inside functions
- in the definition of function parameters
- outside of all functions
Local Variables
Variables that are declared inside a function are called local variables. In some C literature, these variables are referred to as automatic variables. This book uses the more common term local variable. Local variables can be used only by statements that are inside the block in which the variables are declared. In other words, local variables are not known outside their own code block. Remember, a block of code begins with an opening curly brace and terminates with a closing curly braces { }.
Local variables exist only while the block of code in which they are declared is executing. That is, a local variable is created upon entry into its block and destroyed upon exit. Further more, a variable declared within one code block has no bearing on or relationship to another variable with the same name declared within a different code block.
The most common code block in which local variables are declared is the function. For example consider the following two functions:
void func1(void)
{
int x;
x = 10;
}
void func2(void)
{
int x;
x = -199;
}
The integer variable x is declared twice, once in func1( ) and once in func2( ). The x in func1( ) has no bearing on or relationship to the x in func2( ). As explained, this is because each x is known only to the code within the block in which it is declared.
The C language contains the keyword auto, which you can use to declare local variables. However, since all non global variables are, by default, assumed to be auto, this keyword is virtually never used. Hence, the examples in this book will not use it.
No comments:
Post a Comment