DOC PREVIEW
UT CS 378 - Lecture Slides

This preview shows page 1-2-3-22-23-24-44-45-46 out of 46 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 46 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 46 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 46 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 46 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 46 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 46 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 46 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 46 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 46 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 46 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

A crash course in CCS378H - Spring 2011Christian MillerOkay so...•I have two hours to teach you the basics of C•You will use it for most of your assignments this semester•Also the most useful language to know, if you want to do pretty much anythingComputer access•You’ll be doing all your work on the CS machines•The Painter or ENS Linux machines will work•You can SSH into them from home•e.g. ssh [email protected]•list of public hosts here: http://bit.ly/e2IfEV•use PuTTY if you’re on a Windows machineEditors•You’ll need to get comfortable with a text editor•Most UNIX editors are arcane and bizarre•vim and emacs are the usual choices•Pick one and look online for tutorials•Be prepared to spend a lot of time learning themWhat is C, really?•It’s a relatively old programming language (1972)•Much lower-level than Java•No classes, purely procedural•Has the ability to manipulate raw bits and memory•Most systems-level coding is done in C, as well as a huge amount of application-level stuffSome basicsC is a compiled languagetext file (.c) object file (.o)executable (.bin / .exe)compiler (gcc) linker (ld)Hello world/* hello.c A simple hello world program.*/#include <stdio.h>int main(int argc, char **argv){! printf("Hello, world!\n"); // greetings! return 0;}comments•Block comments: /* Several lines here */•Single line comments: // One line here•Some older C compilers don’t support these#include statements•Use #include <...> to access code libraries•e.g. #include <stdio.h>•Put them at the top, before you use them•Common ones: stdlib.h (standard utilities), stdio.h (basic I/O), string.h (string functions), time.h (time functions), math.h (math functions)Function declarations•Functions are pretty much the same as Java, except there are no visibility specifiers (public, private, etc.)•Functions have one return type (can be void)•There can be any number of parameterstype fn_name(type param1, type param2){! // code here! return something;}Main function•The main function is the entry point to your program•Return value indicates success (0) or failure (nonzero), though this is usually ignored•argc and argv hold command line argumentsint main(int argc, char **argv){! // code goes here! return 0;}Function caveats•You can’t use a function before declaring it!•To use a function before defining it, declare it first with a function prototype•Parameters passed to functions are copied, so changes made to them disappear when the function ends (use pointers to circumvent this)int foo(); // function prototypeint main(int argc, char **argv){! printf("%i\n", foo()); // foo called before it's defined! return 0;}int foo() // foo defined down here{! return 99;}Printf• printf handles console output, declared in stdio.h•The first argument is the format string, other parameters are for substitutions•Example: printf("Hello, world!\n");•Example: printf("Login attempt %i:", attempts);•There are tons of format specifiers, look them upprintf(char *format, type val1, type val2, ...)Building and running•By default, gcc will compile and link your program•The -o flag tells it the name of the output binary•Use ./name to run somethingVariables•The compiler tries to enforce types, and will attempt to convert or error out as appropriate•Explicit typecasts can force conversions•Variables must be defined at the beginning of a function, before any other code!double foo(){! int a = -5;! unsigned int b = 3;! int c;! c = a * (int)b; // cast b to int, just to be sure! double q; // illegal, must be at top of function! q = c; // implicitly converts c to double! return q;}Data types•char: one byte (eight bits) signed integer•short: two byte signed integer (same as short int)•int: four byte signed integer (same as long int)•unsigned: add to the above to make them unsigned•float: four byte floating point•double: eight byte floating point•const: add to a data type to make its value constantAssignment•The equals operator copies the right hand side to the left hand side•It also returns the value it copied, which enables some cool tricksLogical operators•Logic is supported as usual•In order of precendence: ! (not), && (and), || (or)•No boolean type; any integer zero is considered false, any integer nonzero is true•!0 = 1, usually•For example: 1 && !1 || !0 && -999 // trueMath operators•Math is the same as usual, with normal operator precendence (use parentheses when unsure)•Supported operators are: + - * / %•In-place versions as well: ++, --, +=, *=, etc.•Integers round down after every operation•No operator for exponent, ^ means something much different (look for pow() in math.h)Comparison operators•Comparisons are also what you’d expect•== (equals), != (not equals), < (less than), <= (less than or equals), > (greater than), >= (greater than or equals)Bitwise operators•These treat data as a simple collection of bits•Useful for low-level code, you’ll use them a bunch•They are: & (bitwise and), | (bitwise or), ~(bitwise not), ^ (bitwise xor), << (shift left), >> (shift right)•Also useful: you can write hex numbers using 0x•For example: 0x5B == 91If / else•Evaluates the given conditions in order, and will execute the appropriate block•Can have any number of else ifs•Else if and else are optionalif (condition)!{!! // condition is true!}! else if (other_condition)!{!! // condition not true, but other_condition is!}! else!{!! // none of the above were true!}Switch•A convenient way of doing lots of equality checks•The break statements in each case are necessary!switch (var){! case 0:!! // if var == 0!! break;! case 3:!! // if var == 3!! break;! default:!! // if none of the other cases!! break;}Loops•Loops work the same as in Java•Remember to declare your loop variables at the top of the function•Also do / while loops: same as while, but automatically execute oncefor (i = 0; i < n; i++){! // will execute n times}while (i != 0){! // loop body}Arrays•To declare an array, specify the size in brackets•Size is fixed once an array is declared•You can also provide an initializer list in braces•If you omit the dimension, the compiler will try to figure it out from the initializer list•Use brackets to index, starting with zero (not bounds checked!)int array[15];int array2[] = { 3, 4, 99, -123, 400 };for (i = 0; i < 5; i++)! printf("%i\n",


View Full Document

UT CS 378 - Lecture Slides

Documents in this Course
Epidemics

Epidemics

31 pages

Discourse

Discourse

13 pages

Phishing

Phishing

49 pages

Load more
Download Lecture Slides
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture Slides and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture Slides 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?