Defines A Local Variable As Having A Local Lifetime.: Nested Functions
Defines A Local Variable As Having A Local Lifetime.: Nested Functions
Defines A Local Variable As Having A Local Lifetime.: Nested Functions
As the local lifetime is the default for local variables, auto keyword is
extremely rarely used.
break
Passes control out of the compound statement.
const
Makes variable value or pointer parameter unmodifiable.
In this case, the const modifier allows you to assign an initial value
to a variable that cannot later be changed by the program. For
example,
const my_age = 32;
Any assignments to 'my_age' will result in a compiler error. However,
such declaration is quite different than using
#define my_age 32
In the first case, the compiler allocates a memory for 'my_age' and
stores the initial value 32 there, but it will not allow any later
assignment to this variable. But, in the second case, all occurences
of 'my_age' are simply replaced with 32 by the preprocessor, and no
memory will be allocated for it.
Then, the function cannot modify the variable that the pointer
points to. For example,
int printf (const char *format, ...);
continue
Passes control to the begining of the loop.
For example,
for (i = 0; i < 20; i++)
{
if (array[i] == 0)
continue;
array[i] = 1/array[i];
}
This example changes each element in the array with its reciprocal,
but skips elements which are equal to zero.
do
Do-while loop.
enum
Defines a set of constants of type int.
The set can optionally be given a type tag name with tag. name is
the name of a constant that can optionally be assigned the
(constant) value of value, etc. For example,
enum Numbers {One = 1, Two = 2, Three = 3, Four = 4, Five = 5};
If you give a type tag name, then you can declare variables of
enumerated type using
enum tag variable-names;
For example,
enum Numbers x, y, z;
extern
Indicates that an identifier is defined elsewhere.
Keyword extern indicates that the actual storage and initial value of a
variable, or body of a function, is defined elsewhere, usually in a
separate source code module. So, it may be applied to data
definitions and function prototypes:
extern data-definition;
extern function-prototype;
For example,
extern int _fmode;
extern void Factorial (int n);
float, double
Floating point data types.
for
For loop.
For-loop is yet another kind of loop. It uses for keyword, with the
following syntax:
for ([expr1]; [expr2]; [expr3]) statement
That's why expr1 and expr3 must contain side effects, else they are
useless. For example,
for (i=0; i<100; i++) sum += x[i];
for (i=0, t=string; i<40 && *t; i++, t++) putch(*t);
putch('\n');
goto
Unconditionally transfer control.
Jumping out of scope (for example out of the body of the for loop)
is legal, but jumping into a scope (for example from one function to
another) is notallowed.
if, else
Conditional statement.
Keyword if is used for conditional execution. The basic form
of if uses the following syntax:
if (expression)
statement1
if (x < y) z = x;
else z = y;
if (x < y)
{
printf ("x is smaller");
return x;
}
else
{
printf ("x is greater")
return y;
}
Variables of type int are one machine-type word in length. They can
be signed (default) or unsigned, which means that in this
configuration of the compiler they have by default a range of -
32768 to 32767 and 0 to 65535 respectively, but this default may
be changed if the compiler option '-mnoshort' is given. In this
case, the range of type int is -2147483648 to 2147483647 for
signed case, or 0 to 4294967295 for unsigned case. See
alsoshort and long type modifiers.
Variables of type char are 1 byte in length. They can be signed (this
is the default, unless you use the compiler option '-funsigned-
char') or unsigned, which means they have a range of -128 to 127
and 0 to 255, respectively.
All data types may be used for defining variables, specifying return
types of functions, and specifying types of function arguments. For
example,
int a, b, c; // 'a', 'b', 'c' are integer variables
int func (); // 'func' is a function returning int
char crypt (int key, char value); // 'crypt' is a function returning char
with
// two args: 'key' is int and 'value' is
char
register
Tells the compiler to store the variable being declared in a
CPU register.
The register type modifier tells the compiler to store the variable
being declared in a CPU register (if possible), to optimize access.
For example,
register int i;
Note that TIGCC will automatically store often used variables in CPU
registers when the optimization is turned on, but the
keyword register will force storing in registers even if the
optimization is turned off. However, the request for storing data in
registers may be denied, if the compiler concludes that there is not
enough free registers for use at this place.
return
Exits the function.
For example,
int sqr (int x)
{
return (x*x);
}
A type modifier alters the meaning of the base type to yield a new
type. Each of these type modifiers can be applied to the base
type int. The modifierssigned and unsigned can be applied to the base
type char. In addition, long can be applied to double.
sizeof
Returns the size of the expression or type.
For example,
workspace = calloc (100, sizeof (int));
memset(buff, 0, sizeof buff);
nitems = sizeof (table) / sizeof (table[0]);
Note that type may be an anonymous type (see asterisk for more
info about anonymous types).
static
Preserves variable value to survive after its scope ends.
For example,
static int i = 10;
static void PrintCR (void) { putc ('\n'); }
The fact that global and static variables are initialized in compile-
time and kept in the executable file itself has one serious
consequence, which is not present on "standard" computers like PC,
Mac, etc. Namely, these computers always reload the executable on
each start from an external memory device (disk), but this is not
the case on TI. So, if you have the following global (or static)
variable
int a = 10;
Note, however, that if the program is archived, the initial values will
be restored each time you run the program, because archived
programs are reloaded from the archive memory to the RAM on
each start, similarly like the programs are reloaded from disks on
"standard" computers each time when you start them.
struct
Groups variables into a single record.
typedef
Creates a new type.
This statement assigns the symbol name identifier to the data type
definition type-definition. For example,
typedef unsigned char byte;
typedef char str40[41];
typedef struct {float re, im;} complex;
typedef char *byteptr;
typedef int (*fncptr)(int);
User defined types may be used at any place where the built-in
types may be used.
union
Groups variables which share the same storage space.
For example,
union short_or_long
{
short i;
long l;
} a_number;
void
Empty data type.
When used as a function return type, void means that the function
does not return a value. For example,
void hello (char *name)
{
printf("Hello, %s.", name);
}
When found in a function heading, void means the function does not
take any parameters. For example,
int init (void)
{
return 1;
}
because in the second case the compiler will not check whether the
function is really called with no arguments at all; instead, a function
call with arbitrary number of arguments will be accepted without
any warnings (this is implemented only for the compatibility with
the old-style function definition syntax).
volatile
Indicates that a variable can be changed by a background
routine.
Keyword volatile is an extreme opposite of const. It indicates that a
variable may be changed in a way which is absolutely unpredictable
by analysing the normal program flow (for example, a variable
which may be changed by an interrupt handler). This keyword uses
the following syntax:
volatile data-definition;
while
Repeats execution while the condition is true.