Introduction to C

:class: tip

This lecture will cover contents from Chapter 1 - Appendix version of the book.

```

What is C?

Overview

:::{dropdown} How to learn C (now that you already know Java)?

:::

Scary stuff ahead …

But it is exciting …

C and Java

:::::{tab-set} ::::{tab-item} hello.c

1
2
3
4
5
6
7
8
9
#include <math.h>
#include <stdio.h>

/* main function definition: */
int main() {
    printf("Hello World\n");
    printf("sqrt(4) is %f\n", sqrt(4));
    return 0;  // main returns value 0
}

::::

::::{tab-item} hello.java

1
2
3
4
5
6
7
8
9
10
11
import java.lang.Math;

/* define a HelloWorld class */
class Hello {

  /* main method definition: */
  public static void main(String[] args){
    System.out.println("Hello World");
    System.out.println("sqrt(4) is " + Math.sqrt(4));
  }
}

:::: :::::

Similarities

Differences

:::::{tab-set} ::::{tab-item} Running Java

:::{image} fig/02-c/java.png :alt: Java compilation and execution :class: bg-primary mb-1 :align: center :::

:::: ::::{tab-item} Running C

:::{image} fig/02-c/c.png :alt: C compilation and execution :class: bg-primary mb-1 :align: center :::

:::: :::::

Hands-on: Getting started

1
2
3
4
5
6
cd ~/csc231
pwd
mkdir intro-c
ls
cd intro-c
pwd

:::{image} fig/02-c/02.png :alt: create intro-c directory :class: bg-primary mb-1 :align: center :::

Create hello.c

1
2
pwd
nano -l hello.c

:::{image} fig/02-c/03.png :alt: Create hello.c using nano :class: bg-primary mb-1 :align: center :::

What’s in the code?

Hands-on: Simple compile and run

1
2
3
4
5
ls
pwd
gcc -o hello hello.c
ls
./hello

:::{image} fig/02-c/04.png :alt: Creating hello.c using nano :class: bg-primary mb-1 :align: center :::

Hands-on: Compile and show everything

1
2
3
4
ls -l
gcc -save-temps -o hello2 hello.c
ls -l
./hello2

:::{image} fig/02-c/05.png :alt: compile and run hello.c, keeping intermediate files :class: bg-primary mb-1 :align: center :::

What are those?

:::{image} fig/02-c/06.png :alt: hello.c compilation process :class: bg-primary mb-1 :align: center :::

Hands-on: View files

1
cat -n hello.i
1
cat -n hello.s
1
2
3
xxd -b hello.o hello.o.txt
xxd -b hello hello.txt
ls -l

:::{image} fig/02-c/07.png :alt: hexdumping hello.c and hello :class: bg-primary mb-1 :align: center :::

1
cat -n hello.o.txt
1
cat -n hello.txt

Challenge: output

The usage of C’s printf is similar to Java’s System.out.printf. Find out how to modify hello.c so that the program prints out Hello Golden Rams! with each word on a single line. The program should use exactly one printf statement.

:::{dropdown} Answer

:::

Input: scanf

C formatting placeholders: (similar to Java)

:::::{tab-set} ::::{tab-item} readInput.c

1
2
3
4
5
6
7
8
9
10
11
#include <stdio.h>

int main() {
  int num1, num2;
  printf("Enter a number: ");
  scanf("%d", &num1);
  printf("Enter another: ");
  scanf("%d", &num2);
  printf("%d + %d = %d\n", num1, num2, (num1+num2) );
  return 0;
}

::::

::::{tab-item} InputReader.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
import java.util.Scanner;

class InputReader {
  public static void main(String[] args) {
    int num1, num2;
    Scanner in = new Scanner(System.in);
    System.out.print("Enter a number: ");
    num1 = in.nextInt();
    System.out.print("Enter another: ");
    num2 = in.nextInt();
    System.out.printf( "%d + %d = %d\n",
          num1, num2, (num1+num2) );
  }
}

:::: :::::

Challenge

Create a file called mixedInput.c that is modified from readInput.c. The file should do the followings:

:::{dropdown} Answer

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <stdio.h>

int main() {
  float num1, num2, num3;
  printf("Enter a number: ");
  scanf("%g", &num1);
  printf("Enter another: ");
  scanf("%g", &num2);
  printf("Enter another: ");
  scanf("%g", &num3);
  printf("%g * %g * %g = %g\n", num1, num2, num3, (num1*num2*num3) );
  return 0;
}

:::

Conditionals and Loops

Similar to Java.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <stdio.h>

int main(void) {
    int num1, num2;

    printf("Enter the 1st number: ");
    scanf("%d", &num1);
    printf("Enter the 2nd number: ");
    scanf("%d", &num2);

    if (num1 > num2) {
        printf("%d is biggest\n", num1);
    } else if (num1 == num2) {
        printf("%d and %d are the same\n", num1, num2);
    } else {
        printf("%d is biggest\n", num2);
    }

    return 0;
}

:::::{tab-set} ::::{tab-item} forLoop.c

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <stdio.h>

int main(void) {
    int num, i;

    printf("Enter a value: ");
    scanf("%d", &num);
    // make sure num is not negative
    if (num < 0) {
        num = -num;
    }

    for (i = 0; i < num; i++) {
        printf("%d\n", i);
    }

    return 0;
}

::::

::::{tab-item} whileLoop.c

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <stdio.h>

int main(void) {
    int num, val;

    printf("Enter a value: ");
    scanf("%d", &num);
    // make sure num is not negative
    if (num < 0) {
        num = -num;
    }
    val = 1;
    while (val < num) {
        printf("%d\n", val);
        val = val * 2;
    }

    return 0;
}

:::: :::::

Function in C

::::{dropdown} Hands-on: Functions in C - definition and declaration

:::{image} fig/02-c/23.png :alt: Compile and run function-1.c, function-2.c, function-3.c :class: bg-primary mb-1 :height: 250px :align: center ::: ::::

Arrays

:::::{dropdown} Exercise

::::{dropdown} Answer

:::: :::::

::::{dropdown} An array variable