CSCI 220
break statement
causes an exit from a loop and is available within the while, do while, for, and switch statements
exit(1)
causes the program to abort
%c
character format
%s
character string format
relational operator
comparison symbol showing a relationship between two expressions
Function Definition
consists of comments , a header, and a body
Hexadecimal to Binary
cover each hex digit in the number into its decimal equivalent
Binary to Hexidecimal
cover each nibble (4 bits) of binary number into its hex equivalent
implicit conversions
data types on the right side of an expression are converted to the data type on the left side
function prototype
declares the function so that the compiler knows about it, provides a complete description of the function interface. Provides the function return value, function name, and parameters
stdlib.h
defines a few mathematical functions in the standard library
#define directive
defines a macro constant name and value whose name is automatically replaced by the macro constant value through the program file. ex. #define KMS_PER_MILES 1.609
scope of an identifier
determines where that identifier can be referred to in the program code
if-esle statement
double alternative decision statement
binary (base 2)
each digit represents a unique power of two
%ld
long decimal
%o
octal format
"r" mode
opens the named file for reading only from the beginning of the file
"w" mode
opens the named file for writing from the beginning of the file (overwrites original file)
"a" mode
opens the named file for writing from the end of the file (does not overwrite original file)
putchar()
outputs a single character to a standard output (terminal window). arguments should be one individual character either variable or constant
file I/O statements
performed using getc() and putc(). each takes an extra argument which identifies the file pointer to be used for input or output
Examples of function
printf(), scanf(), isupper(), tolower()
fopen()
program must open a file before it can be accessed through this function. fopen() returns the required file pointer
Ternary operator
'?' is the only ternary operator in C, i.e. it requires 3 operands: the expression being tested, the two alternative outcome expressions
character size
1 byte (8 bits). A character string is n+1 bytes where n is the number of characters in the string.
3 steps of an algorithm
1. Algorithm must have a finite number of instructions or steps. 2. Each instruction is well defined (not ambiguous). 3. The algorithm eventually halts
Six steps to good programming habits
1. Analyze the problem. 2. Develop an algorithm. 3. Write code for the program. 4. Run the program. 5. Test the results. 6. Document the program
Steps to create a user-defined function
1. Declare the function, 2. Write the function definition, 3. Write the call(s) to the function
Precedence of operators
1. Highest 2. Unary 3. Multiplicative 4. Additive 5. Shift 6. Relational 7. Equality 8. Conditional 9. Assignment 10. Comma
file pointer
C communicated with files using a data type called a file pointer. a file pointer called fp is declared in a statement like: FILE *fp;
Decimal to Binary
Continually divide the number by two and record its remainder (0 or 1). The binary is the number recorded in reverse order
Decimal to Hexadecimal
Divide the number by 16 and store the remainder. print the remainders in reverse order
Compiler Directives
Handled by the C preprocessor as it makes its first pass through your program file
Unsigned/signed binary numbers
If a binary number is unsigned, use the algorithm for binary to decimal. If the number is signed and the leftmost digit is a 0, then use this same algorithm. If the number is signed and the leftmost digit is a 1 then take the two's complement and the use the algorithm and make the decimal you receive negative.
operations on mixed data types
If you perform binary operations on two different data types, the compiler will convert the data type that requires the fewer number of bits to the data type requires the greatest number of bits for storage
Binary to Decimal
Multiply each digit of the binary number by increasing powers of two and add the results
function included in math.h
Power Fnctns, Absolute Val Fnctns, Nearest Integer and Remainder Fnctns, Exponential and Logarithmic Functions, Trig Functions
topdown design
State the problem, subdivide the problem into major subtasks, divide subtasks into smaller tasks called modules, repeat process until each module is one that is easily solved and trivial to implement in C
assignment operator
assigns the value of the operand on the right hand side to the operator on the left hand side
loop control condition
a logical expression that evaluates to either true or false
putc(c, fp);
a macro which places the character value of c in the file pointed to by fp. It returns the int value of the character written. note that putchar(c) is a macro defined a putc(c, stdout). Function prototype: int putc(int c, FILE *fp);
c = getc(fp)
a macro which retrieves the next character from the file pointed to by fp. The value of the character is returned as an int. Function prototype: int getc(FILE *fp);
Function
a subprogram that carries out a particular task. Its purpose is to group together a related set of statements that performs some well-defined step in an algorithm.
Text File
a text file is a sequence of characters terminated by an end-of-file marker
Numeric escape code
allow any character to be expressed by writing that character as its octal (base 8) encoding in the target character set
decision statements
allow programs to follow different courses of action or execute different tasks depending on the data values
repetition statement
allows you to specify that an action is to be repeated while a certain condition remains true
variable
an object that can some different values during the execution of a program. Every variable has a name and value
switch statement
another multiple choice control structure. it can only be used in certain cases where only one variable is tested, the variable must be an integral type
fclose()
empties the buffer and breaks all connections to the indicated file. EOF is returned if fp is not associated with a file. If you don't close the file, the system will close it for you but it is always better to do it to make sure the data is properly saved
semantic error
errors in the meaning or implementation of the program
run-time error
errors that occur when the computer attempts to perform an illegal operation or instruction
syntactic error
erros in the programming language syntax
Logical operator evaluation
evaluated left to right and evaluation stops as soon as the truth of falsehood of the result is known
comma operator
evaluates the first subexpression and then the second
numerical data types
ex. int, double, float, char
initialization
first part of a for loop, it is run before the loop is entered
%f
floating point
nested for loop
for loop within a for loop
scanf()
function in the standard library which allows you to input values into a program from standard input (keyboard)
User defined header files
header files that you create that may define information specific to your program. The double quotes tell the C preprocessor that the included file is user defined instead of a C standard Library header file. C assumes the user define header is in the same directory as where the program is compiled unless otherwise specified
math.h
header that defines various mathematical functions in the math library. All functions in this library take double as an argument and return double as a result
%x
hexadecimal format
global scope
identifiers that are declared before the main () function. These can be referred to anywhere in the program code
local scope
identifiers that are declared within a specific function. They are only visible within that specific function. i.e. function parameters
%d
integer or decimal (base 10 format)
redirection request
puts the program's standard output into a file instead of the terminal
file modes
read, write, and append
Flow of program execution
refers to the order in which the C code is executed. Execution begins at the first statement in main() and statements are executed one after another from first to last
counter controlled loop
repeats a series of executable statements a specified number of times using a variable to count the number of loo iterations completed
floats/doubles
represent numbers that have a fractional component (double is a floating data type that uses double the storage). C guarantees that sizeof(float)<=sizeof(double)
integers (int)
represent whole numbers that do not have a fractional component
sizeof()
returns an integer that represents the number of bytes used to store an object on the host computer. C guarantees that sizeof(short)<=sizeof(int)<=sizeof(long)
getchar()
returns the next character form standard input (keyboard) and returns it as a function return value. If there is no more input then EOF is return instead
%e
scientific notation format
test
second part of the for loop, it tests the loop control variable and the loop is exited when the test is no longer true
do while loop
similar to the while loop except that the test occurs at the end of the loop body which guarantees that the loop is executed at least once before continuing
loop control variable
single variable in a loop control condition
Comments
statements that clarify the meaning and content of your program file
Stepwise refinement
subdivision of tasks
Hexadecimal to decimal
take the reverse order of the hexadecimal number and substitute the values for the letters. then multiply by increasing powers of 16 and add the results
Negative binary numbers
take the two's complement by flipping all bits and adding 1
Angle brackets '<>'
tell the C preprocessor the the included file is a C standard Library header file
accessing text files
text file is accessed via a pointer to a structure that is defined in the standard header file stdio.h as FILE
Calls to the function
the calls cause the function to execute and return values. There may be many calls with different arguments
explicit conversions
the cast operator converts (casts) the value of a variable into a type that is different from the original type of the variable
working directory
the directory where the program was run from
update
third part of the for loop. Typically a statement that is executed every time the loop body is completed, usually an increment or a decrement
increment/decrement
unary operators (require only one operand) adds one to its operand and subtracts one from its operand respectively
%u
unsigned decimal
%lu
unsigned long decimal format
sentinel value
used when we don't know when we want the loop to stop. it indicates "end of data entry". It is a user defined control variable that the user enters when they want the loop to end
decimal base 10
uses 10 digits to represent values
Hexidecimal
uses 16 digits to represent 16 base values
continue statement
utilize if you wan to go back to the top of the loop when something unexpected happens
constants
values that cannot be modified during the execution of a program (can be a number, character, or character string)
Identifiers
variable or function names that are referred to in the program code
nested if statement
whenever an if statement is texted within another if statement, put braces around the nested if statement.
for statement
works well where the number of iterations of the loop is known before the loop is entered