Chapter 2 - Part I(PowerPoint Format)

Download Report

Transcript Chapter 2 - Part I(PowerPoint Format)

EG280 - CS for Engineers
Chapter 2, Introduction to C Part I
Topics:
• Program structure
• Constants and variables
• Assignment Statements
• Standard input and output
• Mathematical and character
functions
Chapter 2 Quiz Topics
•
•
•
•
•
•
Page 45 Practice! 1 – 21
Page 46 Practice! 1 – 6
Page 50 Practice! 1 – 9
Page 54 Practice! 1 – 4
Page 56 Practice! 1 – 6
Page 60 Practice! 1 – 4 (beware answer in back of
text for #1)
Program Structure - General Form
preprocessing directives (i.e. #include
<stdio.h>
int main(void)
{
declarations
statements
}
Spring 2001
EG280 CS for Engineers
3
Program Structure
•Comments begin with /* and end */
•Preprocessor directives give instructions
to the compiler (i.e., #include <stdio.h>
•Every C program contains one function
named main
•The body of the main function is enclosed
by braces, { }
Spring 2001
EG280 CS for Engineers
4
Program Structure - continued
• The main function contains two types of
commands: declarations and statements
• Declarations and statements are required
to end with a semicolon (;)
• Preprocessor directives do not end with a
semicolon
• To exit the program, use a return 0;
statement
Spring 2001
EG280 CS for Engineers
5
First Program
/**********************************************/
/* Program chapter1: compute the sum two numbers
*/
/*
*/
#include <stdio.h>
int main(void)
{
/* Declare and initialize variables. */
double number1 = 473.91, number2 = 45.7, sum;
/* Calculate sum. */
sum = number1 + number2;
/* Print the sum. */
printf(“The sum is %5.2f \n”, sum);
/* Exit program. */
return 0;
}
Spring 2001
EG280 CS for Engineers
6
Constants and Variables
•A constant is a specific value that does not change
during execution.
•A variable is a memory location that is assigned a name
or an identifier.
•An identifier is used to reference a memory location.
•Rules for selecting a valid identifier
must begin with an alphabetic character or underscore
may contain only letters, digits and underscore (no
special characters such as * + - / ( ), and others )
case sensitive (pi and Pi and PI are unique names)
cannot use keywords as identifiers (main, if, int, …)
Spring 2001
EG280 CS for Engineers
7
C Data Types
C supports three basic types of data:
integer – used to represent integer values
examples: 10 -128 1256
character – used to represent coded (i.e. ASCII)
characters:
1
a
A
/

(I ‘’ my wife; I ‘’ my kids; I ‘’ my dog.)
floating point – used to represent scientific values
examples: 3.1415927 -0.907E-12
In addition, special modifiers can be applied to restrict the
range of values or to increase the precision.
For example, unsigned integers have positive (or zero)
value only, and double precision floating point values can
represent very small or very large scientific values.
Spring 2001
EG280 CS for Engineers
8
C Data Types - Integer
Type Name Length (bytes) / Range1
short
2
/ -32768 to +32767
int
2
/ -32768 to +32767
long
4
/ -2,147,483,648 to + 2,147,483,647
unsigned short
unsigned int
unsigned long
2
2
4
/ 0 to +65535
/ 0 to +65535
/ 0 to + 4,294,967,295
1The
actual length and range is system dependent and may vary
from environment to environment. However, these are fairly
typical values for many implementations.
Spring 2001
EG280 CS for Engineers
9
C Data Types - Character
Type Name Length (bytes) / Range
char
1
/ -127 to +128
unsigned char
1
/ 0 to +255
char data is intended to be used to hold character
(ASCII) codes. However, char variables can be
manipulated (added, subtracted, multiplied, …) just like
int data.
Spring 2001
EG280 CS for Engineers
10
C Data Types – Floating Point
Type Name Length (bytes) / Range1
float
4
/ 3.4E38 (7 digits of precision)
double
8
/ 1.7308 (15 digits of precision)
long double 10 / 1.2E4932 (19 digits )
1
Ranges are approximate since there is not an exact conversion
from binary to decimal floating point.
Spring 2001
EG280 CS for Engineers
11
Symbolic Constants
• Defined with a preprocessor directive
• Compiler replaces each occurrence of
the directive identifier with the constant
value in all statements that follow the
directive
• Example
#define PI 3.141593
Spring 2001
EG280 CS for Engineers
12
Assignment Statements
• Used to assign a value to a variable; General
Form:
identifier = expression;
• Example 1:
double sum = 0;(sum  0)
• Example 2:
int x;
x=5;
(x  5)
• Example 3:
char ch;
ch = ‘a’;
Spring 2001
(ch  0x61 = 0b01100001)
EG280 CS for Engineers
13
Assignment Statements - continued
• Example 3
int x, y, z;
x=y=0;
z=2;
x  0 and y  0
z2
• Example 4
y=z;
y2
Spring 2001
EG280 CS for Engineers
14
Arithmetic Operators
• Addition
• Subtraction
• Multiplication
• Division
• Modulus
+
*
/
%
– Modulus returns remainder of division
between two integers
– Example: 5%2 returns a value of 1
Spring 2001
EG280 CS for Engineers
15
Integer Division
• Division between two integers results in an
integer.
• The result is truncated, not rounded
• Example:
5/3 is equal to 1
(5./3 = 5/3. = 1.6667)
3/6 is equal to 0
(3./6 = 3/6. = 0.5)
Spring 2001
EG280 CS for Engineers
16
Priority of Operators
1 Parentheses
Inner most first
2 Unary operators Right to left
(+ -)
3 Binary operators Left to right
(* / %)
4 Binary operators Left to right
(+ -)
Spring 2001
EG280 CS for Engineers
17
Increment and Decrement Operators
• Increment Operator ++
post increment z = x++;
pre increment
z = ++x;
• Decrement Operator -post decrement z = x--;
pre decrement z = --x;
Spring 2001
EG280 CS for Engineers
18
Abbreviated Assignment Operator
operator
+=
-=
*=
/=
%=
Spring 2001
example
equivalent statement
x += 2;
x -= 2;
x *= y;
x /= y;
x %= y;
x = x+2;
x = x-2;
x = x*y;
x = x/y;
x = x%y;
EG280 CS for Engineers
19
Precedence of Arithmetic and Assignment
Operators
Precedence
1
Operator
Parentheses: ()
Associativity
Innermost first
2
Unary operators
+ - ++ -- (type)
Right to left
3
Binary operators
*/%
Left ot right
4
Binary operators
+-
Left ot right
5
Assignment
operators
= += -= *= /= %=
Right to left
Spring 2001
EG280 CS for Engineers
20
Standard Output
• printf() Function
–prints information to the screen (standard output
device)
–requires two arguments
• control string
• conversion specifier
• Example
double angle = 45.5;
printf(“Angle = %.2f degrees \n”, angle);
Output: Angle = 45.50 degrees
Spring 2001
EG280 CS for Engineers
21
Standard Input
• scanf() Function
– inputs values from the keyboard
– required arguments
• control string
• memory locations that correspond to the
specifiers in the control string
• Example:
double distance;
char unit_length;
scanf("%1f %c", &distance, &unit_length);
 It is very important to use a specifier that is
appropriate for the data type of the variable
Spring 2001
EG280 CS for Engineers
22
TABLE 2.7 Conversion Specifiers
for Input Statements
Variable Type
Specifier
Integer Values
int
%i, %d
short
%hi, %hd
long int
%li, %ld
unsigned int
%u
unsigned short
%hu
unsigned long
%lu
Floating-Point Values
float
%f, %e, %E, %g, %G
double
%lf, %le, %lE, %lg,
%lG
long double
%Lf, %Le, %LE, %Lg,
%LG
Character Values
char
%c
Spring 2001
EG280 CS for Engineers
23
Practice!
Assume that the integer variable sum contains the value 65,
the double variable average contains the value 12.368 and
that the char variable ch contains the value 'b'. Show the
output line (or lines) generated by the following statements.
printf("Sum = %5i; Average = %7.1f \n", sum, average);
printf("Sum = %4i \n Average = %8.4f \n", sum, average);
printf("Sum and Average \n\n %d %.1f \n", sum, average);
printf("Character is %c; Sum is %c \n", ch, sum);
printf("Character is %i; Sum is %i \n", ch, sum);
Spring 2001
EG280 CS for Engineers
24
Math Functions
fabs(x)
Absolute value of x.
sqrt(x)
pow(x,y)
Square root of x, where x>=0.
Exponentiation, xy. Errors occur if
x=0 and y<=0, or if x<0 and y is not an
integer.
ceil(x)
Rounds x to the nearest integer toward 
(infinity).
Example, ceil(2.01) is equal to 3.
floor(x)
Rounds x to the nearest integer toward -
(negative infinity).
Example, floor(2.01) is equal to 2.
Spring 2001
EG280 CS for Engineers
25
Math Functions – continued
exp(x)
Computes the value of ex.
log(x)
Returns ln x, the natural logarithm of x to the
base e. Errors occur if x 0.
Returns log10x, logarithm of x to the base 10.
Errors occur if x 0.
log10(x)
Spring 2001
EG280 CS for Engineers
26
Trigonometric Functions
sin(x) Computes the sine of x, where x is in radians.
cos(x) Computes the cosine of x, where x is in radians
tan(x) Computes the tangent of x, where x is in radians.
asin(x) Computes the arcsine or inverse sine of x, where x
must be in the range [-1, 1]. Returns an angle in radians;
range = [-/2,/2].
acos(x) Computes the arccosine or inverse cosine of x,
where x must be in the range [-1, 1]. Returns an angle in
radians; range = [0, ].
atan(x) Computes the arctangent or inverse tangent of x.
The Returns an angle in radians in the range [-/2,/2].
atan2(y,x) Computes the arctangent or inverse tangent of
the value y/x. Returns an angle in radians; range = [-, ].
Spring 2001
EG280 CS for Engineers
27
Character Functions
toupper(ch) If ch is a lowercase letter, function returns the
corresponding uppercase letter; otherwise, it returns ch
isdigit(ch) Returns a nonzero value if ch is a decimal digit;
otherwise, it returns value of zero.
islower(ch) Returns a nonzero value if ch is a lowercase
letter; otherwise, it returns value of zero.
isupper(ch) Returns a nonzero value if ch is an uppercase
letter; otherwise, it returns value of zero.
isalpha(ch) Returns a nonzero value if ch is an uppercase
letter or a lowercase letter; otherwise, it returns zero.
isalnum(ch) Returns a nonzero value if ch is an alphabetic
character or a numeric digit; otherwise, it returns zero.
Spring 2001
EG280 CS for Engineers
28