Download C Programming Primer: A Comparison with Java and Basics - Prof. Thomas Way and more Study notes Operating Systems in PDF only on Docsity! 1 C Primer Outline • Overview comparison of C and Java • Good evening • Preprocessor • Command line arguments • Arrays and structures • Pointers and dynamic memory What we will cover • A crash course in the basics of C Like Java, like C • Operators same as Java: – Arithmetic • i = i+1; i++; i--; i *= 2; • +, -, *, /, %, – Relational and Logical • <, >, <=, >=, ==, != • &&, ||, &, |, ! • Syntax same as in Java: – if ( ) { } else { } – while ( ) { } – do { } while ( ); – for(i=1; i <= 100; i++) { } – switch ( ) {case 1: … } – continue; break; datatype size values char 1 -128 to 127 short 2 -32,768 to 32,767 int 4 -2,147,483,648 to 2,147,483,647 long 4 -2,147,483,648 to 2,147,483,647 float 4 3.4E+/-38 (7 digits) double 8 1.7E+/-308 (15 digits long) Simple Data Types Java programmer gotchas (1) { int i for(i = 0; i < 10; i++) … NOT { for(int i = 0; i < 10; i++) … 2 Java programmer gotchas (2) • Uninitialized variables – catch with –Wall compiler option #include <stdio.h> int main(int argc, char* argv[]) { int i; factorial(i); return 0; } Java programmer gotchas (3) • Error handling – No exceptions – Must look at return values “Good evening” #include <stdio.h> int main(int argc, char* argv[]) { /* print a greeting */ printf("Good evening!\n"); return 0; } $ ./goodevening Good evening! $ Breaking down the code • #include <stdio.h> – Include the contents of the file stdio.h • Case sensitive – lower case only – No semicolon at the end of line • int main(…) – The OS calls this function when the program starts running. • printf(format_string, arg1, …) – Prints out a string, specified by the format string and the arguments. format_string • Composed of ordinary characters (not %) – Copied unchanged into the output • Conversion specifications (start with %) – Fetches one or more arguments – For example • char %c • char* %s • int %d • float %f • For more details: man 3 printf C Preprocessor #define MAX 20 int main(int argc, char* argv[]) { printf(“%d\n”, MAX); return 0; } 5 Function Parameters • Function arguments are passed “by value”. • What is “pass by value”? – The called function is given a copy of the arguments. • What does this imply? – The called function can’t alter a variable in the caller function, but its private copy. • Three examples Example 1: swap_1 void swap_1(int a, int b) { int temp; temp = a; a = b; b = temp; } Q: Let x=3, y=4, after swap_1(x,y); x =? y=? A1: x=4; y=3; A2: x=3; y=4; Example 2: swap_2 void swap_2(int *a, int *b) { int temp; temp = *a; *a = *b; *b = temp; } Q: Let x=3, y=4, after swap_2(&x,&y); x =? y=? A1: x=3; y=4; A2: x=4; y=3; Example 3: scanf #include <stdio.h> int main() { int x; scanf(“%d\n”, &x); printf(“%d\n”, x); } Q: Why using pointers in scanf? A: We need to assign the value to x. Dynamic Memory • Java manages memory for you, C does not – C requires the programmer to explicitly allocate and deallocate memory – Unknown amounts of memory can be allocated dynamically during run-time with malloc() and deallocated using free() Not like Java • No new • No garbage collection • You ask for n bytes – Not a high-level request such as “I’d like an instance of class String” 6 malloc • Allocates memory in the heap – Lives between function invocations • Example – Allocate an integer •int* iptr = (int*) malloc(sizeof(int)); – Allocate a structure •struct name* nameptr = (struct name*) malloc(sizeof(struct name)); free • Deallocates memory in heap. • Pass in a pointer that was returned by malloc. • Example – int* iptr = (int*) malloc(sizeof(int)); free(iptr); • Caveat: don’t free the same memory block twice!