cmsc 1041 introduction to c creating your first c program

25
CMSC 104 Introduction to C Creating your first C program

Upload: chester-nicholson

Post on 29-Jan-2016

221 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CMSC 1041 Introduction to C Creating your first C program

CMSC 104 1

Introduction to C

Creating your first C program

Page 2: CMSC 1041 Introduction to C Creating your first C program

CMSC 104 2

Writing C Programs

The 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.

Page 3: CMSC 1041 Introduction to C Creating your first C program

CMSC 104 3

Using the C Compiler

Invoking the compiler is system dependent.o At UMBC, we have two C compilers

available, cc and gcc. o For this class, we will use the cc compiler,

because the error messages given by the cc compiler are easier for beginners to understand.

Page 4: CMSC 1041 Introduction to C Creating your first C program

CMSC 104 4

Invoking the Compiler Example

cc pgm.c

where pgm.c is the source file that contains a program

Page 5: CMSC 1041 Introduction to C Creating your first C program

CMSC 104 5

The Result : a.out

If there are no errors in pgm.c, this command produces an executable file, one that can be run or executed.o Both the cc and the gcc compilers name the

executable file a.outo To execute the program, type a.out at the

Unix prompt. Although we call this “compiling a program”,

what actually happens is more complicated.

Page 6: CMSC 1041 Introduction to C Creating your first C program

CMSC 104 6

3 Stages of Compilation

1 Preprocessor - modifies the source code

o Handles preprocessor directives

o Strips comments and whitespace from the code

Page 7: CMSC 1041 Introduction to C Creating your first C program

CMSC 104 7

3 Stages of Compilation

2 Compiler - translates the modified source code into object code

o Parser - checks for errors

o Code Generator - makes the object code

o Optimizer - may change the code to be more efficient

Page 8: CMSC 1041 Introduction to C Creating your first C program

CMSC 104 8

3 Stages of Compilation

3 Linker - combines the object code of our program with other object code to produce the executable file.

The other object code can come from:o The Run-Time Library - a collection of object

code with an index so that the linker can find the appropriate code.

o other object fileso other libraries

Page 9: CMSC 1041 Introduction to C Creating your first C program

CMSC 104 9

Compilation Diagram

Editor

Compiler Preprocessor

ParserCode Generator Optimizer

Linker

<

Source File myprog.c

Object File myprog.oOther Obj’s Run-time library Other libraries

Executable file a.out

Page 10: CMSC 1041 Introduction to C Creating your first C program

CMSC 104 10

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 Try to compile the code Test the program Fix any semantic errors Compile the code Test the program

Page 11: CMSC 1041 Introduction to C Creating your first C program

CMSC 104 11

Incremental Approach to Writing Code

Tips about writing code. Write your code in incomplete but working pieces. For instance: For your project

o Don’t write the whole program at once.o Just write enough that you display the prompt

to the user on the screen.o Get that part working first.o Next write the part that gets the value from the

user, and then just print it out.

Page 12: CMSC 1041 Introduction to C Creating your first C program

CMSC 104 12

Incremental Approach to Writing Code (continued)

o Get that working. o Next change the code so that you use the

value in a calculation and print out the answer.o Get that working. o Make program modifications:

� perhaps additional instructions to the user� a displayed program description for the user� add more comments.

o Get the final version working.

Page 13: CMSC 1041 Introduction to C Creating your first C program

CMSC 104 13

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”) ; }

Page 14: CMSC 1041 Introduction to C Creating your first C program

CMSC 104 14

Anatomy of a C Program

program header comment

preprocessor directives

main ( ) { statement(s) }

Page 15: CMSC 1041 Introduction to C Creating your first C program

CMSC 104 15

The Program Header Comment

All comments must begin with the characters /* and end with the characters */

The program header comment always comes first

The program header comment should include the filename, author, date written and a description of the program

Page 16: CMSC 1041 Introduction to C Creating your first C program

CMSC 104 16

Preprocessor Directive

Lines that begin with a # are called preprocessor directives

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.

Page 17: CMSC 1041 Introduction to C Creating your first C program

CMSC 104 17

main ( )

Every program has a function called main, where execution begins

The parenthesis following main indicate to the compiler that it is a function.

Page 18: CMSC 1041 Introduction to C Creating your first C program

CMSC 104 18

Left Brace

A left curly brace -- { -- begins the body of every function. A corresponding right curly brace must end the function.

The style is to place these braces on separate lines in column 1.

Page 19: CMSC 1041 Introduction to C Creating your first C program

CMSC 104 19

printf (“Hello, World!\n”) ;

This is the function printf ( ) being called with a single argument, 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.

Page 20: CMSC 1041 Introduction to C Creating your first C program

CMSC 104 20

Right Brace

This right curly brace -- } --matches the left curly brace above.

It ends the function main ( ).

Page 21: CMSC 1041 Introduction to C Creating your first C program

CMSC 104 21

Good Programming Practices

C programming standards are available on the Web -- see course homepage

You will be expected to conform to these standards for all programming projects in this class and in CMSC 201

These standards include:o Naming conventions

o Use of whitespace

o Use of Braces

o Comments

Page 22: CMSC 1041 Introduction to C Creating your first C program

CMSC 104 22

Examples of Comment Styles

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

Page 23: CMSC 1041 Introduction to C Creating your first C program

CMSC 104 23

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 * \*******************************************/

Page 24: CMSC 1041 Introduction to C Creating your first C program

CMSC 104 24

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

Page 25: CMSC 1041 Introduction to C Creating your first C program

CMSC 104 25

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.