Upload
brooklyn
View
43
Download
4
Embed Size (px)
DESCRIPTION
CISC105 – General Computer Science. Class 2 – 6/7/2006. User-Defined Identifiers (Variables). Syntax Rules: An identifier can consist of only letters, digits and underscores An identifier cannot begin with a digit A C reserved word cannot be used as an identifier - PowerPoint PPT Presentation
Citation preview
CISC105 – General Computer Science
Class 2 – 6/7/2006
User-Defined Identifiers (Variables)
Syntax Rules:1. An identifier can consist of only letters,
digits and underscores2. An identifier cannot begin with a digit3. A C reserved word cannot be used as an
identifier4. An identifier defined in a C standard
library should not be redefined (Advice – not a rule)
User-Defined Identifiers
• User-Defined Identifiers are case sensitive – Hello != hello != HELLO
• Accepted rule:– Constants use uppercase KMS_PER_MILE– Other identifiers in lowercase miles, kms– Separate words by using an underscore
per_capita_income
Data Types
• int : whole numbers in the range -32767 to 32767 – Why is this limited?
• double : contains and integral and fractional part – Larger values use Scientific Notation– 1.25E5 = 1.25e5 = 1.25 x 105
– Still have size limitation – all real numbers cannot be represented!
Data Types
• char : Represents an individual character – a letter, a digit or symbol– Represented by single quotes ‘1’, ‘a’, ‘A’, ‘ ‘– Do not enter a char as ‘z’ at a prompt– char first_initial = ‘m’;– You can compare and add char data types
but use with care! • char letter = ‘A’ + 32; Will return ‘a’;
Executable Statements - scanf
• scanf is an input function call that usually follows a printf prompt
• scanf(“%lf”, &miles)• & is the C address-of operator and tells scanf to where to
find the variable to store the information into• Using scanf for multiple input? Write scanf to input Bob?
Executable Statements – return(0)
• The return(0) statement returns control from the program back to the OS. The return value of 0 indicates an error free execution.
General Form of a C program
Program Style
• Do not write more than one command per line.
• Be consistent in use of spaces– /*This is a comment*/– /* This is a comment */
• Use program documentation to enhance the readability of your program
Program Style
• Every program should start with comments telling the reader:– The programmers name – Class and assignment info if appilicable– Date of the current version– Brief description of what the program does
Arithmetic Expressions
• + addition operator
• - subtraction operator
• * multiplication operator
• / division operator
• % remainder operator (modulus)
Arithmetic Expressions
• Mixed-type expressions: int + double = double
• Type cast – convert an expression to a different type by writing the desired type in paranthesis
• Unary operator (+, -) – requires one operand
• Binary operator – requires 2 operands
Arithmetic Expressions
• Rules for evaluating expressions• Parentheses• Operator precedence
– Unary– *,/,%– +,-
• Associativity – Unary – right to left (right associativity)– Binary – left to right (left associativity)
Arithmetic Expressions
Arithmetic Expressions
Writing Formulas in C
• Multiplication can be implied in a formula by writing the 2 items together (a=bc)– C requires the use of * operator a = b * c;
• Writing a formula for division we usually write the numerator and denominator on a separate line– C requires numerator and denominator on the same
line m = (y – b) / (x – a); Note: use parentheses to clarify the numerator and denominator.
ax
bym
Formatting Numbers in Output
• Formatting int is easy! Just add the number of characters you wish to display before the ‘d’ – printf(“The value is: %3d.\n”, this_value);
• Displays this_value with a minimum of 3 characters if this_value is larger than 3 characters it will print the entire value.
Formatting Numbers in Output
• Formatting a double variable in output is done by indicating the total field width and the number of decimal places desired in the format %n.mf (n can be zero)– printf(“The value is : %6.2f\n”, this_value);– If this_value was 120.567 the output would be
120.57 (rounding the decimal)
Interactive Mode vs. Batch Mode
• Interactive Mode – a mode of execution where the user responds to prompts by entering data
• Batch Mode – a mode of execution where the program scans data from a previously prepared data file– Input Redirection is achived in Unix / MS-DOS
by placing <filename at the end of the command line
A matter of Style
Output Redirection
• You can redirect output to file by using >filename
• You can use input and output redirection on the same command line:– myprog <mydata >myoutput
Program Controlled – I/O Files
Common Programming Errors
• Syntax Errors: Violation of C grammar rules – detected by compiler as it attempts to translate your program
• Run-Time Errors: Detected and displayed by computer during execution of program
• Undetected Errors: An execution error that does not prevent successful run but can lead to incorrect results.
• Logic Errors: Program follows a faulty algorithm
Syntax Error
Run-Time Error
Undetected Error
Error due to omission
Functions
We have already seen some functions
• From stdio.h – printf and scanf
• From math.h – sqrt and others (see table 3.1)
Functions
Why use functions?• Need to do a computation several times in a
program• Way to modularize a program
– Big problem -> smaller problems– Top-down Design
• Code Reuse– Use previous program to solve a bigger/slightly
different problem– Why reinvent the wheel – sqrt would be difficult to
write every time you need it – just use the math.h library!
Functions
• Procedural Abstractions: main program only contains a list of sub-functions
Aspects of a Function
• Declarations – tells the name of the function– what inputs are required – the type of output to expect
Note: Declarations are usually put in a .h file, but .h files are more advanced so we will have the declaration in same file as the main program.
Aspects of a Function
• Definition– Defines what the function does– Contains the actual code for whatever task
the function needs to program
• Use– When a function is used in another place in a
program– The same function can be used several times
House and Stick FigureWhat functions to write?
Structure Chart of Functions
Function Declaration and Main Program
Function Definitions
Function Use and Control Flow
Functions with Arguments
• Input Arguments: arguments used to pass information into a function program
Actual Argument = 135.68
Formal Parameter is rnum
Functions with Arguements
• Output Argument: arguments used to return results to the calling function
What would be returned by the following?double result = scale(25.0, 4);
Answer: 250,000
Function Notes
• Variables are local– Variables are not shared between functions– Variable names can be reused in functions
• Do not point to same memory space• Should only be used when referencing the same information
– Necessary variables must be passed a arguments
• Constant Variables are Global• Values of constants are unique and can be
accessed from all functions in the program
Function Notes
• Make sure you comment preconditions and postconditions– Precondition: condition assumed to be true
prior to a function call• N and m are defined, math.h is included
– Postcondition: condition assumed to be true after a function call
Function Notes
• Number of arguments in a function call must be equal to the formal parameters in the function definition!
• Order of Arguments in the function call must correspond to the order required in the formal parameters in the function definition
• Each argument in the function call must be of the same data type as the formal parameter in the function definition