Skip to content

Commit 2b3e728

Browse files
committed
Sync with Spring 2020 class & video examples.
1 parent 801545a commit 2b3e728

69 files changed

Lines changed: 3050 additions & 103 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

c/README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# C
2+
Each directory contains multiple C files.
3+
4+
## **c0** - An introduction to C, printf/scanf, contruld structures, & datatypes
5+
6+
## **c1** - Arrays, functions, strings, structs, unions
7+
8+
## **c2** - Pointers
9+
10+
## **c3** - Header files, constants, recursion

c/c0/README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
### hello.c
2+
*A hello world example.*
3+
The #include statement brings in the forward declaration for the printf function.
4+
5+
### c_overview.c
6+
*Example of several different C features*
7+
Video - https://youtu.be/uUtQIEIxvWY
8+
This file goes over many of the features of C you will need to know, including data types, printf, structures, calling functions, and arrays.
9+
10+
### inputoutput.c
11+
*Getting user input in C using scanf.*
12+
Note that scanf is not the best way to do it, but will suffice at first. The scanf function takes a pointer to a variable as its parameter so we need to pass the address (&) of the variable. We will go into more detail later in the course.
13+
14+
### control.c
15+
*Control structures in C such as loops, conditional statements, and controling loop execution.*
16+
The syntax in this file should look familiar to you since, other than the input and output statements, almost every line of code is valid Java syntax. The scanf function gets input and stores it at the address given - the & operator gets the address of a variable. We will discuss that operator more when we discuss pointers.
17+
18+
### datatypes.c
19+
*Examples of different data types built in to C and how we can work with them.*
20+
Multiple examples of declaring and printing different data types. The variable declarations have multiple examples of different types of literals (a token that represents a fixed value). The printf statement allows you to include values in the output string through the use of control sequences. The control sequences are replaced by the expression(s) that follows the output string. Note that whatever expression you try to print will be implicitly converted to the type declared in the control sequence. That can cause confusion when you print a negative number using the %u control sequence, so if you get unexpected output always check that you are using the correct control sequence for the variable. Additional control sequences are listed in the C notes.
21+
22+

c/c0/c_overview.c

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#include<stdio.h>
2+
#include<string.h>
3+
4+
/*
5+
* Data types
6+
* Arrays
7+
* User-defined types (structs)
8+
* Functions
9+
*/
10+
11+
/* Forward declarations */
12+
void print_num(int num);
13+
int add_numbers(int a, int b);
14+
15+
struct simple_struct {
16+
int num;
17+
char name[10];
18+
};
19+
20+
int main(void) {
21+
int x = 10;
22+
char c = 'K';
23+
int nums[5];
24+
int vals[] = {1,2,3,4};
25+
struct simple_struct thing = {25, "Arizona"};
26+
27+
char hello[] = {'H', 'e', 'l', 'l', 'o', '\0'}; /* Null terminated */
28+
char world[] = "world";
29+
30+
printf("Hello, world\n");
31+
32+
printf("x=%d, c=%c\n", x, c);
33+
printf("x=%x, c=%d\n", x, c);
34+
printf("hello=%s, sizeof hello = %lu\n", hello, sizeof(hello));
35+
printf("world=%s, sizeof world = %lu\n", world, sizeof(world));
36+
37+
printf("thing: num=%d, name=%s\n", thing.num, thing.name);
38+
thing.num = thing.num * 10;
39+
thing.name[4] = 'X';
40+
printf("thing: num=%d, name=%s\n", thing.num, thing.name);
41+
thing.num = 2020;
42+
/* thing.name = "Texas"; // You can't do this! */
43+
strncpy(thing.name, "Texas", 6);
44+
printf("thing: num=%d, name=%s\n", thing.num, thing.name);
45+
46+
print_num(add_numbers(thing.num, 6));
47+
nums[0] = add_numbers(vals[2], 10);
48+
print_num(nums[0]);
49+
50+
}
51+
52+
void print_num(int num) {
53+
printf("The number is %d\n", num);
54+
}
55+
56+
int add_numbers(int a, int b) {
57+
return a + b;
58+
}
59+

