190 likes | 293 Views
Introductory Concepts. ANSI-C. Types: Integer types. All data values are stored in memory using a binary representation char signed char unsigned char short int long unsigned short unsigned unsigned long Range: (32 bit machine) int : +/- 2 billion Char: 0..255 Short: -127..128
E N D
Introductory Concepts ANSI-C
Types: Integer types • All data values are stored in memory using a binary representation • char signed char unsigned char short int long unsigned short unsigned unsigned long • Range: (32 bit machine) • int : +/- 2 billion • Char: 0..255 • Short: -127..128 • Unsigned char: 0..65000
Remarks on Character type • char values are integer values between 0 and 255; • they can be manipulated as integers. • The value of a character variable can be safely forced to yield the actual ASCII number it represents. • The value of an integer variable can be forced to yield the character it represents if value is between 0..255. • See code: intchar.c
Types: floating types • floatdouble long double • Range: (32 bit machine) double : +- 10E38
Variables • Use to represent data in memory It has a name: identifier It has a location in memory It has a value It has a type: set of values that it can legally contain.
Variable declaration • int num; char letter; float value; • declarations with initializations: int num = 0; char letter = ‘a’;
Variable’s location in memory • Given: int value = 42; • We can find the location in memory for value: &value • When reading we use it, to indicate that value read must be placed in that location in memory: scanf(“%d”, &value);
Assignment: Fundamental operation with variables Name of variable whose value will be updated Value to be given to variable • Assignment updates value in variable identifier=expression; • Meaning of assignment: evaluates expression and give it to left hand side as its value • Type of result value must be equal to type of variable. • Assignment: an statement. • Power of computation: expressions you can write. • C is a very rich language in the formation of expressions
Looking up value in variable • The name of the variable will stand for two of its attributes: • In general a variable stands for the value it contains • On the left hand side of an assignment statement stands for the location in memory where the assignment will take place. • Does the equation below make sense? value = value + 1;
Expression formation • Literals: 45, 3.56, ‘a’, ‘Z’ • Variables • Arithmetic expressions form with literals, variables and operators • Integer arithmetic operators: +, -, *, \, % • Float, double arithmetic operators: +, -, *, \
Examples of expressions int value1 = 5; int value2 = 18; value1 + value2 – 10 value 2 – value1 + 3 value2/value1 value1/value2 value2 % value1 value1 % value2
Examples of expressions double value1 = 5.0; double value2 = 18.0; value1 + value2 – 10.0 value 2 – value1 + 3.0 value2/value1 value1/value2
Precedence of operators when expression does not have parenthesis int value1 = 5; int value2 = 8; value1 + value2 * 5 value1/value2 + 3 * value1
Arithmetic expression with mixed type variablesdangerous!!! int value1 = 5; double value2 = 7.1; value1 = value2; value2 = value1; value1*value2 value2 + value1 value1 + 5 + value2*3
Example • Convert fahrenheit degrees to celsius: 5 * (fahrenheit – 32) / 9 or 5/9 * (fahrenheit – 32) • Avoid errors: 5.0/9.0 * (fahrenheit – 32.0)
Input • Input is performed using the function scanf. This function takes two or more parameters. • The first parameter • specifies the type of data to be read in; • specified as a string which contains a specification of the type format of each of the variables to be read. • Second parameter • list of of the variables for the data specified in the string to be read.
Input Examples int x,y; float z; char ch; scanf(“%d”, &x); /*%d specifies read one integer */ scanf(“%f”, &z); /* “%f” specifies read one float */ scanf(“%c”, &ch); /* “%c” specifies to read one character*/ scanf(“%d%d”, &x,&y);/*”%d%d” specifies read 2 integers*/ scanf(“%f%d”, &z, &y); /*”%f%d” specifies read one float followed by one integer */ scanf(“%c%f%d%d”, &ch, &z, &x,&y); • scanf skips blanks, newlines and tabs when reading numbers;
Output • performed using the function printf. This function takes two or more parameters. • The first parameter • specifies the type (format) of data to be written out; • parameter is specified as a string which contains a specification of the type format of each of the variables to be output. • second parameter • it follows a list of of the variables for the data specified in the string to be written out.
Output Examples int x,y; float z; char ch; printf(“%d”, x); /*%d specifies to write one integer */ printf(“%f”, z); /* “%f” specifies to write one float */ printf(“%c”, ch); /* “%c” specifies write one character */ prinff(“%d”, ch); /* it writes the integer value of it */ printf(“%d %d”, x,y);/*”%d%d” writes 2 integers*/ printf(“%f %d”, z, y); /*”%f%d” specifies to write one float followed by one integer */ printf(“%c %f %d %d”, &ch, &z, &x,&y); • To print strings we can do:printf(“Hello there”); /* or */ printf(“%s”, “Hello there”); • We can also mix strings within % specifiers:printf(“The square root of %d is %f”, x, z);