C Programming
Definitions:
Overflowed: A computation that lacks the storage to represent its result has overflowed.
Modular arithmetic: Any device that behaves like an odometer
Unsigned Overflow: Unsigned numbers behave similarly to the decimal
odometer examples given that both represent only non-negative values.
Given that unsigned interpretations can't hold negative values,the
discontinuity again sits between the maximum value and zero.
Carry in: increments the value of the result by carrying one into Shortcut for
detecting unsigned overflow for addition and subtraction:
the least significant bit of the arithmetic operation. Is only set to 1 for
subtraction as part of the negation procedure.
Carry out: Shortcut for detecting unsigned overflow for addition and subtraction:
is a carry from the most significant bit in the result of the computation
Code Syntax:
// #include <(stdio.h)>
// (\t) print tabs
// (\n) newlines
// (%d) placeholder for an integer value or decimal value (int, short, char)
// (%s) placeholder for a string value
// (%g) placeholder for a float (or double) value
// (%c) placeholder for printing a character value
EX
// code
// char ch;
// ch = 'A';
// printf("ch value is %d which is the ASCII value of %c\n", ch, ch);
// ch = 99;
// printf("ch value is %d which is the ASCII value of %c\n", ch, ch);
Output:
ch value is 65 which is the ASCII value of A
ch value is 99 which is the ASCII value of c
C uses scanf to read in an int value and to store it at the location in memory of an int
program variable (for example, &num1)
// #include <(stdio.h)>
// int main() {
// int num1, num2;
//
// printf("Enter a number: ");
// scanf("%d", &num1);
// printf("Enter another: ");
// scanf("%d", &num2);
// printf("%d + %d = %d\n", num1, num2, (num1 + num2));
// return 0;
}
Output:
Enter a number: 30
Enter another: 67
30 + 67 = 97
Notes:
The scanf function skips over leading and trailing whitespaces as it reads in a numeric value.
C Programming Syntax
// a one-way branch:
if ( <(boolean expression)> ) {
<(true body)>
}
// a two-way branch:
if ( <(boolean expression)> ) {
<(true body)>
}
else {
<(false body)>
}
Boolean Values:
zero evaluates to false
nonzero (any positve or negative value) evaluates to true
The for loop evaluation rules are:
Evaluate initialization one time when first entering the loop.
Evaluate the boolean expression. If it's false, drop out of the floor loop
Evaluate the statements inside the loop body
Evaluate the step expression
Repeat from step 2
Functions that do not return a value should specify the void return type.
Arguments to C functions are passed by value: each function parameter is assigned
the value of the corresponding argument passed to it in the function call by the caller.
Arrays:
Ordered collection of data. The number of char/int/doubles stored in the string is fixed
or set/defined when the array is first created.
The array is traversed by indexing and indexing starts with 0. Also need to specify the
type when first initializng the array
Strings in C end with the null character \0 (null terminated)
More Definitions:
scope: the set of program code blocks in which a variable is bound to (associated with) a
program memory location and can be used by program code.
address space: represents the storage locations for everything it needs in its execution
Parts of Program Memory:
Code: function instructions stored here
Data: global variables stored here
Heap: dynamically allocated memory, grows as program allocates memory
Stack: local variables and parameters stored here; grows as program calls functions;
shrinks on return from function.
Pointers: implement functions whose parameters can modify values in the caller's stack frame;
dynamically allocate (and deallocate) program memory at runtime when the program needs it.
efficiently pass large data structures to functions; create linked dynamic data structures;
interpret bytes of program memory in different ways.
Pointer variable: stores the address of a memory location in which a value of a specific type
can be stored.
"Pass by pointer" parameters, for writing functions that can modify their argument's value
through a pointer parameter.
Dynamic Memory Allocation: dynamically allocating arrays when the array size is unknown when
first running the program.
Use the pointer variable: the dereference operator (*) follows a pointer variable to the location
in memory that it points to and accesses the value at that location.
Notes: 1/19/2023
How to represent numbers on a computer? Signed Magnitude; Two's Complement
Conditional Statements:
C: No boolean data type; instead we use the following: 0 is False; 1 is True
EX:
int num = 10;
if (num == 10) // This is True
Loops
Functions
Arrays and Pointers