show some basic types and type operations

Upload: bonadefkijio

Post on 30-May-2018

215 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/9/2019 Show Some Basic Types and Type Operations

    1/17

    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)

    {

    /* vars */

    int number = 0; /* whole signed num register size */

    char letter = 'c'; /* whole signed num 1 byte size */

    char *string = "hello"; /* ptr to null-term char array */

    char buffer[50]; /* ptr to a buffer of 50 bytes */

    int array[3] = {1,2,3}; /* ptr to 3 member array of ints */

    float fraction = 3.14; /* real num 4 byte size */

    double bigfrac = 1.41; /* real num 8 byte size */

    unsigned long num = 22; /* whole unsigned num 4 byte size */signed short num2 = -17; /* whole signed num 2 byte size */

    int multi_arr[5][2]; /* ptr to 5 arrays of 2 ints */

    static int times_called = 0;

    /* var operations */

    number = (int)letter + 1; /* number now equals 'd' which is 0x64 */

    number++; /* number now equals 'e' which is 0x65 */

    number = number % 2; /* number now equals 1 (modulus) */

    letter -= 2; /* letter now equals 'a' which is 0x61 */

    num = num ^ 0x55555555; /* num is xored with 0x55555555 */

    string[0] = 'j'; /* string now points to "jello", 0 is first pos */

    *(string+4) = 'y'; /* string now points to "jelly" */

    string = &letter; /* string now points to letter, which is 'a' */

    fraction = 2 * 2.3 / 1.2; /* fraction now equals 3.83333 */

    num = (~0x00) & 0x03 | (1

  • 8/9/2019 Show Some Basic Types and Type Operations

    2/17

    if ((0 == age) && (age != 1)) return; /* just born */

    /* if-else statement */

    if ((age < 18) || !(age

  • 8/9/2019 Show Some Basic Types and Type Operations

    3/17

  • 8/9/2019 Show Some Basic Types and Type Operations

    4/17

    glock.magazine_size++;

    gun_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;

    }

    do_something(buffer, dyn_int_array);

    /* cleanup */

    free(buffer);free(dyn_int_array);

    return 0;

    }

    Standard IO

    #include

    #define NULL (0)

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

    name: show_stdiopurpose: show usage of standard input / output

    args: (in) filename - name of file to work with

  • 8/9/2019 Show Some Basic Types and Type Operations

    5/17

    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");

    robert

    adhiif (NULL == input)

    {

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

    return -1;

    }

    /* read dwords from the file */

  • 8/9/2019 Show Some Basic Types and Type Operations

    6/17

    robert

    adhi

  • 8/9/2019 Show Some Basic Types and Type Operations

    7/17

    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

    *****************************************************************************/#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 */

    abram#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);

  • 8/9/2019 Show Some Basic Types and Type Operations

    8/17

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

    name: show_flow

    purpose: show some basic flow blocks

    args: none

    returns: total score

    *****************************************************************************/int show_flow();

    #endif /*__H_FILE_EXAMPLE__*/

    Powered by Notepa

  • 8/9/2019 Show Some Basic Types and Type Operations

    9/17

    abram

  • 8/9/2019 Show Some Basic Types and Type Operations

    10/17

  • 8/9/2019 Show Some Basic Types and Type Operations

    11/17

  • 8/9/2019 Show Some Basic Types and Type Operations

    12/17

  • 8/9/2019 Show Some Basic Types and Type Operations

    13/17

  • 8/9/2019 Show Some Basic Types and Type Operations

    14/17

  • 8/9/2019 Show Some Basic Types and Type Operations

    15/17

  • 8/9/2019 Show Some Basic Types and Type Operations

    16/17

  • 8/9/2019 Show Some Basic Types and Type Operations

    17/17