This chapter examines the most fundamental element of the C language: the expression. Expressions
in C are substantially more flexible and powerful than in many other computer languages.
Expressions are formed from these atomic elements: data and operators. Data may be represented by
variables, constants, or values returned by functions. C supports several different types of data. It
also provides a wide variety of operators.
The Basic Data Types
C89 defines five foundational data types: character, integer, floating-point, double floating-point,
and valueless. These are declared using char, int, float, double, and void, respectively.
The size and range of these data types may vary among
processor types and compilers.
In all cases an object of type char is 1 byte.
In 16-bit operators int is 2 bytes , in 32-bit and 64-bit operators int is 4 bytes.
float is 4 bytes.
double is 8 bytes.
The type void either explicitly declares a function as returning no value or creates generic pointers.
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 default integer declaration
assumes a
signed number.
If
you specify a signed integer, the compiler generates code that assumes the high-order bit of an
integer is to be used as a sign flag. If the sign flag is 0, the number is positive; if it is 1, the number
is negative.
Identifier Names
In C, the names of variables, functions, labels, and various other user-defined items are called
identifiers. The length of these identifiers can vary from one to several characters. The first character
must be a letter or an underscore, and subsequent characters must be either letters, digits, or
underscores. Here are some correct and incorrect identifier names:
Correct Incorrect
count 1count
test23 hi!there
high_balance high . . . balance
- In C, identifiers may be of any length.
- C
defines two kinds of identifiers: external and internal. An external identifier will be involved in an
external link process. These identifiers, called external names, include function names and global
variable names that are shared between source files. If the identifier is not used in an external link
process, then it is internal. This type of identifier is called an internal name and includes the names
of local variables, for example.
- In an identifier, upper- and lowercase are treated as distinct. Hence, count, Count, and COUNT are
three separate identifiers.
- An identifier cannot be the same as a C keyword and should not have the same name as functions
that are in the C library.
Variables
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,
and outside of all functions. These positions correspond to local variables, formal parameters, and
global variables, respectively.
Local Variables
Variables that are declared inside a function are called local variables.
s. In some C literature, these
variables are referred to as automatic variables.
- 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
brace.
The most common code block in which local variables are declared is the function.
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 nonglobal variables are, by default, assumed to be auto, this keyword is virtually never
used.
- you may declare local variables within any code block.
- When a variable declared within an inner block has the same name as a variable declared by an
enclosing block, the variable in the inner block hides the variable in the outer block.
#include <stdio.h>
int main(void)
{
int x;
x = 10;
if(x == 10)
{
int x;
/* this x hides the outer x */
x = 99;
printf("Inner x: %d\n", x);
}
printf("Outer x: %d\n", x);
return 0;
}
The program displays this output:
Inner x: 99
Outer x: 10
In this example, the x that is declared within the if block hides the outer x. Thus, the inner x and the
outer x are two separate and distinct objects. Once that block ends, the outer x once again becomes
visible.
Formal Parameters
If a function is to use arguments, it must declare variables that will accept the values of the
arguments. These variables are called the formal parameters of the function. They behave like any
other local variables inside the function.
Even though the formal parameters receive the value of the arguments passed to the function, they
otherwise act like ''normal" local variables.
Global Variables
Unlike local variables, global variables are known throughout the program and may be used by any
piece of code. Also, they will hold their value throughout the program's execution. You create global
variables by declaring them outside of any function. Any expression may access them, regardless of
what block of code that expression is in.
In the following program, the variable count has been declared outside of all functions. Although its
declaration occurs before the main( ) function, you could have placed it anywhere before its first use
as long as it was not in a function. However, it is usually best to declare global variables at the top of
the program.
#include <stdio.h>
int count; /* count is global */
void func1(void);
void func2(void);
int main(void)
{
count = 100;
func1();
return 0;
}
void func1(void)
{
int temp;
temp = count;
func2();
printf("count is %
d", count); /* will print 100 */
}
void func2(void)
{
int count;
for(count=l; count<10; count++)
putchar('.');
}
Look closely at this program. Notice that although neither main( ) nor func1( ) has declared the
variable count, both may use it. func2( ), however, has declared a local variable called count. When
func2( ) refers to count, it refers to only its local variable, not the global one. If a global variable
and a local variable have the same name, all references to that variable name inside the code block
in which the local variable is declared will refer to that local variable and have no effect on the
global variable.
the terms local and global
are used to describe in a general way the difference between identifiers that are declared within a
block and those declared outside all blocks.
The Four C Scopes
Standard C defines four scopes that determine the visibility of an identifier.
Comments
Post a Comment