Primitive Variable types Basic types char single character

Primitive Variable types • Basic types – – – char (single character or ASCII integer value) int (integer) short (not longer than int) long (longer than int) float (fraction floating point) double (fraction high precision floating point) • Modified types – – – unsigned char or unsigned int (no sign bit) long double (more precision than just double) short int register int (for data used often In a program) static double (private if global, persist if in function) • Rules – short int (not longer than int – int (at least 16 bits wide, not longer than long int) – long int (at least 32 bits wide)

Strings and Boolean • Strings: char *string. Data; – * is a pointer (like java reference variables) – A string is an array of characters – Length(variable or data-type): sizeof(<var. Name>) or sizeof(<data-type>) • Boolean – – declare as int true is non-zero, false is zero No true/false reserved word (use #define if you wish) Example: int flag=1; if (flag) { … true part … } else { … false part … }

Defining Constants • Using preprocessor directive: #define PI 3. 14167 Note: Preprocessing directives do not end with ; Note: Preprocessing replaces by PI with 3. 14167 • const int MONTHS=12; Analogous to 'final' in Java

Strongly Typed • Like Java, C is a Typed Language – Java is Strongly typed: compile errors occur when rules are violated – C is Somewhat Strongly types: errors do not always occur if rules are violated (ex: int i=5. 6; ) • C and Java – All variables must be declared before use – Variables start with alphanumeric or underscore, then can have alphanumeric, underscore, or numbers – Variables are case sensitive • Some C compilers – variables must be declared at the start of a block – Recent C's sometimes relax this requirement

Scope • Variables declared outside of functions are global – They are accessible by any function – They are accessible by separately compiled functions (extern <var. Name>) unless declared as static (analigous to Java private) – Only use global variables when necessary • Local variable is declared within a function – Only accessible within the declared function – This is because of the activation record concept – Those declared static persist in memory after the function returns • Example int m; /* global variable */ int main(int argc, char *argv[]) { int i; /* local variable */ • • • }

printf function (one of many built-in functions) • Syntax: printf("template", args … ) • "template" – Mixes text with conversion specifiers – Example: int i=5; printf("i is %dn", i); – Example: int i=5; c='a'; float v = 6. 2; printf("%d %c %fn", i, c, v); /* call the function */ output: 5 a 6. 200000 • Notes: – Java has System. out. printf (JVM 1. 5) – awk allows printf – C gives No errors when variables don't match formats Note: break characters like n, t, r, a are JLJ

printf specifiers c d or i e or E f Character Signed integer Scientific Floating point Output 392 3. 92 e+2 392. 65 g or G o s u x or X l[idoux] L[e. Efg. G] h[idoux] Shorter of %d or %e Signed octal String Unsigned integer Unsigned hexadecimal Long double Short 392. 65 610 Sample 7235 7 Fa

Additional Features • Specify width of field: printf(“%4. 2 f", 376. 88888); outputs " 376. 88" • Force a sign: printf(“%+3 i", 37); outputs "+37" • Left justify (default is right justify): printf(“%-6 i", 37); outputs "37 " • Right justify: printf(“%6 i", 37); outputs " 37" • Dynamic width formatting: printf(“%*d", 5, 37); outputs " 37" • Fill with zeroes: printf(“%05 d", 37); outputs "00037"

Control Statements: Almost JLJ • JLJ – – – – if if-else switch while do-while for continue and break (<boolean expression>)? <expression>: <expression> • Not JLJ – Boolean is an int (0 = false, not 0 = true) – for (int i=0; i<10; i++) is illegal; declare i explicitly first.

(condition)? exp 1: exp 2 • x = (y<0)? –y: y; if (y<0) x = -y; else x = y; • x = (y<0)? x + y – 1: y – x + 1; if (y<0) x = x + y -1; else x = y – x + 1; • max = (a>b)? a: b; if (a>b) max = a; else max = b; • Note: this construct is handy for function calls

Expressions and operators: Almost JLJ • JLJ – variable = <expression> – Multiple assignments: i = j = k = 0; – Increment and decrement: n++, n--, ++n, and --n – Adjust previous value: +=, -=, *=, /=, %=, &=, |= – Logical: &&, ||, and ! • Not JLJ – str + 3 does not concatenate Use sprintf: sprintf(buf, “%s%d”, str, 3)); – printf("%d %dn", n, n++) Could execute n++ before n or the other way around.

Precedence and Operations Almost JLJ • JLJ – – – – 5 / 3 gives a truncated result of 1 5. 0 / 3 gives a fractional result of 1. 6666 5 % 3 gives a result of 2 Precedence is like java except for the star operator (see below) No exponentiation operator Unary + and – Casting: float v = 5 / (float) 3; Short circuit logical expression evaluation • Not JLJ – Java: all non-primitives are reference variables – C: non primitives may not be reference variables, so a dereference operator is needed (*). More later
- Slides: 12