(1) | _____ |
#ifndef is a C function. |
(2) | _____ |
#define is a preprocesor directive. |
(3) | _____ |
-pedantic-errors is an option/switch on the cc
command that instructs it to issue an error whenever the "base standard"
requires a diagnostic.
|
makefile
:
test: test.o module1.o cc test.o module1.o -o test test.o: test.c module1.h cc -c test.c module1.o: module1.c module1.h cc -c module1.c
#define QUIET #include <stdio.h> int main(void) { printf("Execution begins...\n"); #ifndef QUIET for (int i=0; i<5; i++) printf("Iteration: %d\n", i); #endif printf("Execution ends.\n"); }
what will be printed when it is executed?
cc -E -P filename
will just run the preprocessor
on filename (without including linemarkers).
constants.h
Rubber
library.h
#include "constants.h" Baby Buggy
library.c
#include "constants.h" #include "library.h" Bumpers
what output will be generated by:
cc -E -P library.c
constants.h
#ifndef CONSTANTS_H #define CONSTANTS_H Rubber #endif
library.h
#ifndef LIBRARY_H #define LIBRARY_H #include "constants.h" Baby Buggy #endif
library.c
#include "constants.h" #include "library.h" #define BUMPERS Bumpers BUMPERS
what output will be generated by:
cc -E -P library.c
calculator.c
and that there is no file named calculator.h
.
int add (int a, int b) { return a+b; }
cc -c calculator.c -o calculator.o
execute without error? If not, why not (and will the error be
generated by the preprocessor, compiler, or linker)? If so, what
file will be produced?
cc calculator.c
execute without error? If not, why not (and will the error be
generated by the preprocessor, compiler, or linker)? If so, what
file will be produced?
calculator.c
and that there is no file named calculator.h
.
int bump(int a) { return a + increase; }
cc -c calculator.c -o calculator.o
execute without error? If not, why not (and will the error be
generated by the preprocessor, compiler, or linker)? If so, what
file will be produced?
#include "calculator.h"Will the command
cc -c calculator.c -o calculator.o
execute without error? If not, why not (and will the error be
generated by the preprocessor, compiler, or linker)? If so, what
file will be produced?
calculator.h
int increase;
calculator.c
#include "calculator.h" int bump(int a) { return a + increase; }
gradebook.c
#include "calculator.h" #include <stdio.h> int main(void) { increase = 10; printf("Midterms Average: %d\n", add(80, 100) / 2); printf("Final (Curved): %d\n", bump(70)); return 0; }
cc -c calculator.c -o calculator.o
execute without error? If not, why not (and will the error be
generated by the preprocessor, compiler, or linker)? If so, what
file will be produced?
cc -pedantic -pedantic-errors -Wall -c gradebook.c -o gradebook.o
execute without error? If not, why not (and will the error be
generated by the preprocessor, compiler, or linker)? If so, what
file will be produced?
cc -Wall -c gradebook.c -o gradebook.o
execute without error? If not, why not (and will the error be
generated by the preprocessor, compiler, or linker)? If so, what
file will be produced?
cc gradebook.o calculator.o -o gradebook
execute without error? If not, why not (and will the error be
generated by the preprocessor, compiler, or linker)? If so, what
file will be produced?
calculator.h
is changed so that it
contains the line extern int increase;
.
Will the two .c
files compile? Will they link?
If not, why not?
lib.h
#ifndef LIB_H #define LIB_H int net_price(int price, int discount); static int validate(int n); #endif
lib.c
#include "lib.h" int net_price(int price, int discount) { return validate(price - discount); } static int validate(int n) { if (n < 0) return 0; else return n; }
pricer.c
#include "lib.h" int main(void) { int i, j; i = validate(-5); j = net_price(6, 2); }
cc -pedantic -pedantic-errors -Wall -c lib.c -o lib.o
execute without error? If not, why not (and will the error be
generated by the preprocessor, compiler, or linker)? If so, what
file will be produced?
cc -pedantic -pedantic-errors -Wall -c pricer.c -o pricer.o
execute without error? If not, why not (and will the error be
generated by the preprocessor, compiler, or linker)? If so, what
file will be produced?
cc -Wall -c pricer.c -o pricer.o
execute without error? If not, why not (and will the error be
generated by the preprocessor, compiler, or linker)? If so, what
file will be produced?
cc pricer.o lib.o -o pricer
execute without error? If not, why not (and will the error be
generated by the preprocessor, compiler, or linker)? If so, what
file will be produced?
Copyright 2017