1 / 19

Introductory Concepts

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

Download Presentation

Introductory Concepts

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Introductory Concepts ANSI-C

  2. 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

  3. 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

  4. Types: floating types • floatdouble long double • Range: (32 bit machine) double : +- 10E38

  5. 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.

  6. Variable declaration • int num; char letter; float value; • declarations with initializations: int num = 0; char letter = ‘a’;

  7. 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);

  8. 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

  9. 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;

  10. Expression formation • Literals: 45, 3.56, ‘a’, ‘Z’ • Variables • Arithmetic expressions form with literals, variables and operators • Integer arithmetic operators: +, -, *, \, % • Float, double arithmetic operators: +, -, *, \

  11. Examples of expressions int value1 = 5; int value2 = 18; value1 + value2 – 10 value 2 – value1 + 3 value2/value1 value1/value2 value2 % value1 value1 % value2

  12. Examples of expressions double value1 = 5.0; double value2 = 18.0; value1 + value2 – 10.0 value 2 – value1 + 3.0 value2/value1 value1/value2

  13. Precedence of operators when expression does not have parenthesis int value1 = 5; int value2 = 8; value1 + value2 * 5 value1/value2 + 3 * value1

  14. 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

  15. Example • Convert fahrenheit degrees to celsius: 5 * (fahrenheit – 32) / 9 or 5/9 * (fahrenheit – 32) • Avoid errors: 5.0/9.0 * (fahrenheit – 32.0)

  16. 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.

  17. 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;

  18. 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.

  19. 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);

More Related