Unformatted text preview:

Slide 1Before We Get Started…Auditing the CourseC Syntax and Hello WorldLexical ScopingComparison and Mathematical OperatorsAssignment OperatorsA Quick Digression About the CompilerC Memory PointersThe “memory”What is a Variable?Multi-byte VariablesMemory, a more detailed view…Example…Another ExampleCan a C function modify its arguments?Slide 17C PointersPointers (cont.)Pointer ValidityAnswer: No, it’s invalid…Example: What gets printed out?Example: Usage of Pointers & Pointer ArithmeticExample [Cntd.]Use of pointers, another example…Slide 26ME964High Performance Computing for Engineering Applications“There is no reason for any individual to have a computer in their home.” Ken Olson, president and founder, Digital Equipment Corporation, 1977.© Dan Negrut, 2011ME964 UW-MadisonQuick Overview of C ProgrammingJanuary 20, 2011Before We Get Started…Last timeCourse logistics & syllabus overviewDiscussed Midterm ProjectsDiscrete Element Method on the GPUCollision Detection on the GPUBasic Finite Element Analysis on the GPUSparse Linear Solver on the GPUTodayQuick overview of C ProgrammingEssential read: Chapter 5 of “The C Programming Language” (Kernighan and Ritchie)Acknowledgement: Slides on this C Intro include material due to Donghui Zhang and Lewis GirodCorrection:Email your homework a this address: [email protected] 2Auditing the CourseWhy auditing?Large participation justifies another offering of this courseAugments your experience with this classYou can get an account on the GPU clusterYou will be added to the email listCan post questions on the forumHow to register for auditing:In order to audit a course, a student must first enroll in the course as usual. Then the student must request to audit the course online. (There is a tutorial available through the Office of the Registrar.) Finally, the student must save & print the form. Once they have obtained the necessary signatures, the form should be turned in to the Academic Dean in the Grad School at 217 Bascom. The Grad School offers more information on Auditing Courses in their Academic Policies and Procedures. Tutorial website: http://www.registrar.wisc.edu/isis_helpdocs/enrollment_demos/V90CourseChangeRequest/V90CourseChangeRequest.htm Auditing Courses: http://www.grad.wisc.edu/education/acadpolicy/guidelines.html#13 3C Syntax and Hello World#include <stdio.h>/* The simplest C Program */int main(int argc, char **argv){ printf(“Hello World\n”); return 0;}The main() function is always where your program starts running. #include inserts another file. “.h” files are called “header” files. They contain declarations/definitions needed to interface to libraries and code in other “.c” files. A comment, ignored by the compilerBlocks of code (“lexical scopes”) are marked by { … }Return ‘0’ from this function What do the < > mean?4Lexical ScopingEvery Variable is Defined within some scope. A Variable cannot be referenced by name (a.k.a. Symbol) from outside of that scope.The scope of Function Arguments is the complete body of that function.void p(char x){ /* p,x */ char y; /* p,x,y */ char z; /* p,x,y,z */} /* p */char z; /* p,z */void q(char a){ char b; /* p,z,q,a,b */ { char c; /* p,z,q,a,b,c */ } char d; /* p,z,q,a,b,d (not c) */}/* p,z,q */The scope of Variables defined inside a function starts at the definition and ends at the closing brace of the containing blockLexical scopes are defined with curly braces { }.The scope of Variables defined outside a function starts at the definition and ends at the end of the file. Called “Global” Vars.legal?char b?5Comparison and Mathematical Operators== equal to< less than<= less than or equal> greater than>= greater than or equal!= not equal&& logical and|| logical or! logical not+ plus- minus* mult/ divide % moduloThe rules of precedence are clearly defined but often difficult to remember or non-intuitive. When in doubt, add parentheses to make it explicit. Beware division:• 5 / 10  0 whereas 5 / 10.0  0.5• Division by 0 will cause a FPE& bitwise and| bitwise or^ bitwise xor~ bitwise not<< shift left>> shift rightDon’t confuse & and &&.. 1 & 2  0 whereas 1 && 2  <true>6Assignment Operatorsx = y assign y to xx++ post-increment x++x pre-increment xx-- post-decrement x--x pre-decrement xNote the difference between ++x and x++ (high vs low priority (precedence)):Don’t confuse “=“ and “==“! int x=5;int y;y = ++x;/* x == 6, y == 6 */int x=5;int y;y = x++;/* x == 6, y == 5 */int x=5;if (x=6) /* always true */ { /* x is now 6 */}/* ... */int x=5;if (x==6) /* false */{ /* ... */}/* x is still 5 */x += y assign (x+y) to xx -= y assign (x-y) to xx *= y assign (x*y) to xx /= y assign (x/y) to xx %= y assign (x%y) to x7A Quick Digression About the Compiler#include <stdio.h>/* The simplest C Program */int main(int argc, char **argv){ printf(“Hello World\n”); return 0;}my_program__extension__ typedef unsigned long long int __dev_t;__extension__ typedef unsigned int __uid_t;__extension__ typedef unsigned int __gid_t;__extension__ typedef unsigned long int __ino_t;__extension__ typedef unsigned long long int __ino64_t;__extension__ typedef unsigned int __nlink_t;__extension__ typedef long int __off_t;__extension__ typedef long long int __off64_t;extern void flockfile (FILE *__stream) ;extern int ftrylockfile (FILE *__stream) ;extern void funlockfile (FILE *__stream) ;int main(int argc, char **argv){ printf(“Hello World\n”); return 0;}Compilation occurs in two steps:“Preprocessing” and “Compiling”In Preprocessing, source code is “expanded” into a larger form that is simpler for the compiler to understand. Any line that starts with ‘#’ is a line that is interpreted by the Preprocessor.• Include files are “pasted in” (#include)• Macros are “expanded” (#define)• Comments are stripped out ( /* */ , // )• Continued lines are joined ( \ )PreprocessCompileThe compiler then converts the resulting text (called translation unit) into binary code the CPU can execute.8C Memory PointersTo discuss memory pointers, we need to talk a bit about the concept of memoryWe’ll conclude by touching on a couple of other C elements:Arrays, typedef, and structs9The “memory”Memory: similar to a big table of numbered slots where bytes


View Full Document

UW-Madison ME 964 - Quick Overview of C Programming

Documents in this Course
Load more
Download Quick Overview of C Programming
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 Quick Overview of C Programming 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 Quick Overview of C Programming 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?