CMSC 104, Lecture 111 Introduction to C Topics l Compilation l Using the gcc Compiler l The Anatomy of a C Program l 104 C Programming Standards and Indentation.

Slides:



Advertisements
Similar presentations
Lecture 2 Introduction to C Programming
Advertisements

Introduction to C Programming
 2000 Prentice Hall, Inc. All rights reserved. Chapter 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line.
Introduction to C Creating your first C program. Writing C Programs  The programmer uses a text editor to create or modify files containing C code. 
 C++ programming facilitates a disciplined approach to program design. ◦ If you learn the correct way, you will be spared a lot of work and frustration.
Introduction to C Programming
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
Introduction to C Programming Overview of C Hello World program Unix environment C programming basics.
Introduction to C Programming
CMSC 104, Version 9/011 Introduction to C Topics Compilation Using the gcc Compiler The Anatomy of a C Program 104 C Programming Standards and Indentation.
Introduction to C Topics Compilation Using the gcc Compiler
Introduction to C. A Brief History Created by Dennis Ritchie at AT&T Labs in 1972 Originally created to design and support the Unix operating system.
Chapter 3: Introduction to C Programming Language C development environment A simple program example Characters and tokens Structure of a C program –comment.
Introduction to C/C++ Programming This lecture has major focus on programming, compilation.
Introduction to C Programming. A Brief History u Created by Dennis Ritchie at AT&T Labs in 1972 u Originally created to design and support the Unix operating.
Goals of Course Introduction to the programming language C Learn how to program Learn ‘good’ programming practices.
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
History of C and C++ C++ evolved from C ANSI C C++ “spruces up” C
Theory is when you know something, but it doesn't work. Practice is when something works, but you don't know why. Programmers combine theory and practice:
Creating your first C++ program
Programming With C.
/* Documentations */ Pre process / Linking statements Global declarations; main( ) { Local Declarations; Program statements / Executable statements; }
EPSII 59:006 Spring Introduction to C More Administrative Details The C Programming Language How a computer processes programs Your first C program.
CMSC 104, Version 9/011 Introduction to C Topics Compilation Using the gcc Compiler The Anatomy of a C Program 104 C Programming Standards and Indentation.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
Programming Fundamentals. Today’s Lecture Why do we need Object Oriented Language C++ and C Basics of a typical C++ Environment Basic Program Construction.
COMPUTER PROGRAMMING. A Typical C++ Environment Phases of C++ Programs: 1- Edit 2- Preprocess 3- Compile 4- Link 5- Load 6- Execute Loader Primary Memory.
1 Programming in C Hello World! Soon I will control the world! Soon I will control the world!
Algorithms  Problem: Write pseudocode for a program that keeps asking the user to input integers until the user enters zero, and then determines and outputs.
1 A simple C++ program // ======================================================= // File:helloworld.cpp // Author:Vana Doufexi // Date:1/4/2006 // Description:Displays.
Introduction to C CMSC 104, Section 4 Richard Chang 1.
Introduction to C 2008/09/29: Lecture 7 CMSC 104, Section 0101 John Y. Park 1.
CMSC 1041 Introduction to C Creating your first C program.
Anatomy of the simplest C Program Sen Zhang SUNY Oneonta Oneonta, NEW YORK © 2004
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
 2007 Pearson Education, Inc. All rights reserved. A Simple C Program 1 /* ************************************************* *** Program: hello_world.
Principles of Programming CSEB134 : BS/ CHAPTER Fundamentals of the C Programming Language.
Introduction to C Programming
1 Types of Programming Language (1) Three types of programming languages 1.Machine languages Strings of numbers giving machine specific instructions Example:
L071 Introduction to C Topics Compilation Using the gcc Compiler The Anatomy of a C Program Reading Sections
12/06/ :04:41 CSC Alliance — 1 Kimera Richard Phone: INSTITUTE OF COMPUTER SCIENCE DEPARTMENT.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 1.
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
STRUCTURED PROGRAMMING Complete C++ Program. Content 2  Main Function  Preprocessor directives  User comments  Escape characters  cout statement.
Introduction to C Topics Compilation Using the gcc Compiler
UMBC CMSC 104 – Section 01, Fall 2016
ANNOUNCEMENT The missed lecture will be made up this Monday evening in the Tech PC classroom (MG51). A tentative time interval is 6:30-8:00. The exact.
CSC201: Computer Programming
Introduction to C Language
Chapter 2 - Introduction to C Programming
Chapter 2, Part I Introduction to C Programming
Algorithms Problem: Write pseudocode for a program that keeps asking the user to input integers until the user enters zero, and then determines and outputs.
Introduction to C Topics Compilation Using the gcc Compiler
Introduction to C Topics Compilation Using the gcc Compiler
Chapter 2 - Introduction to C Programming
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
Introduction to C Topics Compilation Using the gcc Compiler
Creating your first C program
Chapter 2 - Introduction to C Programming
Programming Fundamentals Lecture #3 Overview of Computer Programming
2008/09/29: Lecture 7 CMSC 104, Section 0101 John Y. Park
Introduction to C Topics Compilation Using the gcc Compiler
Chapter 2 - Introduction to C Programming
More Loops Topics Relational Operators Logical Operators for Loops.
Introduction to C Topics Compilation Using the gcc Compiler
Introduction to C Programming
Functions, Part 2 of 42 Topics Functions That Return a Value
Presentation transcript:

CMSC 104, Lecture 111 Introduction to C Topics l Compilation l Using the gcc Compiler l The Anatomy of a C Program l 104 C Programming Standards and Indentation Styles Reading l Sections ,

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

CMSC 104, Lecture 113 Using the C Compiler at UMBC 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 gcc compiler as it is the compiler available on the Linux system. (in fact cc is just a link to gcc)

CMSC 104, Lecture 114 Invoking the gcc Compiler At the prompt, type gcc -ansi -Wall pgm.c where pgm.c is the C program source file. l -ansi is a compiler option that tells the compiler to adhere to the ANSI C standard. l -Wall is an option to turn on all compiler warnings (best for new programmers).

CMSC 104, Lecture 115 The Result : a.out l If there are no errors in pgm.c, this command produces an executable file, which is one that can be executed (run). l The gcc compiler names the executable file a.out l To execute the program, at the prompt, type a.out l Although we call this process “compiling a program,” what actually happens is more complicated.

CMSC 104, Lecture Stages of Compilation Stage 1: Preprocessing o Performed by a program called the preprocessor o Modifies the source code (in RAM) according to preprocessor directives (preprocessor commands) embedded in the source code oStrips comments and whitespace from the code oThe source code as stored on disk is not modified.

CMSC 104, Lecture Stages of Compilation (con’t) Stage 2: Compilation oPerformed by a program called the compiler oTranslates the preprocessor-modified source code into object code (machine code) oChecks for syntax errors and warnings oSaves the object code to a disk file, if instructed to do so (we will not do this). oIf any compiler errors are received, no object code file will be generated. oAn object code file will be generated if only warnings, not errors, are received.

CMSC 104, Lecture Stages of Compilation (con’t) The Compiler consists of the following sub- systems: oParser - checks for errors oCode Generator - makes the object code oOptimizer - may change the code to be more efficient

CMSC 104, Lecture Stages of Compilation (con’t) Stage 3: Linking oCombines the program object code with other object code to produce the executable file. oThe other object code can come from the Run-Time Library (a collection of object code with an index so that the linker can find the appropriate code), other libraries, or object files that you have created. oSaves the executable code to a disk file. On the Linux system, that file is called a.out. oIf any linker errors are received, no executable file will be generated.

CMSC 104, Lecture 1110 Program Development Using gcc Source File pgm.c Program Object Code File pgm.o Executable File a.out Preprocessor Modified Source Code in RAM Compiler Linker Other Object Code Files (if any) Editor

CMSC 104, Lecture 1111 An algorithm for writing code Write the code using a text editor (eg emacs, vi, pico) 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

CMSC 104, Lecture 1112 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.

CMSC 104, Lecture 1113 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.

CMSC 104, Lecture 1114 A Simple C Program /* Filename: hello.c Author: Brian Kernighan & Dennis Ritchie Date written: ?/?/1978 Description: This program prints the greeting “Hello, World!” */ #include int main ( ) { printf (“Hello, World!\n”) ; return 0 ; }

