C - Hello, World Flashcards
What are the 3 general steps in creating an executable?
- Preprocessing
- Compiling
- Linking
What is preprocessing?
Before code is compiled it is given to a program called the preprocessor. The preprocessor edits and manipulates code, setting it up for the compiler. Typically the preprocessor looks for lines that begin with # and alters the code based on these directives.
What is compiling?
Preprocessed code is given to the compiler, a program that translates C statements into machine instructions.
What is linking?
A linker combines the object code produced by the compiler with any additional code needed to create a complete execuatable. This additional code includes things like library functions.
What are the three fundamental language features that C relies on?
- Directives
- Fuctions
- Statements
What is a function?
A series of statements grouped together and given a name
What does main return?
An integer status code
How do you start all c programs?
include
int main(void)
Why include a header file in a C program?
to use standard libraries
What are the steps for building C programs?
preprocessing, compiling, linking. (Using gcc usually executes all three steps automatically.)
in console:
>gcc -o hello hello.c
>./hello
What is pre-processing?
Pre-processing: processing directives,
modifying source file text
What is compiling?
Compiling: translating source code into
machine instructions (object code)
What is linking?
Linking: combining object code created
by compiler with other code to create a
single executable program
What are comments?
They are text for documentation purposes and are mapped to whitespaces before preprocessing. (Neither pre-processor nor compiler sees them.)
What is a variable
it has a name, a type and a value. It can be assigned values, do calculations and in-/output variable
What are control structures in C?
Two types:
Selection
* if statement
* switch statement
Loops
* while loop
* do while loop
* for loop
What does the Pre-processor do?
Removes comments
Modifies source code
according to preprocessor directives
Most importantly:
includes header files into
source code
What are the steps to compiling a program in C?
Step 1: Pre-Processor. Removes comments, reads pre-directives, includes header files. Output: Translation Unit,
Step 2: Compiler. Performs correctness checks, code optimizations. Output: assembler code (.s file),
Step 3: Assembler. Translates assembler code (.s file) into binary machine instructions/object code (.o file),
Step 4: Linker. Combines multiple object files into a single file, Output: exe file.
What is the use of a header guard in preprocessing?
Use: to avoid including a header file more than once, check if it has already been included.
Ex:
grandparent.c
#ifndef GRANDPARENT_H
#define GRANDPARENT_H
struct foo{
int member;
};
#endif
….
in parent.c
#ifndef PARENT_H
#define PARENT_H
#include “grandparent.h”
#endif
What is the difference between variable definition and declaration?
Variable declarations can be duplicated, have static name type binding done at compile time and specifiy identifier and type.
Variable definition: only one allowed, assigns fixed address to variable (at runtime?).
Definition and declaration of VARIABLE looks the same, possible: int a; but not so for functions.
What is initialization of a variable?
Initialization: assigns initial value to a variable, value not needed at compile time. Assigns scope-
What is scope?
Scope is the portion of program where name of variable (identifier) can be used to refer to a variable.
How to compile a file world.c in both gcc and clang?
gcc world.c -std =c90
clang world,c -std=c99
what are the different compiler options and what are their uses?
The different compiler options are
-o: Name your executable
-g: Add in debugging information
-Wall: Give warnings
-Wextra: Give additional warnings
-O, O2, O3: Compiler optimization and different levels of it.