primitive variable types basic types –char (single character or ascii integer value) –int...

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

Post on 19-Dec-2015

217 views

Category:

Documents


0 download

TRANSCRIPT

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

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)

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

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(<varName>) 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 … }

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

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

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

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

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

Scope• Variables declared outside of functions are global

– They are accessible by any function– They are accessible by separately compiled functions (extern

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

• Exampleint m; /* global variable */int main(int argc, char *argv[]){ int i; /* local variable */ ••• }

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

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

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

printf specifiers c Character Output

d or i Signed integer 392

e or E Scientific 3.92e+2

f Floating point 392.65

g or G Shorter of %d or %e 392.65

o Signed octal 610

s String Sample

u Unsigned integer 7235

x or X Unsigned hexadecimal 7Fa

l[idoux] Long

L[eEfgG] double

h[idoux] Short 7Fa

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

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"

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

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.

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

(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

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

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 %d\n", n, n++) Could execute n++ before n or the other way around.

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

Precedence and OperationsAlmost 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