Академический Документы
Профессиональный Документы
Культура Документы
float
fractional parts, positive and negative
keyword: float
float height;
height = 1.72;
sign
exponent
mantissa
Type
short
32,768 .. 32,767
unsigned short
0 .. 65,535
int
2,147,483,648 .. 2,147,483,647
unsigned
0 .. 4,294,967,295
long
2,147,483,648 .. 2,147,483,647
unsigned long
0 .. 4,294,967,295
ASCII Code
American Standard Code for Information Interchange
particular code that specifies the integer representing each char
value.
The digit characters '0' through '9' have code values of 48 through
57 (decimal).
uppercase letters have the decimal code values 65 through 90
Lowercase letters have the decimal code values 97 through 122
the printable characters have codes from 32 (code for a blank or
space) to 126 (code for the symbol ~ ).
#define
You may also associate constant using #define preprocessor directive
#include <stdio.h>
#define PI 3.14
int main(void)
{
double height, radius, base, volume;
printf(Enter the height and radius of the cone:);
scanf(%lf %lf ,&height,&radius);
Input/Output Operations
Input operation
an instruction that copies data from an input device into
memory
Output operation
an instruction that displays information stored in memory to
Input/Output Functions
A C function that performs an input or output operation
A few functions that are pre-defined in the header file stdio.h
such as :
printf()
scanf()
getchar() & putchar()
marks.
sequence.
Side Effect:
Escape Sequence
Escape Sequence
Effect
\a
Beep sound
\b
Backspace
\f
\n
\r
Carriage return
\t
Tab
\v
Vertical tab
\\
Backslash
sign
\o
Octal decimal
\x
Hexadecimal
\O
NULL
Conversion
Specifier
%d
%i
%o
%u
%x
%X
%f
%e
%E
10
11
12
13
%g
%G
%c
%s
Output Type
Output Example
76
76
134
76
9c
9C
76.0000
7.6000e+01
7.6000E+01
76
76
7
76'
value
int age;
printf(Enter your age: );
scanf(%d, &age);
Common Conversion Identifier used in printf and scanf
functions.
printf
int
float
double
char
string
%d
%f
%lf
%c
%s
scanf
%d
%f
%lf
%c
%s
Input/Output
%d and %lf are examples of format specifiers; they are placeholders for values to be
displayed or read
Placeholder
Variable Type
Function Use
%c
char
printf / scanf
%d
int
printf / scanf
%f
float or double
printf
%f
float
scanf
%lf
double
scanf
%e
float or double
Input/Output
Escape
sequence
Meaning
Result
\n
New line
\t
Horizontal tab
\"
Double quote
%%
Percent
Operators
Arithmetic operations
Unary operators: +,
Right Associative
x = 23
p = +4 * 10
Execution from left to right, respecting parentheses rule, and then precedence
rule, and then associative rule (next page)
46 / 15 / 2 3 / 2 1
19 % 7 % 3 5 % 3 2
int n;
n = 9 * 0.5;
Operators
Name
Number of
operands
Position
Associativity
Parentheses
Unary
Prefix
Left to right
Parentheses
Unary
Postfix
Left to right
Positive sign
Unary
Prefix
Right to left
Negative sign
Unary
Prefix
Right to left
++
Post-increment
Unary
Postfix
Left to right
--
Post-decrement
Unary
Postfix
Left to right
++
Pre-increment
Unary
Prefix
Right to left
--
Pre-decrement
Unary
Prefix
Right to left
Multiplication, division,
remainder
Binary
Infix
Left to right
Addition, subtraction
Binary
Infix
Left to right
Assignment
Binary
Infix
Right to left
*, /, %
+, =, +=, -=,
*=, /=, %=
Precedence
Operators
m
p
n
q
r
10/4;
means
10/4;
means
10/4.0; means
10/4.0;
means
-10/4.0; means
=
=
=
=
=
Type Casting
Use a cast operator to change the type of an expression
syntax:
(type) expression
int aa =
float pp
int
nn
float qq
6; float ff = 15.8;
= (float) aa / 4;
means
= (int) ff / aa;
means
= (float) (aa / 4); means
program
Three (3) kinds of errors :
Syntax Error
a violation of the C grammar rules, detected during
Structure Charts
documentation tool
shows the relationships among the subproblems of a
or screen.
basic components:
a circle
parallel lines
a base line intersecting lines
Draw a figure
DESIGN : Divide the problem in three sub problems
INITIAL ALGORITHM
Draw a circle.
Draw a triangle.
Draw intersecting.
ALGORITHM REFINEMENTS
Step 2 Refinement
2.1 Draw intersecting lines.
2.2 Draw a base.
Structure Chart
rocket
Analysis:
Design:
male
female
Draw 3
Figures
Draw Rocket
Ship
Draw Triangle
Draw
Rectangle
Draw Male
Stick Figure
Draw
Inverted V
Draw Circle
Draw
Rectangle
male
female
Draw Female
Stick Figure
Draw
Inverted V
Draw Circle
Draw Triangle
Draw
Inverted V
Modular Programming
Break a large problem into smaller pieces
Smaller pieces sometimes called modules or subroutines or
procedures or functions
Why?
Helps manage complexity
a = sqrt(9.0);
Functions
The building blocks of a C program
Youve used predefined functions already:
main()
printf(), scanf(), pow()
User-defined functions
Your own code
In combination with predefined functions
f ( x) = x 2 + 2 x + 3
What is f(2)?
f (2) (2) 2 + 2(2) + 3 4 + 4 + 3 11
f (2) is 11
Function
Returned
value
f ( x)
11
Functions in C
resultType functionName(type1 param1, type2 param2, )
{
body
}
// functionName
If no result, resultType should be void
Warning if not!
If no parameters, use void between ()
Library Function
Goal to write error-free code
Code reuse
Cs standard math library defines a function named sqrt -
C Library Function
Function
Header
Purpose: Example
abs(x)
Argument(s) Result
int
int
argument: if x is 5 , abs(x) is 5
ceil(x)
<math.h>
double
double
cos(x)
<math.h>
double
(radians)
double
double,
double
double
pow(x,y) <math.h>
sqrt(x)
<math.h>
double
double
log(x)
<math.h>
double
log10(x)
<math.h>
double
double
Using Functions
Let int
+ d;
f(pi*pow(r,2), b+c)
Using Functions
Let int
+ d;
f(pi*pow(r,2), b+c)
Result of f is added to d
Function definition
Function Prototype
a Function Prototype
Header files
Include
#include <foo.h>
Search
#include "foo.h"
Search
Descriptive
Statements
Variable declaration
Operations
Return value (if any)
Functions - Example
Function prototype
Like a variable declaration
Tells compiler that the function will be defined
later
Helps detect program errors
Note semicolon!!
Function definition
See previous slide
Note, NO semicolon
Function return
return statement terminates execution of the
current function
Control returns to the calling function
if return expression;
Function call
#include <stdio.h>
/* function prototype */
double product(double x, double y);
int main()
{
double var1 = 3.0, var2 = 5.0;
double ans;
ans = product(var1, var2);
printf("var1 = %.2f\n"
"var2 = %.2f\n",var1,var2);
printf("var1*var2 = %g\n", ans);
}
/* function definition */
double product(double x, double y)
{
double result;
result = x * y;
return result;
}
Creating Function
#include <stdio.h>
#include <math.h>
#define PI 3.14159
Function
definition
int main(void) {
// identical portion omitted for brevity
// compute weight of a single washer
Calling circle_area()
twice.
Creating Function
character
Return type is void if function does not need to return any value
Function body: code to perform the task; contains a return statement if return
Return type
Parameter
Function body
Programming style
#include <stdio.h>
#include <math.h>
#define PI 3.14159
Function prototype
double circle_area(double);
int main(void) {
// identical portion omitted for brevity
// compute weight of a single washer
Function
definition