Week 2 - Friday. What did we talk about last time? Base systems C literals Representations in...
-
Upload
alexander-warren -
Category
Documents
-
view
212 -
download
0
Transcript of Week 2 - Friday. What did we talk about last time? Base systems C literals Representations in...
![Page 1: Week 2 - Friday. What did we talk about last time? Base systems C literals Representations in memory.](https://reader030.fdocuments.us/reader030/viewer/2022032805/56649efa5503460f94c0ba6e/html5/thumbnails/1.jpg)
CS222Week 2 - Friday
![Page 2: Week 2 - Friday. What did we talk about last time? Base systems C literals Representations in memory.](https://reader030.fdocuments.us/reader030/viewer/2022032805/56649efa5503460f94c0ba6e/html5/thumbnails/2.jpg)
Last time
What did we talk about last time? Base systems C literals Representations in memory
![Page 3: Week 2 - Friday. What did we talk about last time? Base systems C literals Representations in memory.](https://reader030.fdocuments.us/reader030/viewer/2022032805/56649efa5503460f94c0ba6e/html5/thumbnails/3.jpg)
Questions?
![Page 4: Week 2 - Friday. What did we talk about last time? Base systems C literals Representations in memory.](https://reader030.fdocuments.us/reader030/viewer/2022032805/56649efa5503460f94c0ba6e/html5/thumbnails/4.jpg)
Project 1
![Page 5: Week 2 - Friday. What did we talk about last time? Base systems C literals Representations in memory.](https://reader030.fdocuments.us/reader030/viewer/2022032805/56649efa5503460f94c0ba6e/html5/thumbnails/5.jpg)
Quotes
It ain't what you don't know that gets you into trouble. It's what you know for sure that just ain't so.
Mark Twain
![Page 6: Week 2 - Friday. What did we talk about last time? Base systems C literals Representations in memory.](https://reader030.fdocuments.us/reader030/viewer/2022032805/56649efa5503460f94c0ba6e/html5/thumbnails/6.jpg)
Math libraryFunction Result Function Result
cos(double theta) Cosine of theta exp(double x) ex
sin(double theta) Sine of theta log(double x) Natural logarithm of x
tan(double theta) Tangent of theta
log10(double x) Common logarithm of x
acos(double x) Arc cosine of xpow(double base, double exponent)
Raise base to power exponent
asin(double x) Arc sine of x sqrt(double x) Square root of x
atan(double x) Arc tangent of x ceil(double x) Round up value of x
atan2(double y, double x)
Arc tangent of y/x
floor(double x) Round down value of x
fabs(double x) Absolute value of x
fmod(double value, double divisor)
Remainder of dividing value by divisor
![Page 7: Week 2 - Friday. What did we talk about last time? Base systems C literals Representations in memory.](https://reader030.fdocuments.us/reader030/viewer/2022032805/56649efa5503460f94c0ba6e/html5/thumbnails/7.jpg)
Math library in action
You must #include <math.h> to use math functions
#include <math.h>#include <stdio.h>
int main(){
double a = 3.0;double b = 4.0;double c = sqrt(a*a + b*b);printf("Hypotenuse: %f\n", c); return 0;
}
![Page 8: Week 2 - Friday. What did we talk about last time? Base systems C literals Representations in memory.](https://reader030.fdocuments.us/reader030/viewer/2022032805/56649efa5503460f94c0ba6e/html5/thumbnails/8.jpg)
It doesn't work!
Just using #include gives the headers for math functions, not the actual code
You must link the math library with flag –lm
Now, how are you supposed to know that?
> gcc -lm hypotenuse.c -o hypotenuse
> man 3 sqrt
![Page 9: Week 2 - Friday. What did we talk about last time? Base systems C literals Representations in memory.](https://reader030.fdocuments.us/reader030/viewer/2022032805/56649efa5503460f94c0ba6e/html5/thumbnails/9.jpg)
My main man
Man (manual) pages give you more information about commands and functions, in 8 areas:1. General commands2. System calls3. Library functions (C library, especially)4. Special files and devices5. File formats6. Miscellaneous stuff7. System administration
Try by typing man topic for something you're interested in If it lists topics in different sections, specify the section
For more information:
> man 3 sqrt
> man man
![Page 10: Week 2 - Friday. What did we talk about last time? Base systems C literals Representations in memory.](https://reader030.fdocuments.us/reader030/viewer/2022032805/56649efa5503460f94c0ba6e/html5/thumbnails/10.jpg)
Single Character I/O
![Page 11: Week 2 - Friday. What did we talk about last time? Base systems C literals Representations in memory.](https://reader030.fdocuments.us/reader030/viewer/2022032805/56649efa5503460f94c0ba6e/html5/thumbnails/11.jpg)
getchar()
We haven't talked about any input in C yet To read the next character from input, you can
use the getchar() function It will return the value of the next character (as
an int) or -1 if the end of the file is reached Store the value as an int first to check to see if the
end of the file has been reached If not, you can then store it as a char
int value = getchar();if( value == -1 )
printf("End of file!");
![Page 12: Week 2 - Friday. What did we talk about last time? Base systems C literals Representations in memory.](https://reader030.fdocuments.us/reader030/viewer/2022032805/56649efa5503460f94c0ba6e/html5/thumbnails/12.jpg)
putchar()
putchar() is the output equivalent of getchar()
It outputs a single character at a time You could use printf() with the %c
formatter instead, but putchar() can be more convenient for single characters
char letter = 's';putchar('q');putchar(letter);
![Page 13: Week 2 - Friday. What did we talk about last time? Base systems C literals Representations in memory.](https://reader030.fdocuments.us/reader030/viewer/2022032805/56649efa5503460f94c0ba6e/html5/thumbnails/13.jpg)
Preprocessor Directives
![Page 14: Week 2 - Friday. What did we talk about last time? Base systems C literals Representations in memory.](https://reader030.fdocuments.us/reader030/viewer/2022032805/56649efa5503460f94c0ba6e/html5/thumbnails/14.jpg)
Preprocessor directives
There are preprocessor directives which are technically not part of the C language
These are processed before the real C compiler becomes involved
The most important of these are #include #define Conditional compilation directives
![Page 15: Week 2 - Friday. What did we talk about last time? Base systems C literals Representations in memory.](https://reader030.fdocuments.us/reader030/viewer/2022032805/56649efa5503460f94c0ba6e/html5/thumbnails/15.jpg)
#include
You have already used #include before #include <stdio.h>
It can be used to include any other file Use angle brackets (< >) for standard libraries Use quotes (" ") for anything else
It literally pastes the file into the document where the #include directive is
Never #include .c files (executable code), only .h files (definitions and prototypes)
It is possible to have a circular include problem
![Page 16: Week 2 - Friday. What did we talk about last time? Base systems C literals Representations in memory.](https://reader030.fdocuments.us/reader030/viewer/2022032805/56649efa5503460f94c0ba6e/html5/thumbnails/16.jpg)
#define
The primary way to specify constants in C is with a #define
When you #define something, the preprocessor does a find and replace Don't use a semicolon!
#define directives are usually put close to the top of a file, for easy visibility
#define SIZE 100
int main(){
int array[SIZE];int i = 0;for( i = 0; i < SIZE; i++ )
array[i] = i*i;
return 0;}
![Page 17: Week 2 - Friday. What did we talk about last time? Base systems C literals Representations in memory.](https://reader030.fdocuments.us/reader030/viewer/2022032805/56649efa5503460f94c0ba6e/html5/thumbnails/17.jpg)
#define macros
You can also make macros with #define that take arguments
You need to be careful with parentheses Constants and macros are usually written in
ALL CAPS to avoid confusion
#include <math.h>#define TO_DEGREES(x) ((x) * 57.29578)#define ADD(a,b) ((a) + (b))
int main(){double theta = TO_DEGREES(2*M_PI);int value = ADD(5 * 2, 7);
return 0;}
![Page 18: Week 2 - Friday. What did we talk about last time? Base systems C literals Representations in memory.](https://reader030.fdocuments.us/reader030/viewer/2022032805/56649efa5503460f94c0ba6e/html5/thumbnails/18.jpg)
Conditional compilation
You can use directives #if, #ifdef, #ifndef, #else, #elif and #endif
These are mostly used to avoid infinite include problems
Sometimes they will change what gets compiled based on compiler version, system libraries, or other stuff
#ifndef SOMETHING_H#define SOMETHING_H
int something(int a, int b);#endif
![Page 19: Week 2 - Friday. What did we talk about last time? Base systems C literals Representations in memory.](https://reader030.fdocuments.us/reader030/viewer/2022032805/56649efa5503460f94c0ba6e/html5/thumbnails/19.jpg)
Lab 2
![Page 20: Week 2 - Friday. What did we talk about last time? Base systems C literals Representations in memory.](https://reader030.fdocuments.us/reader030/viewer/2022032805/56649efa5503460f94c0ba6e/html5/thumbnails/20.jpg)
Upcoming
![Page 21: Week 2 - Friday. What did we talk about last time? Base systems C literals Representations in memory.](https://reader030.fdocuments.us/reader030/viewer/2022032805/56649efa5503460f94c0ba6e/html5/thumbnails/21.jpg)
Next time…
System limitsconst Bitwise operators Operator precedence
![Page 22: Week 2 - Friday. What did we talk about last time? Base systems C literals Representations in memory.](https://reader030.fdocuments.us/reader030/viewer/2022032805/56649efa5503460f94c0ba6e/html5/thumbnails/22.jpg)
Reminders
Keep reading K&R chapter 2