Week 2: C Fundamentals Flashcards
What are the two main ways of changing programs to machine language?
Compilers and Interpreters
What is source code?
Code written in a programming language by a developer
What is a compiler?
A computer program that transforms code from one format to another (needs a different compiler depending on what system you are using)
What is an interpreter?
A program that translates and executes code, usually translates and executes source code line by line. There is no object code written as an intermediary
What is primary and secondary memory?
Primary memory: mechanism to present memory to the CPU, is temporary
Secondary memory: stores memory, is permanent
What is C?
A general purpose, procedural computer programming language supporting structured programming, lexical variable scope, and recursion, with a static type system
Who developed C?
Bell Labs: Ken Thompson, Dennis Ritchie, and others
Why was C originally developed?
To design and support Unix operating system
Benefits of C:
- Intended as a language for programmers
- Powerful and efficient
- Structured
- Standardized
What three steps are necessary for a program to be executed?
Preprocessing, compiling, and linking
What is preprocessing?
Finds and deals with processing commands (directives) starting with a #
What is compiling?
Translating the program into machine instructions
What is linking?
Combining the object code produced by the compiler with any additional code needed to yield a complete executable program
What is the UNIX compile command and its syntax? (For C)
gcc
Syntax: { gcc -o nameOfExecutable program.c }
What are the three key language features of C?
- Directives
- Functions
- Statements
What do directives always begin with?
#
Do directives end with a semicolon or any other special marking?
NO
What are directives?
Commands intended for the preprocessor
What are functions?
A series of statements that have been grouped together and given a name
What does a function end with
Usually a return statement
What is the mandatory function?
main
What should the main function return?
A status code, 0 indicates normal termination
What is a statement?
A command to be executed when the code runs
C requires each statement to end with a
; semicolon
What are the two formats for comments?
/* comment */
or
// comment // more comment
Each variable must have a
type
All variables must be… before use
Declared
When main declares variables, they must…
precede statements
Variable initialization is also known as
Defining
What does this do?
int height = 8, length = 12, width = 10;
Declares three ints and initializes them
What does this do?
int height, length, width = 10;
Declares three ints but initializes only width
Initializing a float requires what? Where?
an f at the end of the constant
profit = 2150.48f;
Macro definitions are typically
define RECIPROCAL_OF_PI (1.0f / 3.14159f)
Containing only upper-case letters
Identifiers (name for a variable) must
Begin with a letter or underscore
Identifiers can contain:
Can contain letters, digits, and underscores ONLY
Are identifiers case-sensitive?
Yes
Keywords must:
contain only lowercase letters
What are the steps for compiling a program?
Step 1: Write the source codes and header files
Step 2: Pre-process the source codes according to the preprocessor directives
Step 3: Compile the pre-processed source codes into object codes
Step 4: Link the compiled object codes with other object codes to produce the executable code
Step 5: Load the executable code into computer memory
Step 6: Run the executable code
What is the syntax of a switch statement?
switch (input) { case 1: statement; break; case 2: statement; break; default: statement; break; }
Default is the equivalent of an “else”
What is the difference between a while loop and a do-while loop?
While loops pre-test the condition
Do-while loops post test the condition
What is the order of events in a for-loop?
a-b
c-b
c-b
c-b
What is the format of the conditional ? operator
(condition) ? expression1 : expression2
If the condition evaluates to true, expression1 is executed, if the condition evaluates to false, expression2 is evaluated