CSC 230 NCSU Final
standard library
#include <stdio.h> I/O functions are provided by the library
conversion specification
%6.2f
precision
%6.2f The number after the decimal is how many decimal places the number can go to
Floating Point Representation
+7.625 * 10^2 1) Sign for the value 2) Mantissa 3) A (signed) exponent
short circuiting
- && and II operators - stop evaluating early if the outcome is determined
Static initialization
- Are initialized at program start-up - they automatically get initialized at zero - must initialize them to a constant expression
make Variables
- CC : the default compiler to use - CFLAGS : the default flags to compile with - LDLIBS : options for the linker (libraries, e.g., -lm)
Functions
- Can take any number of parameters - including no parameters C will implicitly convert actual parameters to the formal parameter types
IEEE Representation
- Normalize the mantissa to the point just after the 1 -> free bit, bc we don't actually store the bit Store the exponent in binary 127 bigger than the actual number given after normalizing the exponent Stored as sign, exponent and then mantissa
overflow during arithmetic
- On addition -> carry out of the most significant bit -On subtraction -> a borrow into the most significant bit
Auto/Local Scope
- Variable declared inside a function in scope from declaration to the end of the surrounding block
stdbool.h
- can use bool - false : preprocessor constant for 0 - true: preprocessor constant for 1
Revision Control System
- multiple versions of the project It can provide a backup to a remote server provides a convenient, secure access provides conflict control between developers
auto storage class
- storage for local variables is automatically allocated called auto storage class or local/stack variables - if you don't initialize- get what ever is left in the stack memory - initialization is evaluated each time control passes the declaration
Statically Typed
- type of variable that cannot be changed while the program runs
Overflow situations
-Compute a value that's outside the range of type that's holding it -Copy from a wider type to a narrower one Copy between signed and unsigned types of the same width
Signed
-is the default, means can have negative numbers
Computing the Twos Complements
1) Flip all the bits 0-> 1 2) then add one
Rules for Arithmetic Conversions
1. Use the wider of the two types if it's good enough. 2. Don't use types smaller than int. 3. If operands are the same size but mixed signedness, use unsigned. If either value is a real If just one operand is real -> convert to that type. If both are real types -> convert to the wider one. Lower ranks get promoted to at least an int If you have a signed parameter and an unsigned parameter: Promote to the signed type if can store every value the unsigned type can. Otherwise, use the unsigned version of the higher-ranked type.
row major order
2-d arrays are laid out in a striaght line rather than rows and columns
Executing C Programs
A C Compiler generates native machine code for the target processor. C file - compilation unit object file - .o file compilation unit generates one object file
header file
A file typically containing type, class and/or standalone function declarations, as well as constants, with little or no active code, designed to be included in code files that are clients of the declared types, classes or functions.
String
A sequence of characters stored in a one-dimensional array. - null terminator at the end
format string
A string passed to printf to specify the format of the output. Ex) "value: %6.2f\n"
Common Platform
Agreed system that is used to maintain similar language versions, line termination and conventions
comma operator
Also known as the sequential evaluation operator, an operator that causes a list of expressions to be evaluated in turn, from left to right.
sizeof()
An operator that estimates the size of a data object.
File scope
C functions aren't part of any class. They are defined at file scope
Object- Oriented
C is not object oriented
Procedural
C is procedural A program is a collection of procedures (functions) Focus on actions performed by these procedures
Weaknesses of C
C offers very little protection and security
constant expressions
C requires a value that can be determined at compile time -> compiler must be able to determine the value of the expression
Lexical Analysis
Compiler has to break the source into tokens
preprocessor directives
Ex) include statements Tells the compiler to get these files in order to understand the rest of the source code
GDB
GNU debugger a symbolic debugger -> lets us work with the names in our source code - need to use the g flag when you compile gcc -Wall -std=c99 -g myProgram.c -o myProgram
indirection
Instead of working directly with values, we work with the locations where values are stored.
Imperative Language
Like Java, it's an imperative language, focused on how a computation should be performed
Size Type Guarantees
Long may or may not have more capacity than int, but it cannot have less capacity
Variable Components
Name - legal identifier name Type -how the compiler interprets the contents of memory representing the variable scope- the section of code that has access to the variable storage class- how its stored and initialized
Signed Overflow Instances
Negative (10000) plus a negative (10000) equaling a number that starts with a 0 A negative minus a positive 10111000 - 01000100 01110100
sequence points
Places where we know side effects to the left will occur before evaluation continues ex) statement termination ; closing paranthesis ) after returing a value from a function after evaluation of all arguments to a function call && || ? ,
int putchar(int c)
Returns the character you just wrote or EOF if it can't
Steps in C Compilation
Source Code -> Preprocessing Expanded Source Code ->Lexical Analysis Tokens ->Parsing Parse Tree ->Code Generation Assembly Code ->Assembling Object Code ->:Linking Exectuable
int getchar(void)
Stdio.h provides a function Returns an int Doesn't take any parameters If there is no input - return EOF
field width
The ___ is the minimum number of character positions, or spaces, on the screen in which to print a value. Ex)%6.2f The 6 is the minimum field width
size_t
The unsigned integer type returned by the sizeof operator.
int scanf( const char *fmt, more...args )
Uses format string to scan for input, and matches it with the argument variables given to store scanf( "%d", &val ); need to use ampersand on the value variable- pass by reference returns the number of matches, EOF if it reaches end of file before matching Leaves parameter unchanged if it cannot match specifier
Global Variable Scope
Variable defined outside any function - has lifetime for the whole execution of the program -> storage class is static
Identifiers
Variable, function or other names An identifier consists of letters, digits or underscores Cannot start with a digit are case sensitive cannot be a reserved word
Linker
We can compile and link all at once. gcc -Wall -std=c99 main.c stuff.c or generate intermediate object files and link them together
goto ( really bad)
We can put a label before any statement in a function. myLabel: • Then, we can jump to that label when we need to. goto myLabel;
making an Array
When you declare an array, you get to specify - The type of its elements - How many elements it will have - How they will be initialized (if at all) int a[ 10 ]; C does no bounds checking for array access
Sign Extension
Wider signed integer to a narrower one -> just take the low order bits • Narrower signed integer to a wider one: Fill high-order bits with a copy of the leftmost bit.
Char/ASCII
a char variable can hold a smaller integer only one byte ASCII - comes from common platform
Stream
a file or device we can read or write 3 Streams: - Standard input - Standard output - Standard error
stack trace
a list of the function calls that got the program to this point
Preprocessor
a program that carries out preprocessor directives Operates on the source code before the compiler sees it Includes Headers and expands macros Lines starting with # are preprocessor directives
Linker
a program that combines the object program with other programs in the library, and is used in the program to create the executable code
hexdump
a shell command that can show you the exact sequence of bytes in a file hexdump -C someFile.txt
Character literal
a single character that appears in single quotes (') in a program - Gives the numeric code for any character
makefile
a text file format dependency and build instructions make- a tool for following these instructions
address-of operator : &
a unary operator that returns the address of its operand
function prototype
code that describes a function, allowing it to be invoked before it is defined with prototypes, we can define our functions in any order just needs type information, but you can include parameter names
commit
collect related changes put them into a repository defines the version in the repository
explicit type coversion
conversion through type cast has a very high precedence
rank
describes the relative size of integer types
storage class
determines lifetime, where its allocated, how its initalized global variables have a static storage class
underflow
falling below the smallest magnitude a floating point can hold
Declarative Languages
focus on what the program should compute rather than how it should compute it
Command for Building a C program
gcc -Wall -std=c99 X.c -o X
git commands
git add-> tells git about the set of changes you want to commit - staging the current version of a file to the index - git commit-> takes the contents of the index and adds the local repository git push -> local commits are now in the remote repository
buffer overflow
happens when a program writes into memory past the end of an array
component
header and implementation file together makes up a component
Definition
heres some thing, allocate space or write code for the compiler ex-> function definition
partial initialization
if you initialize just some of the array, the rest will be initialized to zero.
Multi -Dimensional Arrays
int table[ 3 ][ 4 ]; [3]- Outer dimension (rows) [4]- Inner Dimension (columns) With initialization, elements are initialized - row-by row, each row in curly brackets - and, left-to-right within each row. can let the first dimension be empty cannot be returned
Computing Array Size
len = sizeof( b ) / sizeof( b[0] );
break
lets us bail out of the innermost loop early
Continue
lets us skip the rest of the innermost loops current iteration
register storage class
local variables don't have to be stored on the stack - can only be used for local variables
repository
master copies of project files are maintained - all revision control systems have this
address
memory location
Redirecting Streams
myProgram < input_file.txt redirecting standard input from a file $ myProgram > output_file.txt redirecting standard output to a file
escape sequences
need to be entered with a \ to work
maximal munch
scanner works from let to right, grabbing the longest token it can
static
scope is not storage class variable that has the lifetime of a global variable, but the scope of a stack variable requires constant intialization expression
octal
split into groups of 3 base 8 int a = 0123 need the 0 in front to show compiler its octal
hexadecimal
split into groups of 4 base 4 int b = 0x 123
Makefile Syntax
target: prereq1 prereq2 ... shell-cmd1 shell-cmd2 target - something that can be built. prerequisites - indicating what the target needs and when it must be rebuilt main: main.o stuff.o gcc main.o stuff.o -o main main.o: main.c stuff.h gcc -Wall -std=c99 -c main.c stuff.o: stuff.c stuff.h gcc -Wall -std=c99 -c stuff.c
extern
tells the compiler about a something that is defined elsewhere just a declaration!
ternary operator
test ? expr1 : expr2 works like an if else statment
string literal
text in double quotes - AN array of character codes - ends with a null terminator at the end
implicit type conversion
the process by which a value is automatically converted to fit the memory location to which it is assigned
dependencies
the relationships between files we build and the files they depend on
Declaration
there's something like this out there, but it's defined elsewhere
shadowing
two variables can have the same name as long as they are declared in different scopes compiler decides by using the one declared in the narrowest surrounding scope narrower scope shadows the one in the wider scope.
private
using the static keyword on global variables marks it as invisible to the linker internal linkage
Exit Status
void exit( int status ); Can be used only inside of main Declared in stdlib.h header Any exit status you want never actually returns
nm
we can look inside an object or library
Twos Complement
what hardware normally uses to represent signed numbers leftmost bit tells you if the number is negative or non-negative