Presentation is loading. Please wait.

Presentation is loading. Please wait.

Primitive Variable types Basic types –char (single character or ASCII integer value) –int (integer) –short (not longer than int) –long (longer than int)

Similar presentations


Presentation on theme: "Primitive Variable types Basic types –char (single character or ASCII integer value) –int (integer) –short (not longer than int) –long (longer than int)"— Presentation transcript:

1 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)

2 Strings and Boolean Strings: char *stringData; –* is a pointer (like java reference variables) –A string is an array of characters –Length(variable or data-type): sizeof( ) or sizeof( ) 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 … }

3 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

4 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

5 Scope Variables declared outside of functions are global –They are accessible by any function –They are accessible by separately compiled functions (extern ) 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 */ }

6 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 %d\n", i); –Example: int i=5; c='a'; float v = 6.2; printf("%d %c %f\n", i, c, v); /* call the function */ output: 5a6.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

7 printf specifiers cCharacterOutput d or iSigned integer392 e or EScientific3.92e+2 fFloating point392.65 g or GShorter of %d or %e392.65 oSigned octal610 sStringSample uUnsigned integer7235 x or XUnsigned hexadecimal7Fa l[idoux]Long L[eEfgG]double h[idoux]Short7Fa

8 Additional Features Specify width of field: printf(“%4.2f", 376.88888); outputs " 376.88" Force a sign: printf(“%+3i", 37); outputs "+37" Left justify (default is right justify): printf(“%-6i", 37); outputs "37 " Right justify: printf(“%6i", 37); outputs " 37" Dynamic width formatting: printf(“%*d", 5, 37); outputs " 37" Fill with zeroes: printf(“%05d", 37); outputs "00037"

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

10 (condition)?exp1:exp2 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

11 Expressions and operators: Almost JLJ JLJ –variable = –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 %d\n", n, n++) Could execute n++ before n or the other way around.

12 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


Download ppt "Primitive Variable types Basic types –char (single character or ASCII integer value) –int (integer) –short (not longer than int) –long (longer than int)"

Similar presentations


Ads by Google