c/c0/control.c

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/* C Control Structure examples */
2+
#include <stdio.h>
3+
4+
int main (void)
5+
{
6+
int a = 5;
7+
int b, ii;
8+
9+
printf ("Enter an integer : ");
10+
scanf ("%d", &b);
11+
12+
printf ("\nIf-then-else statment : ");
13+
if (a < b)
14+
printf("%d is less than %d\n", a, b);
15+
else
16+
printf("%d is not less than %d\n", a, b);
17+
18+
printf ("\nSwitch statement : ");
19+
switch (b)
20+
{
21+
case 1 :
22+
printf("You entered a one.\n");
23+
break; /* Without the break execution will continue to the next line */
24+
case 2 :
25+
printf("You entered a two.\n");
26+
break;
27+
case 3 :
28+
printf("You entered a three.\n");
29+
break;
30+
case 4 :
31+
printf("You entered a four\n");
32+
break;
33+
default : /* The default case is used when no other cases match */
34+
printf("You entered a number greater than 4 or less than 1\n");
35+
}
36+
37+
printf ("\nWhile loop : \n");
38+
ii = 0;
39+
while (ii <= b)
40+
{
41+
printf (" Your number = %d, loop variable = %d\n", b, ii);
42+
ii = ii + 2;
43+
}
44+
45+
/* The body of a do-while loop is always executed at least once */
46+
printf ("\nDo-while loop : \n");
47+
do {
48+
printf (" Your number = %d, loop variable = %d\n", b, ii);
49+
ii -= 2;
50+
} while ( (ii > 0) );
51+
52+
53+
/* Note: in C the loop variable must be declared before the loop */
54+
printf ("\nFor loop : \n");
55+
for (ii=0;ii<b;ii++) {
56+
printf (" loop variable = %d\n", ii);
57+
}
58+
59+
/* The break statement exists the current loop */
60+
printf ("\nBreak example : \n");
61+
for(;;) {
62+
/* If a is bigger than 100 quit the loop */
63+
if (a > 100) {
64+
printf (" Breaking . . \n");
65+
break;
66+
}
67+
else {
68+
a++;
69+
}
70+
}
71+
72+
/* The continue statment skips the rest of the current iteration */
73+
printf ("\nContinue example : \n");
74+
for (ii=0;ii<=b;ii++) {
75+
/* If ii is not a multiple of 3 then go to next loop iteration */
76+
if ( (ii % 3) != 0) {
77+
continue;
78+
}
79+
printf (" %d", ii);
80+
}
81+
printf ("\n");
82+
83+
return 0;
84+
}
85+

c/c0/datatypes.c

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/* C data type examples */
2+
#include<stdio.h>
3+
4+
int main(void) {
5+
int x = 59; /* Integer literal */
6+
int y = 0x0A; /* Hexidecimal literal */
7+
int z;
8+
unsigned int a = -40; /* What happens here? */
9+
char c = 'A'; /* Character literal */
10+
char k = 66;
11+
char s[] = "Hello!"; /* String literal */
12+
float f = 10.0 / 3.0; /* Float literals */
13+
14+
15+
/* To print variables in C, you use a control sequence to represent the
16+
variable's place in the string, then include a list of the variables
17+
you want to print after the string. */
18+
/* Examples of printf control sequences
19+
%d - integer
20+
%x - hexidecimal
21+
%u - unsigned
22+
%c - character
23+
%f - floating point
24+
%s - string
25+
*/
26+
printf("x = %d (%x hex)\n", x, x);
27+
printf("y = %x (%d dec)\n", y, y);
28+
printf("a = %u (%d int) (%x hex)\n", a, a, a);
29+
printf("c = %c (%d int) (%x hex)\n", c, c, c);
30+
printf("k = %c (%d int) (%x hex)\n", k, k, k);
31+
printf("f = %f (%.2f formatted)\n", f, f);
32+
printf("s= %s\n", s);
33+
/* Note that z has not been initialized. What happens if we
34+
* uncomment out the next line? */
35+
/*
36+
printf("z = %d (%x hex)\n", z, z);
37+
*/
38+
z = x + y;
39+
printf("z = %d (%x hex)\n", z, z);
40+
41+
/* int vs char */
42+
printf("\nHex: 100 - A = %d\n", 100-0xA);
43+
printf("Char: 100 - A = %d\n", 100-'A');
44+
for(z=0;z<26;z++) {
45+
/* We can add an integer to a character literal */
46+
printf("%c ", 'A' - z);
47+
}
48+
printf("\n");
49+
50+
return 0;
51+
}

c/c0/hello.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/* C Hello World example */
2+
#include<stdio.h>
3+
4+
int main(void) {
5+
printf("Hello, world!\n");
6+
return 0;
7+
}

c/c0/inputoutput.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/* C input output */
2+
#include<stdio.h>
3+
4+
int main(void) {
5+
int b;
6+
printf ("Enter an integer : ");
7+
scanf ("%d", &b);
8+
printf("Here is the integer you typed : %d \n", b);
9+
return 0;
10+
}

