# Introduction to C --- CS 130 // 2021-09-08 ## Administrivia - Finished grading assignment 1 + Decided not to do *forced* assigned seating + However, please consider sitting with the same table for the whole semester + Mask usage reminder - Quiz 1 will be on Monday + 10 minutes, paper quiz + Basics of C programming and CLI commands # Questions ## ...about anything? # Command Line Interfaces ## Command Line Interfaces - A *textual* interface to your computer - Each OS has their own version of a terminal - All commands are of the form: + `command arg1 arg2 ...` + `gcc hello.c` ## Basic Commands ```bash cd directory_name # change directory ls # list files/folders pwd # print working directory mkdir directory_name # make directory mv dir1 dir2 # move/rename directory mv file1 file2 # move/rename file rm file # remove file rm -r directory # remove directory (recursively) ``` # Introduction to C ## Exercise 1 - Write a program that asks the users to type in four numbers and prints the average of those four numbers ```c #include
int main() { int n1, n2, n3, n4; printf("Enter four numbers: "); scanf("%d %d %d %d", &n1, &n2, &n3, &n4); double avg = (n1 + n2 + n3 + n4) / 4.0; printf("The average is: %lf\n", avg); return 0; } ``` ## Exercise 2 - Create a function `void reverse(int arr[], int n)` that takes an array with `n` elements and reverses the order of the elements - Test it out in a `main` function such as: ```c #include
int main() { int arr[5] = {1, 2, 3, 4, 5}; reverse(arr, 5); for (int i = 0; i < 5; i++) { printf("The next value is: %d\n", arr[i]); } return 0; } ``` # Strings ## Strings - There is no **String** type in C - Strings are just arrays of type `char` ```c char str[20] = "abc"; printf("The string is %s", str); ``` - The above code is shorthand for the following: ```c char str[20]; str[0] = 'a'; str[1] = 'b'; str[2] = 'c'; str[3] = '\0'; // String terminating character ``` - The remaining 16 characters are unused ## Strings - Suppose we forget to include the `'\0'` character ```c char str[20]; str[0] = 'a'; str[1] = 'b'; str[2] = 'c'; printf("The string is %s", str); ``` - What do you think happens? - `printf` continues printing characters until it finds some random `'\0'` character in memory ## Strings - Very simple string operations now become complicated... - Suppose I want to concatenate two strings: ```c char s1[20] = "hello"; char s2[20] = "world"; char s3[20] = ??? // how do I concatenate s1 and s2? ``` - Manually copy all the characters into `s3`... ## Strings ```c char s1[20] = "hello"; char s2[20] = "world"; char s3[20]; int j = 0; // keeps track of the next spot in s3 for (int i = 0; s1[i] != '\0'; i++) { s3[j] = s1[i]; j++; } for (int i = 0; s2[i] != '\0'; i++) { s3[j] = s2[i]; j++; } s3[j] = '\0'; ``` ## Strings - Luckily, there is a `string.h` library with a few helpful functions that does some of this for us, but they just do the same thing as above ```c #include
char s1[20] = "hello"; char s2[20] = "world"; strcat(s1, s2); printf("The new string is: %s", s1); ``` ## Strings - Here is a common issue: ```c char s[3] = "abc"; int x = 10; printf("The values are %s and %d", s, x); ``` - We did not allocate enough space in `s` to store the string termination character `'\0'` ```c char s[4] = "abc"; int x = 10; printf("The values are %s and %d", s, x); ``` ## Pointers - One of the most powerful features of C is declaring "pointers" to values - A **pointer** is a variable whose value is the address of a memory location - To declare a variable as a pointer, you use a `*` before the variable name: ```c int *p; // p contains an address to an int ``` ## Pointers - To get the memory address of a variable, we use `&`, the address operator ```c int x = 5; printf("Address of x is: %x", &x); int *p = &x; // p is pointing to x's memory location ``` - To access the value a pointer is "pointing to", you can **dereference** it using the `*` operator ```c int y = *p; // gets the int p is pointing to printf("p was pointing to %d", y); ``` ## Arrays are Pointers - In C, arrays are just **pointers** ```c int arr[10]; // arr is a pointer to the address of the // first element of the array ``` - You can use array notation on pointers and pointer notation on arrays ```c int *p = arr; // p points to what arr is pointing to p[0] = 100; // changes arr[0] to be 100 int x = *arr; // gets 100 back ``` ## Exercise 3 - Suppose we want to write a function that computes the average of a bunch of `double` values - What would the prototype of this function be? ```c double average(double arr[], int n) ``` - Finish writing the function # `printf` # `scanf` ## Exercise 4 - Write a program that asks the user to enter in 10 doubles, computes the average, and prints it off - Use an array and a loop to get input from the user - Use the `average` function to compute the average ## Exercise 5 - How can we modify this program so that the user can enter **any** number of inputs?