CMPS 12B Midterm: C Crash Course

9 Pages
Unlock Document

University of California - Santa Cruz
Computer Science
Darrell Long

CMPS 12B Crash Course 4/15/2017 (1:00-4:00) Taught By… Yash Daniel Goals for Session  Main() function  Variables and Arrays  Control flow  Functions  Pointers – Important  Command arguments  Dynamic memory allocation  Boolean Algebra o Bitwise “and, or, are” C Basics # , hash include  Use libraries  Libraries define functions for you so you don’t have to rewrite the code  stdio = standard input output Main() Function  Why int? o returns -1 and 0  -1 or any other number = failure  0 = success  Why?  Convention: just what we decided  Returning a non-zero  all it means is that the function failed/is false, not that it’s a bad thing.  You don’t have to return 0 for it to be success, but again, convention o Void would return nothing, and we wouldn’t be able to tell if the function works  If your function doesn’t take any arguments, you need to put void (otherwise you lose style points) o Ex: int foo() = bad style  main() is the starting point in the program (where program starts), you must have one, you only need one.  { } = scope of function printf()  printf(“format string”, variables);  printf = print format  In C, you can’t add strings like you do in Java; you use qualifiers o Ex: int a = 5; printf(“Shawn has %d apples”, a);  List of terms: o %d  digits, integer o %c  character o %s  C-style String o %f  Float  Using multiple variables o Ex: printf(“%s has %d apples”, “Shawn”, 10);  Doesn’t need to be stored in a variable (unless you want it scanf()  Mostly the same as prinf(), except the variable you pass to it uses an ampersand (&) o Ex: scanf(“%d %d”, &a, &b);  If you pass a file to scanf… o Ex: scanf(“%s %d”, buffer, &numApples);  Input to string, you don’t need the ampersand o Ex: scanf(“enter seed: %d”, &a);  wrong, since then the program expects you to type “enter seed: number” every time in order for you to work Variables and Arrays How do you define an integer?  Ex: int a;  You don’t want your variables called a or anything non-descriptive o In Python… a = s; you don’t need types, so it’d be impossible to understand what’s happening if you go back to the code o So instead write a to be like: numApples o Or write a comment: // = single line comments  Ex: //holds the number of apples for Shawn  These comments don’t do anything for the compiler, but it helps others reading the code  Comments are important in your code o Helps graders trust you more Float a  Both int and float are 32 bits, but… o Int can only do whole numbers o Float can do decimal numbers  Ex: 1.1 o Ex: int b; b = 5; b/3 = 1  it’s one because the int forces it to truncate the .667; takes the floor of the decimal. o Ex: float b; b = 5; b/3 = 1.66  Trick: Ex: a = 5.471 printf(“%.2f”, a); Answer: 5.47  % means modulo, which gives you remainder o Ex: x%3 = y  y < 3  you do this to limit the range of a variable/number  float a % # doesn’t make any sense because the remainder will always be zero since it deals with decimal numbers.  Ex: int a; //%d float b; //%f char c;  //%c  holds single character c = ‘a’;  use single quotes printf(“%c”, c); //a unsigned int a; //%u  32 bits, 4 bytes, max value of 8-bit number = 2^8 = 256, so 32-bit means your int value can go from -2,000,000,000  2,000,000,000. To make it 0  4,000,000,000, make the variable unsigned.  You have to specify if you want a variable to be unsigned  long int;  make 64-bit number  if you want a really long number Int Arrays in C  In Java: int[] A = {1…n};  In C: int A[5];  20 bytes int B; o To access first four bytes, A[0], access index of 0. o A[0] = 10; reserve first 4 bytes of array for 10. o If you reserve 20 bytes, it’d be 0-19, increments of 4 for each block of memory o A + 1 in memory = A + (1*sizeof(int)) = A + 4 bytes  Why is this a thing?  Say you take an address of a location, it doesn’t describe it. Address does not describe what is inside the memory.  A+1 = 3, A+2 = 7, A+3 = 11, A+5 = something; it exists, but you didn’t allocate your memory
More Less

Related notes for CMPS 12B

Log In


Don't have an account?

Join OneClass

Access over 10 million pages of study
documents for 1.3 million courses.

Sign up

Join to view


By registering, I agree to the Terms and Privacy Policies
Already have an account?
Just a few more details

So we can recommend you notes for your school.

Reset Password

Please enter below the email address you registered with and we will send you a link to reset your password.

Add your courses

Get notes from the top students in your class.