C-Programming CSC209: Software Tools and Systems Programming Paul Vrbik University of Toronto Mississauga https://mcs.utm.utoronto.ca/~209/ Adapted from Dan Zingaro s 2015 slides. Week 2.0 1 / 19
What is C? 1. Structured, like a high-level language. 2. Grants you machine access, like a low-level language. 3. A small language (e.g. 49-page reference manual), extendable with libraries. 4. Permissive: assumes you know what you re doing. 5. Good: efficient, portable, powerful (data types, operators), and flexible. 6. Bad: easy to make errors, terse code, little support for modularization. 2 / 19
Hello World HelloWorld.c 1 # include <stdio.h> 2 int main () { 3 printf (" Hello, World!\n"); 4 return 0; 5 } 1 $ls 2 HelloWorld. c 3 $gcc -o HelloWorld HelloWorld. c 4 $./ HelloWorld 5 Hello World! 6 $_ 3 / 19
For Loops sandbox.c 1 # include <stdio.h> 2 int main () { 3 for ( int counter = 0; counter <= 10; counter ++) { 4 printf ("%d ", counter ); 5 } 6 } 1 $gcc -o sandbox sandbox. c &&./ sandbox 2 0 1 2 3 4 5 6 7 8 9 10$_ Note $A && B means: Execute A then if successful execute B. 4 / 19
While 1 while ( condition ) { 2 <code > 3 } Do-While 1 do { 2 <code > 3 } while ( condition ); Note condition is some predicate evaluating to 1 (True) or 0 (False). 5 / 19
If-Then-Else 1 if ( predicate ) { 2 <code > 3 } else ( predicate ) { 4 <code > 5 } else ( predicate ) { 6 <code > 7 }... 8 } else { 9 <code > 10 } 6 / 19
Functions 1 // printf is not include by default 2 # include <stdio.h> 3 4 // function prototypes 5 int gcd ( int x, int y); 6 7 int main ( void ) { 8 // variable declarations 9 int i; 10 for (i = 0; i < 5; i ++) { 11 printf (" GCD of 12 and %d is %d.\n", i, gcd (12, i)); 12 } 13 return 0; 14 } 7 / 19
Functions 1. The printf function is not built-in to C #include: add declarations of external functions 2. main(void) returns an int, the exit status. 3. Functions must be: declared (which tells compiler how to use function), and defined (which creates the function). 4. Functions should be declared (not necessarily defined) before they are called. 8 / 19
Defaults Uninitialized variables do not initialize to nice values like 0 or "" rather to whatever happens to be in memory. sandbox.c 1 # include <stdio.h> 2 int main ( void ) { 3 int i; 4 printf ("%d\n", i); 5 return 0; 6 } 1 $gcc -o sandbox sandbox. c &&./ sandbox 2 121069622 3 $_ 9 / 19
Warnings Use -Wall when compiling to get all the help you can get. The option -g includes symbols for debuggers. 1 $gcc - Wall -g -o sandbox sandbox. c 2 HelloWorld. c :5:18: warning : variable 'i' is uninitialized when used here [- Wuninitialized ] 3 printf ("%d\n", i); 4 ^ 5 1 warning generated. 6 $_ 10 / 19
Integers 1 # include <stdio.h> 2 int main ( void ) { 3 int i = 38; long el = 38 L; 4 int hex = 0 x2a ; int oct = 033; 5 6 printf ("i = %d, el = %ld, hex = %d, oct = %d\n", i, el, hex, oct ); 7 8 return 0; 9 } 1 $gcc -o sandbox sandbox. c &&./ sandbox 2 i = 38, el = 38, hex = 42, oct = 27 3 $ 11 / 19
Doubles 1 # include <stdio.h> 2 int main ( void ) { 3 double d1 = 0.3, d2 = 3.0, d3 = 6.02 e23 ; 4 5 printf ("d1 = %f, d2 = %f, d3 = %e\n", d1, d2, d3); 6 printf ("d1 = %f, d2 = %e, d3 = %f\n", d1, d2, d3); 7 return 0; 8 } 1 $gcc -o sandbox sandbox. c &&./ sandbox 2 d1 = 0.300000, d2 = 3.000000, d3 = 6. 020000 e +23 3 d1 = 0.300000, d2 = 3. 000000 e +00, d3 = 601999999999999995805696. 000000 12 / 19
Literals and Types Literal Value Type 38 38 int 38L 38 long int 0x2a (hex) 42 int 033 (octal) 27 int 38.0 38.0 double 38.0f 38.0 float 13 / 19
More about ints Many C values are really ints: 1. Boolean values are ints (0 means false, nonzero means true). 2. Characters are ints (ASCII codes) (e.g. 'a' has value 97, '\n' has value 10) 3. signed vs. unsigned types are different forms of ints. 4. char, int, long, etc. are just different sizes of ints. 14 / 19
Data Type Conversion In an assignment statement, the expression on the right side is converted to the type of the variable on the left 1 char c; 2 int i = c; /* c is converted to int */ 3 double d = i; /* i is converted to double */ provided the variable s type is at least as wide as the expression s type 1 char c = 500; /* compiler warning */ 2 double d1 = 4.5; 3 int k = d1; 4 printf ("c = %c, k = %d\n", c, k); 5 /* c is a strange symbol, k = 4*/ 15 / 19
What happens when the following code is executed? 1 # include <stdio.h> 2 int main ( void ) { 3 char c = 127; 4 int d; 5 printf ("c = %d\n", c); 6 c ++; 7 d = 512 / c; 8 printf ("c = %d, d = %d\n", c, d); 9 return 0; 10 } 1 $gcc -o sandbox sandbox. c &&./ sandbox 2 c = 127 3 c = -128, d = -4 16 / 19
Results of Operations Operations on values of the same type produce results of that type. For instance, if we divide two integers, we perform integer division (dropping the fraction): 10 / 3 = 3 10.0 / 3.0 = 3.333333... Whereas if we perform an operation with one integer and one floating-point number, the result is a floating-point number: 4.3 + 5 = 9.3 17 / 19
Casting A cast is used to change the type of a value. For instance, (int)2.95 converts the floating-point number 2.95 to the int 2. The cast operator has a higher precedence than the mathematical operators (int)(5.7) + 8.9 = 5+8.9 = 13.9 and not (int)(5.7 + 8.9) = (int)14.6 = 14. 18 / 19
Halftime 1. Worksheet 1. 2. Arrays. 19 / 19