Topic 2 - C Fundamentals Flashcards
Sime C programs have the form:
directives int main(void) { statements }
Every C program relies on 3 key language features:
1) Directives
2) Functions
3) Statements
Before a program can be executed, three steps are usually necessary:
1) Preprocessing
2) Compiling
3) Linking
Define: Preprocessing:
The preprocessor obeys commands that begin with # (known as directives)
Define: Compiling:
A compiler translates the program into machine instructions (object code)
Define: Linking
A linker combines the object code produced by the compiler with any additional code needed to yield a complete executable program
Compiling and Linking: The _____ and _____ are usually integrated within the _____.
preprocessor; linker; compiler.
With filenames for C, is the .c extension required or not.
Required
To compile and link a program called pun.c under Unix, enter the following command in a terminal at command-line promo:
cc pun.c
What is automatically done when “cc” is invoked?
Preprocessing and linking are automatic when using cc.
After compiling and linking the program, what happens?
cc leaves the executable program in a file named a.out by default
What does -o option do
choose the name of the file containing the executable program
Give the command to choose the name “pun” for the output file when you compile the program pun.c
cc -o pun pun.c
or
cc pun.c -o pun
What happens before a C program is compiled?
It is first edited by a preprocessor.
Commands intended fro the preprocessor are called ______
Directives
What character do directives start with?
A hashtag (#) character
Examples of directives:
include
What is
A header containing information aboutC’s standard I/O library
By default, each directive is how many lines long?
One line long
Are there any semicolons besides directives?
No and no other special marker at the end either
What is a function?
A series of statements that have been grouped together and given a name
A function that computes a value uses a____
return statement to specify what value it returns
What gets called automatically when a C program you’ve made is executed?
The Main function
What does the main return?
A Status code
Is the status code that’s retuned by a Unix shell script similar to the status code retuned by a Unix shells script?
Yes
What is the status code returned by main when a program normally terminates after finishing?
0
Will you run into problems if there’s no return statement at end of main function?
Yes, warning message.
When printing strings with printf function, does the curse automatically advance the cursor one line?
No, you have to include the \n in the string to be printed
How do you comment?
/* and ends with */
Variables must be ______ before they are used.
Declared
Can several variables be declared at the same time?
Yes:
int height, length, width, volume
In C99, how can you comment?
// this is a commend ended at the end of the line
When main function contains declarations, where must they be placed?
Must precede ANY statements.
In C99, declarations don’t have to come before statements
Yet, variable must be declared before they are used
Can you assign value of certain type into a variable of a different type?
It is possible but not always safe.
Explain the different parts to this statement:
printf ( “Height: %d\n”, height );
- printf is being used to display the current value of a variable
- %d is a placeholder indicating where the value of the variable height is to be filled in
What type of variables does %d work on?
only int variables
How do you print float variables since you cannot use %d with float?
Use %f instead.
%f prints how many decimals?
6 decimals.
profit = 2150.48f;
printf(“Profit: $ \n”, profit);
What goes in the space to print only 2 decimals of the profit value instead of 6?
%.2f
Can an initial value of a variable be included in its declaration?
Yes, called the “initializer” ex:
int height = 8, length = 12, width = 10;
int height, length, width = 10;
How do you scan an int value stored in variable “i”?
scant (“%d”, &i)
How do you scan a float value stored in variable “x”?
scanf(“%f”, &x);
What are macro definitions?
Name a constant - ex: #define INCHES_PER_POUND 166
When a program is compiled, what is replacing each macro by the value it represents?
The Preprocessor
Duringpreprocessing,thestatement
weight = (volume + INCHES_PER_POUND - 1) / INCHES_PER_POUND;
will become
weight = (volume + 166 - 1) / 166;
Names for variables, functions, macros, and other entities are called _____
identifiers
identifiers must being with a
letter or underscore (i.e. no numbers)
Is C case-sensitive?
Yes
What is the limit on the maximum length of an identifier?
C places no limit; none.
The following keywords can’t be used as identifiers:
auto enum restrict unsigned break extern return void case float char for const goto continue if default inline struct _Imaginary do int switch double long typedef else register union short signed sizeof static volatile while
The following keywords can’t be used as identifiers (C99 only):
inline; restrict; _Bool; _Complex; _Imaginary
A c program is a series of _____
tokens
Give examples of tokens:
- identifiers keywords operators punctuations constants string literals
How many tokens are in:
printf(“Height: %d\n”, height);
7: printf - identifier ( - punctuation "Height: %d\n" - string literal , - punctuation height - identifier ) - punctuation ; - punctuation
How important is the amount of space between tokens usually?
Usually not critical.
Can you put a new-line character in a string over two lines?
No its illegal