1 / 19

# Introductory Concepts - PowerPoint PPT Presentation

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

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.

## PowerPoint Slideshow about ' Introductory Concepts' - imani-fisher

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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

### 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

• 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

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

• int num; char letter; float value;

• declarations with initializations:

int num = 0; char letter = ‘a’;

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

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

• 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;

• Literals: 45, 3.56, ‘a’, ‘Z’

• Variables

• Arithmetic expressions form with literals, variables and operators

• Integer arithmetic operators:

+, -, *, \, %

• Float, double arithmetic operators:

+, -, *, \

int value1 = 5;

int value2 = 18;

value1 + value2 – 10

value 2 – value1 + 3

value2/value1

value1/value2

value2 % value1

value1 % value2

double value1 = 5.0;

double value2 = 18.0;

value1 + value2 – 10.0

value 2 – value1 + 3.0

value2/value1

value1/value2

int value1 = 5;

int value2 = 8;

value1 + value2 * 5

value1/value2 + 3 * value1

Arithmetic expression with mixed type variables parenthesisdangerous!!!

int value1 = 5;

double value2 = 7.1;

value1 = value2;

value2 = value1;

value1*value2

value2 + value1

value1 + 5 + value2*3

Example parenthesis

• Convert fahrenheit degrees to celsius:

5 * (fahrenheit – 32) / 9

or

5/9 * (fahrenheit – 32)

• Avoid errors:

5.0/9.0 * (fahrenheit – 32.0)

Input parenthesis

• 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 parenthesis

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 parenthesis

• 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 parenthesis

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