c/c1/README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
2+
### arrays.c
3+
*Declaring and initialiing arrays. Array sizes. sizeof.*
4+
The important thing to keep in mind about C arrays is that C does not perform any bounds checking. So you can use an index that is larger than the size of the array! What happens in that case? In C the size of the array is the number of bytes allocated for the array, not the number of elements in the array. So to determine the number of elements in an array you have to divide its size by the size of the type of the element it stores. One caveat to keep in mind - even the size of the array is lost when you pass the array to a function. So if you want to write a function that takes an array as a parameter you should include a length parameter as well.
5+
6+
### strings.c
7+
*String handling in C. Initializing strings vs character arrays.*
8+
C does not have a string type. Strings are represented using null-terminated character arrays. So if you have a valid string (character array) and you change the element where the null-terminator - '\0' - is stored then you no longer have a valid string. When you perform an operation on a string, including printf, the operation continues until a null-terminator is reached. This can be a problem if the string is not valid, so when writing functions that work on strings you should always include the length as a parameter.
9+
10+
### functions.c
11+
*Defining & calling functions in C. Passing paremeters by pointer (reference). Forward declarations.*
12+
In C, the compiler has to know the function signature before it is called in the code. One way to do this is to include a forward declaration, which is just the function signature followed by a semi-colon. In cases where the parameter is intended to be changed in the function we use pass-by-address: the parameter is preceded by * in the declaration (formal parameter) and when it is called (actual parameter) it is preceded by a &. If an array is passed as a parameter then the formal parameter does not need to include the length of the array as that is not passed to the function.
13+
14+
### countspaces.c
15+
*Calling a function with an array pointer. Getting string input from user using fgets. Symbolic constants.*
16+
We have to pass the length of the array to the function so the loop will know when to stop. The fgets() function has 3 parameters - the array you want to fill with the user input, the length of the array, and the file to read from. The identifier stdin refers to standard input (stdout is output and stderr is for errors).
17+
18+
### usertypes.c
19+
*User-defined types in C, including enums, structs, and unions. typedef.*
20+
21+
### stuructsize.c
22+
*Size of structures with similar fields. Bitfields.*
23+

c/c1/arrays.c

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#include<stdio.h>
2+
3+
int main(void) {
4+
5+
/* Initializing array declarations */
6+
int a[4] = {1, 2, 3, 4};
7+
double b[] = {2.4, 3.14, 9.9999}; /* b is the size of the initializer */
8+
/* What happens if we declare an array with a size smaller than the initializer? */
9+
10+
unsigned int ii;
11+
12+
/* This is undefined behavior! */
13+
for (ii=0;ii<6;ii++) {
14+
printf("a[%d]=%d\t\tb[%d]=%.2f\n", ii, a[ii], ii, b[ii]);
15+
}
16+
17+
printf("\n");
18+
19+
/* Are these sizes what you expect? */
20+
printf("Size of int=%lu\n", sizeof(int));
21+
printf("Size of a=%lu\n", sizeof(a));
22+
printf("Size of double=%lu\n", sizeof(double));
23+
printf("Size of b=%lu\n", sizeof(b));
24+
printf("\n");
25+
26+
27+
a[2] = 20;
28+
a[3] = 200;
29+
/* what does (sizeof(a)/sizeof(int)) do? */
30+
for(ii=0;ii<(sizeof(a)/sizeof(int));ii++) {
31+
printf("a[%d]=%d (%x)\n", ii, a[ii], a[ii]);
32+
}
33+
printf("\n");
34+
35+
return 0;
36+
}

c/c1/countspaces.c

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#include <stdio.h>
2+
3+
/* Symbolic constant */
4+
#define LEN 100
5+
6+
int count_spaces(char[], int);
7+
8+
int main(void) {
9+
char str[LEN];
10+
11+
printf("Enter a string: ");
12+
fgets(str, LEN, stdin); /* Get a string from input, store it in str */
13+
14+
printf("Your string is: %s", str);
15+
printf("Your string has %d spaces\n", count_spaces(str, LEN));
16+
17+
return 0;
18+
}
19+
20+
/* Count the number of spaces in a string */
21+
int count_spaces(char str[], int len) {
22+
int ii=0;
23+
int count = 0;
24+
25+
/* The size of the string is unknown here, so we look for a null-terminator */
26+
while ( (ii < len) && ( str[ii] != '\0') ) {
27+
if (str[ii] == ' ') {
28+
count ++;
29+
}
30+
ii++;
31+
}
32+
return count;
33+
}

0 commit comments

Comments
 (0)