C Basics

An Introduction to C programming language

18 February 2020

Obed N Munoz

Cloud Software Engineer

Hello world

#include <stdio.h>

main()
{
    printf("Hello world \n");
}
2

GCC Compiler

The GNU Compiler with support for C, C++, Objective-C, Fortran, Ada, Go and D.

Compiling your first program:

cc hello.c -o hello.o
3

Custom Hello world

#include <stdio.h>

int main(int argc, char **argv)
{
    printf("Hello  %s\n", argv[1]);
}

Let's run it:

cc hello.c -o hello.o
./hello.o Obed
Hello Obed
4

A bit more complex example

#include <stdio.h>

#define   LOWER  0       /* lower limit of table */
#define   UPPER  300     /* upper limit */
#define   STEP   20      /* step size */

/* print Fahrenheit-Celsius table */

int main()
{
    int fahr;

    for (fahr = LOWER; fahr <= UPPER; fahr = fahr + STEP)
    printf("Fahrenheit: %3d, Celcius: %6.1f\n", fahr, (5.0/9.0)*(fahr-32));

    return 0;
}
5

Let's Code: Fahrenheit to Celsius

# Example
./fahrenhet_celsius 32
Fahrenheit: 32, Celsius: 0

classify.obedmr.com/get-lab/ap-labs/c-basics

6

Simple Input and Output 1/2

#include <stdio.h>

#define IN   1   /* inside a word */
#define OUT  0   /* outside a word */
7

Simple Input and Output 2/2

int main()

{
    int c, nl, nw, nc, state;
    state = OUT;

    nl = nw = nc = 0;

    while ((c = getchar()) != EOF) {
    ++nc;
    if (c == '\n')
        ++nl;

    if (c == ' ' || c == '\n' || c == '\t')
        state = OUT;
    else if (state == OUT) {
        state = IN;
        ++nw;
    }
    }

    printf("Lines: %d,  Words: %d, Chars: %d\n", nl, nw, nc);
    return 0;
}
8

Arrays 1/3 - fixed size

#define ARRAY_LENGHT 10

int main() {

    int i, tmp, arr[ARRAY_LENGHT];
    srand(time(NULL));

    printf("Initial array: \n");
    for (i = 0;  i < ARRAY_LENGHT; i++) {
        arr[i] = rand() % 100;
        printf("%d " , arr[i]);
    }

    for (i = 0;  i < ARRAY_LENGHT/2; i++) {
        tmp = arr[i];
        arr[i] = arr[ARRAY_LENGHT - i - 1];
        arr[ARRAY_LENGHT - i - 1] = tmp;
    }
}
9

Arrays 2/3 - as argument

void reverse(int arr[ARRAY_LENGHT]) {

    int i, tmp;

    for (i = 0;  i < ARRAY_LENGHT/2; i++) {
        tmp = arr[i];
        arr[i] = arr[ARRAY_LENGHT - i - 1];
        arr[ARRAY_LENGHT - i - 1] = tmp;
    }
}

int main() {
    ...
    reverse(arr);
    ...
}
10

Arrays 3/3 - dynamic size

void reverse(int lenght, int arr[]) {

    int i, tmp;

    for (i = 0;  i < lenght/2; i++) {
        tmp = arr[i];
        arr[i] = arr[lenght - i - 1];
        arr[lenght - i - 1] = tmp;
    }
}

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

    int i, lenght, *arr;
    ...
    arr = malloc(sizeof(int) * lenght);
    ...
    reverse(lenght, arr);
    ...
}
11

Let's Code: Word reverser

Follow instructions from:

classify.obedmr.com/get-lab/ap-labs/word-reverser

12

Resources

This material is genereated thanks to some extracts of the following book:

13

Thank you

Obed N Munoz

Cloud Software Engineer

Use the left and right arrow keys or click the left and right edges of the page to navigate between slides.
(Press 'H' or navigate to hide this message.)