Download presentation
Presentation is loading. Please wait.
Published byChester Nicholson Modified over 9 years ago
1
CMSC 1041 Introduction to C Creating your first C program
2
CMSC 1042 Writing C Programs l The programmer uses a text editor to create or modify files containing C code. l C code is also called source code l A file containing source code is called a source file l After a source file has been created, the programmer must invoke the C compiler.
3
CMSC 1043 Using the C Compiler l Invoking the compiler is system dependent. oAt UMBC, we have two C compilers available, cc and gcc. oFor this class, we will use the cc compiler, because the error messages given by the cc compiler are easier for beginners to understand.
4
CMSC 1044 Invoking the Compiler Example cc pgm.c where pgm.c is the source file that contains a program
5
CMSC 1045 The Result : a.out l If there are no errors in pgm.c, this command produces an executable file, one that can be run or executed. oBoth the cc and the gcc compilers name the executable file a.out oTo execute the program, type a.out at the Unix prompt. l Although we call this “compiling a program”, what actually happens is more complicated.
6
CMSC 1046 3 Stages of Compilation 1Preprocessor - modifies the source code oHandles preprocessor directives oStrips comments and whitespace from the code
7
CMSC 1047 3 Stages of Compilation 2Compiler - translates the modified source code into object code oParser - checks for errors oCode Generator - makes the object code oOptimizer - may change the code to be more efficient
8
CMSC 1048 3 Stages of Compilation 3Linker - combines the object code of our program with other object code to produce the executable file. The other object code can come from: oThe Run-Time Library - a collection of object code with an index so that the linker can find the appropriate code. oother object files oother libraries
9
CMSC 1049 Compilation Diagram Editor Compiler Preprocessor Parser Code Generator Optimizer Linker < Source File myprog.c Object File myprog.o Other Obj’s Run-time library Other libraries Executable file a.out
10
CMSC 10410 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
11
CMSC 10411 Incremental Approach to Writing Code l Tips about writing code. l Write your code in incomplete but working pieces. l For instance: For your project oDon’t write the whole program at once. oJust write enough that you display the prompt to the user on the screen. oGet that part working first. oNext write the part that gets the value from the user, and then just print it out.
12
CMSC 10412 Incremental Approach to Writing Code (continued) oGet that working. oNext change the code so that you use the value in a calculation and print out the answer. oGet that working. oMake program modifications: perhaps additional instructions to the user a displayed program description for the user add more comments. oGet the final version working.
13
CMSC 10413 A Simple C Program /* Filename: hello.c Author: Brian Kernighan & Dennis Ritchie Date written: ?/?/1978 Description: This program prints the greeting “Hello, World!” */ #include main ( ) { printf (“Hello, World!\n”) ; }
14
CMSC 10414 Anatomy of a C Program program header comment preprocessor directives main ( ) { statement(s) }
15
CMSC 10415 The Program Header Comment l All comments must begin with the characters /* and end with the characters */ l The program header comment always comes first l The program header comment should include the filename, author, date written and a description of the program
16
CMSC 10416 Preprocessor Directive l Lines that begin with a # are called preprocessor directives l The #include directive causes the preprocessor to include a copy of the standard input/output header file stdio.h at this point in the code. l This header file was included because it contains information about the printf ( ) function that’s used in this program.
17
CMSC 10417 main ( ) l Every program has a function called main, where execution begins l The parenthesis following main indicate to the compiler that it is a function.
18
CMSC 10418 Left Brace l A left curly brace -- { -- begins the body of every function. A corresponding right curly brace must end the function. l The style is to place these braces on separate lines in column 1.
19
CMSC 10419 printf (“Hello, World!\n”) ; l This is the function printf ( ) being called with a single argument, namely the string “Hello, World!\n” l Even though a string may contain many characters, the string itself should be thought of as a single quantity. l Notice that this line ends with a semicolon. All statements in C end with a semicolon.
20
CMSC 10420 Right Brace l This right curly brace -- } --matches the left curly brace above. l It ends the function main ( ).
21
CMSC 10421 Good Programming Practices l C programming standards are available on the Web -- see course homepage l You will be expected to conform to these standards for all programming projects in this class and in CMSC 201 l These standards include: oNaming conventions oUse of whitespace oUse of Braces oComments
22
CMSC 10422 Examples of Comment Styles /* a comment */ /*** another comment ***/ /*****/ /*A comment can be written in this * fashion to set it off from the * surrounding code. */
23
CMSC 10423 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 * \*******************************************/
24
CMSC 10424 Use of Whitespace l Use blank lines to separate major parts of a source file or function l Separate declarations from executable statements with a blank line l Preprocessor directives, main(), and the braces are in column 1
25
CMSC 10425 Use of Whitespace (continued) l All executable statements are indented one tab stop. How deep should my tabs be ? l Either 3 or 4 spaces. Choose whichever you like and use that same number consistently throughout your program. l 2 spaces are not enough for good readability, more than 4 causes the indentation to be too deep.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.