Monday, 2 January 2012

Keywords in "C"

A keyword is a word that is part of C Language itself. These words have predefined meanings and these words cannot be used as variable names.

C Keywords

Defines a local variable as having a local lifetime.
Keyword auto uses the following syntax:
[auto] data-definition;
As the local lifetime is the default for local variables, auto keyword is extremely rarely used.
Passes control out of the compound statement.
The break statement causes control to pass to the statement following the innermost enclosing while, do, for, or switch statement. 
The syntax is simply
Makes variable value or pointer parameter unmodifiable.
when const is used with a variable.
It uses the following syntax:
const variable-name [ = value];
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;
Passes control to the begining of the loop.
continue causes control to pass to the end of the innermost enclosing while, do, or for statement, at which point the loop continuation condition is re-evaluated. 
The syntax is simply
Do-while loop.
Keyword do is usually used together with while to make another form of repeating statement.
Such form of the loop uses the following syntax:
do statement while (expression)
statement, which is usually a compound statement, is executed repeatedly as long as the value of expression remains non-zero. The test takes place after each execution of the statement. 
For example,
i = 1; n = 1;
    n *= i;
  } while (i <= factorial);
Defines a set of constants of type int.
The syntax for defining constants using enum is
enum [tag] {name [=value], ...};
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 value is missing, then a value is assumed to be the value of the previous constant in the list + 1.
If this is the first constant in the list, the default value is 0. 
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;
float, double
Floating point data types.
The keyword float usually represents a single precision floating point data type, and double represents a double precision floating point data type.
For loop.
For-loop is yet another kind of loop. It uses for keyword, with the following syntax:
for ([expr1]; [expr2]; [expr3]) statement
statement is executed repeatedly until the value of expr2 is 0.
Before the first iteration, expr1 is evaluated. 
This is usually used to initialize variables for the loop.
After each iteration of the loop, expr3 is evaluated. 
Unconditionally transfer control.
goto may be used for transferring control from one place to another. 
The syntax is:
goto identifier;
Control is unconditionally transferred to the location of a local label specified by identifier.
For example,
  goto Again;
if, else
Conditional statement.
Keyword if is used for conditional execution. 
The basic form of if uses the following syntax:
if (expression)
Alternatively, if may be used together with else, using the following syntax:
if (expression)
int, char
Basic data types (integer and character).
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 also short 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. 
Tells the compiler to store the variable being declared in a CPU register.
In standard C dialects, keyword auto uses the following syntax:
register data-definition;
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;
Exits the function.
return exits immediately from the currently executing function to the calling routine, optionally returning a value. 
The syntax is:
return [expression];
short, long, signed, unsigned
They are also calles as "Type modifiers".
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 modifiers signed and unsigned can be applied to the base type char. In addition, long can be applied to double. 
When the base type is omitted from a declaration, int is assumed. For example,
long x;                 // 'int' is implied
unsigned char ch;
signed int i;           // 'signed' is default
unsigned long int l;    // 'int' is accepted, but not needed
Returns the size of the expression or type.
Keyword sizeof is, in fact, an operator. It returns the size, in bytes, of the given expression or type (as type size_t). Its argument may be an expression of a type name:
sizeof expression
sizeof (type)
Preserves variable value to survive after its scope ends.
Keyword static may be applied to both data and function definitions:
static data-definition;
static function-definition
Groups variables into a single record.
The syntax for defining records is:
struct [struct-type-name]
    [type variable-names] ;
  } [structure-variables] ;
A struct, like an union, groups variables into a single record. The struct-type-name is an optional tag name that refers to the structure type. The structure-variables are the data definitions, and are also optional. Though both are optional, one of the two must appear. 
Elements in the record are defined by naming a type, followed by variable-names separated by commas. Different variable types can be separated by a semicolon.
For example,
struct my_struct
    char name[80], phone_number[80];
    int age, height;
  } my_friend;
switch, case, default
Branches control.
switch causes control to branch to one of a list of possible statements in the block of statements. 
The syntax is
switch (expression) statement
The statement statement is typically a compound statement (i.e. a block of statements enclosed in braces). The branched-to statement is determined by evaluating expression, which must return an integral type. The list of possible branch points within statement is determined by preceding substatements with
case constant-expression :
where constant-expression must be an int and must be unique. 
For example,
switch (operand)
    case MULTIPLY:
      x *= y; break;
    case DIVIDE:
      x /= y; break;
    case ADD:
      x += y; break;
    case SUBTRACT:
      x -= y; break;
    case INCREMENT2:
    case INCREMENT1:
      x++; break;
    case EXPONENT:
    case ROOT:
    case MOD:
      printf ("Not implemented!\n");
Creates a new type.
The syntax for defining a new type is
typedef type-definition identifier;
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);
After these definition, you can declare
byte m, n;
str40 myStr;
complex z1, z2;
byteptr p;
fncptr myFunc;
Groups variables which share the same storage space.
A union is similar to a struct, except it allows you to define variables that share storage space. 
The syntax for defining unions is:
union [union-type-name]
    type variable-names;
  } [union-variables] ;

For example,
union short_or_long
    short i;
    long l;
  } a_number;
The compiler will allocate enough storage in a number to accommodate the largest element in the union. 
Elements of a union are accessed in the same manner as a struct. 
Unlike a struct, the variables 'a_number.i' and 'a_number.l' occupy the same location in memory. 
Thus, writing into one will overwrite the other.
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;
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;
Repeats execution while the condition is true.
Keyword while is the most general loop statemens. 
It uses the following syntax:
while (expression) statements;

No comments:

Post a Comment

host gator coupon