Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to C Topics Compilation Using the gcc Compiler

Similar presentations


Presentation on theme: "Introduction to C Topics Compilation Using the gcc Compiler"— Presentation transcript:

1 Introduction to C Topics Compilation Using the gcc Compiler
The Anatomy of a C Program Reading Sections

2 Writing C Programs A programmer uses a text editor to create or modify files containing C code. C code is also called source code. A file containing source code is called a source file. After a source file has been created, the programmer must invoke the C compiler before the program can be executed (run).

3 Using the C Compiler Invoking the compiler is system dependent.
At UMBC, we have two C compilers available, cc and gcc. For this class, we will use the gcc compiler as it is the compiler available on the Linux system.

4 Invoking the gcc Compiler
At the prompt, type gcc -ansi -Wall pgm.c where pgm.c is the C program source file.

5 To specify your own executable filename
gcc -ansi -Wall project1.c -o myname.out Where project1.c is the source file that contains a program, and the executable file created by the compiler is: myname.out not the usual a.out filename.

6 The Result : a.out If there are no errors in pgm.c, this command produces an executable file, which is one that can be executed (run). Both the cc and the gcc compilers name the executable file a.out To execute the program, at the prompt, type a.out Although we call this “compiling a program,” what actually happens is more complicated.

7 3 Stages of Compilation Stage 1: Preprocessing
Performed by a program called the preprocessor Modifies the source code (in RAM) according to preprocessor directives (preprocessor commands) embedded in the source code. Strips comments and whitespace from the code The source code as stored on disk is not modified.

8 3 Stages of Compilation (con’t)
Stage 2: Compilation Performed by a program called the compiler Translates the preprocessor-modified source code into object code (machine code) Checks for syntax errors and warnings Saves the object code to a disk file, if instructed to do so. If any compiler errors, compiler warnings, or linker errors are received, no object code file will be generated

9 3 Stages of Compilation (con’t)
Stage 3: Linking Combines the program object code with other object code to produce the executable file. The other object code can come from the Run-Time Library, other libraries, or object files that you have created. Saves the executable code to a disk file. On the Linux system, that file is called a.out. If any compiler errors, compiler warnings, or linker errors are received, no executable file will be generated

10 Program Development Using gcc
Editor Source File pgm.c Preprocessor Modified Source Code in RAM Compiler Program Object Code File pgm.o Other Object Code Files Linker Executable File a.out

11 An algorithm for writing code
Write the algorithm Write the code using emacs (pico, vi) Try to compile the code While there are still syntax errors Fix errors Test the program Fix any semantic errors Compile the code

12 Incremental Approach to Writing Code (I)
Tips about writing code. Write your code in incomplete but working pieces. For instance: For your project Don’t write the whole program at once. Just write enough that you display the prompt to the user on the screen. Get that part working first. Next write the part that gets the value from the user, and then just print it out.

13 Incremental Approach to Writing Code (II)
Get that working. Next change the code so that you use the value in a calculation and print out the answer. Make program modifications: perhaps additional instructions to the user a displayed program description for the user add more comments. Get the final version working.

14 A Simple C Program /* Filename: hello.c
Author: Brian Kernighan & Dennis Ritchie Date written: ?/?/1978 Description: This program prints the greeting “Hello, World!” */ #include <stdio.h> main ( ) { printf ( “ Hello, World! \n ” ) ; }

15 Anatomy of a C Program program header comment
preprocessor directives (if any) main ( ) { statement(s) }

16 Program Header Comment
All comments must begin with the characters /* and end with the characters */ These are called comment delimiters The program header comment always comes first. Look at the class web page for the required contents of our header comment: such things as filename, author, date written and description of the program.

17 Preprocessor Directives
Lines that begin with a # in column 1 are called preprocessor directives (commands). Example: the #include <stdio.h> directive causes the preprocessor to include a copy of the standard input/output header file stdio.h at this point in the code. This header file was included because it contains information about the printf ( ) function that’s used in this program.

18 Preprocessor Directives
The preprocessor Directive files can be found in a directory on the system called: /usr/include You can look at the contents of these files with a unix command like: more /usr/include/stdio.h

19 main ( ) Every program must have a function called main. This is where program execution begins. main() is placed in the source code file as the first function for readability. The parentheses following the reserved word main indicate to the compiler that it is a function.

20 Braces A left brace (curly bracket) -- { -- begins the body of every function. A corresponding right brace -- } -- must end the function. The style is to place these braces on separate lines in column 1.

21 printf ( “ Hello, World! \n ” ) ;
This line is a C statement. It a call to the function printf ( ) with a single argument (parameter), namely the string “ Hello, World! \n ” Even though a string may contain many characters, the string itself should be thought of as a single quantity. Notice that this line ends with a semicolon. All statements in C end with a semicolon.

22 Another C Program /*****************************************
** File: proj1.c ** Author: Joe Student ** Date: 9/15/01 ** ID #: xxx-xx-6789 ** Section: 0304 ** ** ** This program prompts the user for two integer values then displays ** their product. ***********************************************/

23 Another C Program (con’t)
#include <stdio.h> int main() { int value1, value2, product ; printf(“Enter two integer values: “) ; scanf(“%d%d”, &value1, &value2) ; product = value1 * value2 ; printf(“Product = %d\n”, product) ; return 0 ; }

24 Another C Program (con’t)
Note the use of the reserved word int: int main() Many C compilers will cause main() to default to int if it is omitted. However, the gcc compiler will issue a warning. Also note the last line: return 0 ; This statement must also be included as the last line of your program to avoid a compiler warning.

25 Good Programming Practices
C programming standards are available on the class homepage. You are expected to conform to these standards for all programming projects in this class and in CMSC (This is part of your grade for your project!) The program just shown conforms to these standards, but is uncommented (later) Subsequent lectures will include “Good Programming Practices” slides.

26 Examples of Comment Styles
/* a comment */ /*** another comment ***/ /*****/ /*A comment can be written in this * fashion to set it off from the * surrounding code. */

27 More Comments /*******************************************\
* If you wish, you can put comments * * in a box. This is typically used for * * program header comments and for * * function header comments * \*******************************************/

28 Use of Whitespace Use blank lines to separate major parts of a source file or function Separate declarations from executable statements with a blank line Preprocessor directives, main(), and the braces are in column 1

29 Use of Whitespace (continued)
All executable statements are indented one tab stop. How deep should my tabs be ? Either 3 or 4 spaces. Choose whichever you like and use that same number consistently throughout your program. 2 spaces are not enough for good readability, more than 4 causes the indentation to be too deep.


Download ppt "Introduction to C Topics Compilation Using the gcc Compiler"

Similar presentations


Ads by Google