MPATE-GE 2618: C Programming for Music Technology Unit 1.3
More operators e ++ and -- are increment and decrement operators ++i or i++ is equivalent to i = i + 1; --i or i-- is equivalent to i = i - 1; i += 1 is equivalent to i = i + 1; i -= 1 is equivalent to i = i 1; i *= 5 is equivalent to i = i * 5; i /= 3 is equivalent to i = i / 3;
Syntax for if statement: Conditional statements if (condition) { "statements; } Syntax for if-else: if (condition) { "statements; } else { "statements; }"
Conditional statements continued Syntax for else-if: if (condition) { "statements; } else if (condition) { "statements; } else if (condition) { "statements; } else { "statements; } e else ifs and final else are all optional Braces can be optional if there is only one statement to execute after the condition is satisfied, but you are strongly urged to use them. If the first else-if is true, only the statements encapsulated within its braces are executed; statements in subsequent else-ifs down the chain are NOT evaluated even if the else-if would also evaluate to true.
A useful gcc flag e Wall flag allows gcc to output all warnings gcc Wall test.c o test
Positive or negative? A problem example using conditionals and operators Write a program that prompts the user for an integer and then prints a statement saying whether that integer is positive, negative, or zero. Modify the program so it executes 5 times, making sure each prompt from the program asking the user to enter a value is labeled by iteration. At the end of the program print out a sum of the 5 values the user entered. See files posneg.c and posneg2.c for solution.
Note on style Use clear variable names Lower case vs. upper case: starting variable names with a lower case letter is recommended Good variable name: int numtries; Bad variable name for same value: float z2; Comment your code diligently Indent properly (this is very easy if you re using emacs hitting tab will automatically indent the line the correct amount) Be consistent in whatever you do
scanf revisited To read from standard input, use scanf What does this do when the user inputs, say, 999999999999999999999? It returns the number of input items successfully assigned. int n; scanf("%i", &n); printf("%i", n);
Logical operators Logical operators allow compound tests && is AND and is OR and! is NOT #include <stdbool.h> int a = 0; b = 10; char ch = 'z'; bool done = false; if (b > a && done == true ) { } // this is false if (b > a done) { } // this is true if (!(b > a && done)) { } // this is true if (!done && a <= 10) { } // this is true if (b!= 10 ch == 'y') { } // this is false
Leap year program Exercise: write a program that allows a user to enter a year and then tell the user whether that year is a leap year or not. A year is a leap year if: It s evenly divisible by 4 and not by 100 OR it s evenly divisible by 400 Use scanf for input and do some error checking on the input. Solution: see leap.c
for loops Basic for loop syntax: for (initial_expression; loop_condition; loop_expression) { statements; } Example: int i; for (i = 0; i < 10; i++) { "printf("*"); }
Nested for loops Exercise: a program that prints a triangle consisting of userspecified symbols Sample output: * ** *** **** ***** ****** Solution: triangle.c (renamed from test.c)
while loops Syntax for while loop: while (condition) { statements; } Exercise: rewrite the triangle printing program using while loops. Solution: triangle2.c (renamed from test2.c)
Fibonacci numbers 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368, 75025, 121393, F(n) = (n-1)+(n-2); Write a program that asks the user for a number, N, and then outputs the Nth Fibonacci number. See fib.c for solution.
e perils of overflow All variable types have a limit to data they can hold. e.g. a 32-bit integer has a range of 2 31 to 2 31 1 ese low and high limits can be found for in the library limits.h. See example overflow.c
Size of types When would you use long vs. short ints? How do I know the max and min values of each type for your platform? ese values can be found in limits.h SHRT_MIN, SHRT_MAX, USHRT_MAX INT_MIN, INT_MAX, UINT_MAX LONG_MIN, LONG_MAX, ULONG_MAX LLONG_MIN, LLONG_MAX, ULLONG_MAX etc. Note there are no min vals for unsigned types - that s because they re always zero.
do-while loops Syntax for do-while loop: do { "statements to repeat; } while (condition);" Exercise: Rewrite triangle printing program using do-while loops. Solution: triangle3.c
More on printf formatting Width and precision %[-][#]<width>.<precision> %3.2f %-2.5f etc." - means left justify # can mean show the prefix (e.g. 0x for a hex number) or show decimal point for floats. Exercise: write a program that creates a table of corresponding Fahrenheit and Celsius temperatures ranging from -10 to 10 degrees Celsius formatted so that the C/F columns are of a fixed width (8 characters wide) and only 2 decimal places are displayed. Formula Fahrenheit in terms of Celsius: F = 9 See temperature.c example 5 C + 32