C programming language - ch3

Home

Table of Contents

1 Compiling the examples

Compile with the standard ./configure ; make

Additionally add the following make options to turn off optimizations and enable debugging symbols

./configure

make CXXFLAGS='-g3 -Wall -O0 '  CFLAGS='-g3 -Wall -O0 '

when building C samples, you can also create separate build directory

mkdir build
cd build
../configure
make

2 Examples

2.1 atoi2.c

Convert a string to integer

#include <stdio.h>
#include <ctype.h>

/* atoi2: convert s to interger; version 2  */
int atoi2(char s[]);

int main()
{

    char str[] = "-123a";
    printf ("%d\n", atoi2( str ));

    return 0;
}

/* atoi2: convert s to integer; version 2  */
int atoi2(char s[])
{
    int i, n, sign;
    for (i = 0; isspace(s[i]); i++)  /* skip white space  */
        ;

    sign = (s[i] == '-') ? -1 : 1;
    if (s[i] == '+' || s[i] == '-' ) /* skip sign  */
        i++;
    for (n = 0; isdigit(s[i]); i++)
        n = 10 * n + (s[i] - '0');
    return sign * n;
}

2.2 ch3cf.c

Binary search inside sorted array

#include <stdio.h>

/* binsearch: find x in v[0] <= v[1] <= ... <= v[n-1]
   return the position starting at zerp or -1 if not found.
*/
int binsearch(int x, int v[], int n);

int arr_test[5] = {3,5,9,10,22};

int main()
{
    char str[] = "Hello there";
    printf ("%s\n", str);

    int testval = 9;
    int result = binsearch(testval, arr_test, 5);

    printf ("binsearch found in pos: %d\n", result);
}


int binsearch(int x, int v[], int n)
{
    int low, high, mid;
    low = 0;
    high = n - 1;
    while (low <= high ) {
        mid = (low+high)/2;
        if (x < v[mid])
            high = mid + 1;
        else if (x > v[mid])
            low = mid + 1;
        else /* found match  */
            return mid;
    }
    return -1; /* no match  */

}

2.3 reversestring.c

The comma operator example

#include <string.h>
#include <stdio.h>

/* The comma operator examples  */


/* reverse2: reverse string s in place  */
void reverse2(char s[]);

int main(int argc, char *argv[])
{
    char str[] = "this is a test string";

    reverse2( str );
    printf ("%s\n", str);

    return 0;
}



void reverse2(char s[])
{
    int c, i, j;
    for (i = 0, j = strlen(s)-1; i < j; i++, j--) {
        c = s[i],  s[i] = s[j], s[j] = c;
    }

}

2.4 sorting.c

Shell sort example

#include <stdio.h>

/* shellsort: sort v[0]...v[n-1] into increasing order  */
void shellsort(int v[], int n);

int main(int argc, char *argv[])
{
    int arr[10] = {6,4,8,8,1,2,3,2,22,6};
    int i;

    shellsort(arr, 10);

    for (i=0; i < 10; i++)
        printf ("%d\n", arr[i]);

    return 0;
}




void shellsort(int v[], int n)
{
    int gap, i, j, temp;

    for (gap = n/2; gap > 0; gap /= 2)
        for (i = gap; i < n; i++)
            for (j=i-gap; j>=0 && v[j] > v[j+gap]; j-=gap) {
                temp = v[j];
                v[j] = v[j+gap];
                v[j+gap] = temp;
            }
}

2.5 testgoto.c

Using Goto

#include <stdio.h>

int testgoto( int a[], int b[], int alength, int blength );

int main(int argc, char *argv[])
{

    int a[] = {1,5,7,9};
    int b[] = {6,6,8,10,11};

    int result = testgoto(a, b, 4, 5 );

    printf("result= %d", result);


    return 0;
}


int testgoto( int a[], int b[], int alength, int blength )
{
    int i; int j;
    for (i = 0; i < alength; i++)
        for ( j = 0; j < blength; j++)
            if (a[i] == b[j] )
                goto found;

    /* didn't find any common element   */
    return 0;

 found:
    return 1;


}

Author: Sebastian Emilio Narvaez

Created: 2019-10-12 Sat 22:16

Emacs 25.2.2 (Org mode 8.2.10)

Validate