MPATE-GE 2618: C Programming for Music Technology Unit 1.2
Binary = base 2 Decimal = base 10 Octal = base 8 Hexadecimal = base 16 Examples Base 10 357 = (10 2 * 3) + (10 1 * 5) + (10 0 * 7) Binary notation Base 2 1011 = (2 3 * 1) + (2 2 * 0) + (2 1 * 1) + (2 0 * 1) Base 16 A = 10; B = 11; C = 12; D = 13; E = 14; F = 15 0xFA13 = (16 3 * 15) + (16 2 * 10) + (16 1 * 1) + (16 0 + 3) Binary Octal Decimal Hex 0000 0001 01 01 01 0000 0010 02 02 02 0000 0011 03 03 03 0000 0100 04 04 04 0000 0101 05 05 05 0000 0110 06 06 06 0000 0111 07 07 07 0000 1000 10 08 08 0000 1001 11 09 09 0000 1010 12 10 0A 0000 1011 13 11 0B 0000 1100 14 12 0C 0000 1101 15 13 0D 0000 1110 16 14 0E 0000 1111 17 15 0F 0001 0000 20 16 10 0001 0001 21 17 11
Data representation: Bits and bytes Number of Bits Value 1 2 2 4 3 8 4 16 5 32 6 64 7 128 8 (one byte, char) 256 9 512 10 1024 16 (short int) Approx. 65K 24 Approx. 16.7M 32 (int) Approx. 4B
ASCII encoding ASCII = American Standard Code for Information Exchange An industry standard which assigns an 8-bit number to letters, numbers and characters. Original standard without special symbols consisted of 7 bits (128 possibilities). e.g. the letter A = 65, B = 66, etc. Convert the letters A (decimal 65) and m (decimal 109) to binary and hexadecimal: A = decimal 65 = binary 0100 0001 = hex 0x41 m = decimal 109 = binary 0110 1101 = hex 0x6D
Note on C99 C99 is the latest standard for the C programming language ere is a newer one that was published very recently (in 2011), C11. Older C standards Initial development of C took place in the late 60s/early 70s K&R C (1978) ANSI C aka Standard C or C89 (1989) ISO C aka C90 (1990) mostly the same as ANSI C C99 (2000) Some compilers support many C99 features, others don t (Microsoft) gcc has most C99 features implemented
Another type of comment Original C comments /* This is a comment */ C++ comments are part of C99 // A comment that only affects a single line
printf! printf is function that prints a formatted string (a series of text characters) and zero or more variables to stdout (the terminal). Include the header file stdio.h to use printf in your program.! Calling printf:!!printf(formatstring, variables...); e variables are printed in the order they are passed to printf and interpreted as the type specified in the string. e character % always precedes a type specifier (also known as a conversion specifier). (If you want to print an actual percent sign, use %% )
Some basic syntax: statements, variables, types Statements are followed by a semicolon (;) A variable declaration is one type of statement syntax: <type> <variablename>; int number; number = 5; // assignment You can initialize them on declaration too int number = 5; Some rules on naming variables: Must begin with a letter or underscore (_) not a number $ is not a valid character Can t have spaces Can t use reserved words like int Basic types in C int, float, double, char, bool (_Bool)
Types: int When printing an int in decimal format use either %d or %i If you assign a value starting with a leading zero, it will be interpreted as an octal value int n = 0177; // equivalent to decimal value 12 printf( An octal number: %#o, n); // format string If you assign a value preceded by 0x, it will be interpreted as a hexadecimal value int n = 0x6D; // equivalent to decimal value 109 printf( A hex number: %#x, n); // format string Type specifiers: long (%li), long long (%lli), short (%hi), unsigned (%u) NOTE: long long is only available in C99
Types: float and double floats are used to store values with decimal places doubles are larger than floats and more precise printf format specifiers for doubles and floats: %f, %e, %g %.5f, for example, displays 5 decimal places (Never test floats and doubles for equality)
Types: char and _Bool Char is a single character and is denoted by single quotes (double quotes are used for strings); use %c to print char ch = c ; char ch2 = \n ; // newline character _Bools have only two states, 0 or 1 e type bool as well as true and false are defined in the library <stdbool.h> bool condition = false; Print them as you would integers (using %i or %d) NOTE: bool is only available in C99
Reading in input To read from standard input, use scanf (for now) e following reads into variable n an integer entered by the user: int n; scanf( %i, &n); is reads in two floats into f1 and f2: float f1, f2; scanf( %f %f, &f1, &f2);
Basic types: review Four basic types: int float/double char bool (make sure to add #include <stdbool.h>)
More types: long, short, unsigned NOTE: size of integers depends on the platform; C leaves features of integer types implementation-defined, meaning that it is up to the particular compiler to determine the exact specifications. You can add you can put long, short, and unsigned before the int type specifier or leave out the int (it s assumed by default): short int i; short i; /* equivalent to the line above*/ long int i; long i; /* equivalent to the line above*/ long long int i; long long i; unsigned short int i; unsigned short i; /* equivalent to the line above*/
What s wrong with this? #include <stdio.h>! int main (void)! {! short unsigned i = -2;! printf("%d\n", i);! }! return 0;!
Operators Arithmetic operators: + for addition, - for subtraction, * for multiplication, / for division Precedence: some operators have higher priority a * b + c * d is the same as (a * b) + (c * d) When in doubt, use parentheses White space not necessary but recommended for readability Associativity: expressions containing operators of the same precedence are evaluated either left to right or vice versa depending on the operator. For example, for the assignment operator (= ): int x = 0, y = 5, z = 3; x = y = z; /* x has the value 3, not 5 */!
Operators continued e unary minus ( ) operator negates a value it precedes and has higher precedence than all the arithmetic operators (example: c = -a * b; ) e modulus operator % gives the remainder of the first value divided by the second value int a, b, c; a = 10; b = 4; c = a % b; // results in c being 2
Integer and floating point conversions and casting floats are truncated when assigned to ints (beware of this) float f1 = 123.125, f2; int i1, i2 = -150; i1 = f1; // i1 set to 123 f1 = i2; // f1 set to 150.000000 f1 = i2 / 100; // f1 set to 1.000000 f2 = i2 / 100.0; // f2 set to 1.500000 f2 = (float)i2 / 100; // f2 set to 1.500000 e last line above is an example of casting an integer to a float
Relational operators Relational operators: == equal to!= not equal to < less than <= less than or equal to > greater than >= greater than or equal to
Boolean expressions Expressions can be true or false int a = 4, b = 10; a == b is false a!= b is true a > b is false b > a is true b - a > a * 2 is false