:class: tip
This lecture will cover contents from Chapter 1 - Appendix version of the book.
```
:::{dropdown} How to learn C (now that you already know 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));
}
}
:::: :::::
:::::{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 :::
:::: :::::
molly. Refer to the Setup page if you need a refresher on how to do so.csc231 from inside your home directory. ~ sign.intro-c inside csc231, then change into that directory.
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 :::
intro-c, then use nano to create hello.cwith the source code below.
1
2
pwd
nano -l hello.c
nano: Ctrl-X (same for both Windows and Mac).Y to save modified buffer (new contents).Enter to save to the same file name as what you opened with.:::{image} fig/02-c/03.png :alt: Create hello.c using nano :class: bg-primary mb-1 :align: center :::
import.main: int main argc: number of command line arguments.*argv[]: pointers to array of command line argument strings.printf to print out string Hello world! with an end-of-line character \n. This is similar to System.out.printf.javac, we use gcc to compile C code.intro-c in the terminal.
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 :::
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 :::
hello.i: generated by pre-processorhello.s: generated by compiler.hello.o: generated by assembler.hello (or hello2): executable, generated by linker.:::{image} fig/02-c/06.png :alt: hello.c compilation process :class: bg-primary mb-1 :align: center :::
hello.i and hello.s, they can be view on the editor.hello.i
1
cat -n hello.i
hello.s
1
cat -n hello.s
hello.o and hello, we need to dump the binary contents first.
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 :::
hello.o.txt
1
cat -n hello.o.txt
hello.txt
1
cat -n hello.txt
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
:::
C formatting placeholders: (similar to Java)
%g: placeholder for a float (or double) value%d: placeholder for a decimal value (int, short, char)%s: placeholder for a string value%c: placeholder for a char value:::::{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) );
}
}
:::: :::::
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;
}
:::
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;
}
:::: :::::
::::{dropdown} Hands-on: Functions in C - definition and declaration
function-1.c, function-2.c, and function-3.c, with the source codes below::::{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 ::: ::::
intro-c, then use nano to create array-1.cwith the source code below.array-1.c :::::{dropdown} Exercise
array-1.c called array-2.c.numbers to double.::::{dropdown} Answer
:::: :::::
::::{dropdown} An array variable