show some basic flow blocks

Upload: bonadefkijio

Post on 30-May-2018

217 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/9/2019 Show Some Basic Flow Blocks

    1/10

    Loops and Iterations

    /*****************************************************************************

    name: show_flow

    purpose: show some basic flow blocks

    args: none

    returns: total score*****************************************************************************/

    int show_flow()

    {

    /* vars */

    int score = 0; /* holds num of iterations */

    int i; /* our mighty iterator */

    char ch; /* input character */

    /* for statement */

    for (i=0; i0) /* loop 2 times */

    {

    score++;

    i = i - 2;

    }

    /* do-while statement, check condition after */

    do /* loop 1 time */

    {

    score++;abrami--;

    } while (i>0);

  • 8/9/2019 Show Some Basic Flow Blocks

    2/10

    /* continue and break can help decide locally */

    while (1) /* loop forever */

    {

    ch = get_input_from_user();

    if (ch == 'q') break; /* exit the loop */

    if (ch == 's') continue; /* continue to the next iteration */

    score++;}

    return score;

    }

    Structures and Enums

    /*****************************************************************************

    name: show_moredata

    purpose: show some more data types

    args: nonereturns: nothing

    *****************************************************************************/

    void show_moredata()

    {

    /* types */

    struct gun {

    char name[50];

    int magazine_size;

    float calibre; }; /* structure */

    typedef struct gun gun_type; /* simplify the type */

    enum days {sun = 1,mon,tue,wed,thr,fri,sat}; /* int values with names */

    /* vars */

    struct gun glock = {"Glock",17,0.9}; /* init by members */

    gun_type m16 = {0}; /* init all to zero */

    gun_type *gun_ptr = &m16; /* ptr to the m16 gun */

    enum days today = wed;

    glock.magazine_size++;

  • 8/9/2019 Show Some Basic Flow Blocks

    3/10

    score+

    +;abramgun_ptr->magazine_size = glock.magazine_size + 13;today++; /* today is actually int */}Dynamic Memory

    #include

    #define NULL (0)

    /*****************************************************************************

    name: show_dynamic_mem

    purpose: show usage of dynamic memory allocation

    args: (in) size - size of buffer to use (in bytes)

    (in) elements - num of elements to hold

    returns: 0 on success, -1 on failure

    *****************************************************************************/

    int show_dynamic_mem(int size, int elements)

    {

    /* vars */char *buffer = NULL;

    int *dyn_int_array = NULL;

    /* allocate buffer of size bytes */

    buffer = malloc(size);

    if (NULL == buffer) return -1;

    /* allocate dynamic array of elements ints */

    dyn_int_array = malloc(elements * sizeof(int));

    if (NULL == dyn_int_array)

    {free(buffer);

    return -1;

  • 8/9/2019 Show Some Basic Flow Blocks

    4/10

    }

    do_something(buffer, dyn_int_array);

    /* cleanup */

    free(buffer);

    free(dyn_int_array);return 0;

    }

    Standard IO

    #include

    #define NULL (0)

    /*****************************************************************************

    name: show_stdio

    purpose: show usage of standard input / outputargs: (in) filename - name of file to work with

    returns: 0 on success, -1 on failure

    *****************************************************************************/

    int show_stdio(char *filename)

    {

    /* vars */

    FILE *input = NULL;

    unsigned long dword = 0;

    int items_read = 0;

    /* open the file in binary mode for read only */

    input = fopen(filename, "rb");

    if (NULL == input)

    {

    printf("cant open %s\n",filename);

    return -1;

    }

    /* read dwords from the file */

    while (!feof(input))

    {

    items_read = fread(&dword, sizeof(dword), 1, input);if (1 == items_read) printf("read from %s the dword %08x\n",filename,dword);

    else break;

    }

    /* cleanup */

    fclose(input);

    return 0;

    }

    Preprocessor and H Files

    /*****************************************************************************name: h_file_example

    purpose: demonstrate usage of an h file and the preprocessor

  • 8/9/2019 Show Some Basic Flow Blocks

    5/10

    *****************************************************************************/

    #ifndef __H_FILE_EXAMPLE__

    #define __H_FILE_EXAMPLE__

    #include "another_h_file.h"

    /* defines */#define NULL (0)

    #define TRUE (1)

    #define FALSE (0)

    #define GREETING "hello world"

    /* macros */

    #define max(A,B) ((A)>(B)?(A):(B))

    extern int global_in_c; /* declare a global defined in matching c file */

    /*****************************************************************************name: show_types

    purpose: show some basic types and type operations

    args: (out) result - ptr to the result of this func

    returns: nothing

    *****************************************************************************/

    void show_types(int *result);

    /*****************************************************************************

    name: show_flow

    purpose: show some basic flow blocks

    args: none

    returns: total score

    *****************************************************************************/

    int show_flow();

    #endif /*__H_FILE_EXAMPLE__*/

  • 8/9/2019 Show Some Basic Flow Blocks

    6/10

  • 8/9/2019 Show Some Basic Flow Blocks

    7/10

  • 8/9/2019 Show Some Basic Flow Blocks

    8/10

    suparman

  • 8/9/2019 Show Some Basic Flow Blocks

    9/10

  • 8/9/2019 Show Some Basic Flow Blocks

    10/10

    suparman