CMSC 104, Lecture 1115 Anatomy of a C Program program header comment preprocessor directives (if any) int main ( ) { statement(s) return 0 ; }

CMSC 104, Lecture 1116 The Program Header Comment l A comment is descriptive text used to help a reader of the program understand its content. l All comments must begin with the characters /* and end with the characters */. These are called comment delimiters 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. Look at the class web page for details of the required contents of our header comment.

CMSC 104, Lecture 1117 Preprocessor Directive l Lines that begin with a # in column 1 are called preprocessor directives (comands) l Example: 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.

CMSC 104, Lecture 1118 int main ( ) l Every program must have a function called main. This is where program execution begins. l main() is placed in the source code file as the first function for readability. l The reserved word “int” indicates that main() returns an integer value. l The parenthesis following the reserved word “main” indicate that it is a function.

CMSC 104, Lecture 1119 The Function Body 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 and to indent the entire function body 3 to 5 spaces.

CMSC 104, Lecture 1120 printf (“Hello, World!\n”) ; l This line is a C statement. l It is a call to the function printf ( ) with a single argument (parameter), 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.

CMSC 104, Lecture 1121 return 0 ; l Because function main() returns an integer value, there must be a statement that indicates what this value is. l The statement return 0 ; indicates that main() returns a value of zero to the operating system. l A value of 0 indicates that the program successfully terminated execution. l Do not worry about this concept now. Just remember to use the statement.

CMSC 104, Lecture 1122 Good Programming Practices l C programming and indentation styles are available on the 104 course homepage. l You are expected to conform to these standards for all programming projects in this class and in CMSC 201. (This will be part of your grade for each project!) l These standards include: oNaming conventions oUse of whitespace oUse of Braces oComments

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

CMSC 104, Lecture 1124 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 * \*******************************************/

CMSC 104, Lecture 1125 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

CMSC 104, Lecture 1126 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.

CMSC 104, Lecture 1127 Another C Program /***************************************** ** File: proj1.c ** Author: Joe Student ** Date: 9/15/01 ** SSN: ** Section: 0304 ** ** ** This program prompts the user for two integer values then displays ** their product. ** ***********************************************/

CMSC 104, Lecture 1128 Another C Program (con’t) #include 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 ; }