+ All Categories
Home > Documents > Keyword Volatile is an Extreme Opposite of Const. It Indicates That a Variable May Be Changed in a...

Keyword Volatile is an Extreme Opposite of Const. It Indicates That a Variable May Be Changed in a...

Date post: 30-May-2018
Category:
Upload: bonadefkijio
View: 222 times
Download: 0 times
Share this document with a friend

of 18

Transcript
  • 8/9/2019 Keyword Volatile is an Extreme Opposite of Const. It Indicates That a Variable May Be Changed in a Way Which is A

    1/18

    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 inbraces). 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.

    Once a value is computed for expression, the list of possible constant-expression values determined

    from all case statements is searched for a match. If a match is found, execution continues after the

    matching case statement and continues until a break statement is encountered or the end ofstatement is reached. If a match is not found and this statement prefix is found within statement,

    default :

    execution continues at this point. Otherwise, statement is skipped entirely. 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:

    x++;

    case INCREMENT1:

    x++; break;

    case EXPONENT:case ROOT:

    case MOD:

    printf ("Not implemented!\n");

    break;

    default:

    printf("Bug!\n");

    exit(1);

    }

    See also break.

    Note: GNU C extends the case keyword to allow case ranges.

  • 8/9/2019 Keyword Volatile is an Extreme Opposite of Const. It Indicates That a Variable May Be Changed in a Way Which is A

    2/18

    --------------------------------------------------------------------------------

    typedef

    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;

    with the same meaning as you declare

    unsigned char m, n;

    char myStr[41];

    struct {float re, im;} z1, z2;

    char *p;

    int (*myFunc)(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.

    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,

  • 8/9/2019 Keyword Volatile is an Extreme Opposite of Const. It Indicates That a Variable May Be Changed in a Way Which is A

    3/18

    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.

    --------------------------------------------------------------------------------

    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;

    }

    This is not the same as defining

    int init ()

    {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).

    Pointers can also be declared as void. They can't be dereferenced without explicit casting. This is

    because the compiler can't determine the size of the object the pointer points to. For example,

    int x;

    float f;

  • 8/9/2019 Keyword Volatile is an Extreme Opposite of Const. It Indicates That a Variable May Be Changed in a Way Which is A

    4/18

    void *p = &x; // p points to x

    *(int*)p = 2;

    p = &r; // p points to r

    *(float*)p = 1.1;

    --------------------------------------------------------------------------------

    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, avariable which may be changed by an interrupt handler). This keyword uses the following syntax:

    volatile data-definition;

    Every reference to the variable will reload the contents from memory rather than take advantage of

    situations where a copy can be in a register.

    --------------------------------------------------------------------------------

    whileRepeats execution while the condition is true.

  • 8/9/2019 Keyword Volatile is an Extreme Opposite of Const. It Indicates That a Variable May Be Changed in a Way Which is A

    5/18

    Keyword while is the most general loop statemens. It uses the following syntax:

    while (expression) statement

    statement is executed repeatedly as long as the value of expression remains nonzero. The test takes

    place before each execution of the statement. For example,

    while (*p == ' ') p++;

    Of course, statement may be a compound statement as well.

    --------------------------------------------------------------------------------

    Return to the main index

  • 8/9/2019 Keyword Volatile is an Extreme Opposite of Const. It Indicates That a Variable May Be Changed in a Way Which is A

    6/18

  • 8/9/2019 Keyword Volatile is an Extreme Opposite of Const. It Indicates That a Variable May Be Changed in a Way Which is A

    7/18

  • 8/9/2019 Keyword Volatile is an Extreme Opposite of Const. It Indicates That a Variable May Be Changed in a Way Which is A

    8/18

  • 8/9/2019 Keyword Volatile is an Extreme Opposite of Const. It Indicates That a Variable May Be Changed in a Way Which is A

    9/18

  • 8/9/2019 Keyword Volatile is an Extreme Opposite of Const. It Indicates That a Variable May Be Changed in a Way Which is A

    10/18

  • 8/9/2019 Keyword Volatile is an Extreme Opposite of Const. It Indicates That a Variable May Be Changed in a Way Which is A

    11/18

  • 8/9/2019 Keyword Volatile is an Extreme Opposite of Const. It Indicates That a Variable May Be Changed in a Way Which is A

    12/18

  • 8/9/2019 Keyword Volatile is an Extreme Opposite of Const. It Indicates That a Variable May Be Changed in a Way Which is A

    13/18

  • 8/9/2019 Keyword Volatile is an Extreme Opposite of Const. It Indicates That a Variable May Be Changed in a Way Which is A

    14/18

  • 8/9/2019 Keyword Volatile is an Extreme Opposite of Const. It Indicates That a Variable May Be Changed in a Way Which is A

    15/18

  • 8/9/2019 Keyword Volatile is an Extreme Opposite of Const. It Indicates That a Variable May Be Changed in a Way Which is A

    16/18

  • 8/9/2019 Keyword Volatile is an Extreme Opposite of Const. It Indicates That a Variable May Be Changed in a Way Which is A

    17/18

  • 8/9/2019 Keyword Volatile is an Extreme Opposite of Const. It Indicates That a Variable May Be Changed in a Way Which is A

    18/18


Recommended