CPS 393 Lecture Notes - Lecture 7: Include Directive, Integer Overflow, Gnu Compiler Collection

56 views5 pages
Lecture 7: C functions
Separate compilation and make utility:
two separate programs that are supposed to work together, cannot be compiled separately and run
they won’t function
1. Compile them separately
2. Link the two object files that have been created
3. Linker will build a single file — the executable for the entire project
/* The factorial function: one argument, one result */
#include <stdio.h>
/* this function will return an integer */
int factorial (int n){
int i, product;
product = 1; //intialize
/* computes factorial */
for (i = n; i > 1; i = i-1){
product = product * i;
}
/* the value that goes out */
return (product);
}
int main (void){
int a, result;
printf ("Enter an integer number: ");
scanf ("%d", &a);
/* calling the function */
result = factorial (a);
/* printing the report */
printf ("The factorial of %d is %d.\n", a, result);
return (0);
}
/* NOTE: a large number (like 13) will produce an arithmetic overflow */
// Have to include a library of functions at the top related to your proj
#include "fileName.h" // can be included in other files
// 1. separately compile only for both files
gcc -c <source_file>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
1
2
3
4
5
Unlock document

This preview shows pages 1-2 of the document.
Unlock all 5 pages and 3 million more documents.

Already have an account? Log in
Make Utility:
File dependency graph
arrow = depends on
// example:
gcc -c show_face.c
gcc -c face.c
// 2. now link the object files
gcc <object_files>
// example:
gcc show_face.o face.o
// or if you want to name the executable
gcc -o face_proj show_face.o face.o
// 3. Run program from executable
./a.out
./face_proj
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
Unlock document

This preview shows pages 1-2 of the document.
Unlock all 5 pages and 3 million more documents.

Already have an account? Log in

Get access

Grade+
$10 USD/m
Billed $120 USD annually
Homework Help
Class Notes
Textbook Notes
40 Verified Answers
Study Guides
1 Booster Class
Class+
$8 USD/m
Billed $96 USD annually
Homework Help
Class Notes
Textbook Notes
30 Verified Answers
Study Guides
1 Booster Class