CMSC 1041 Introduction to C Creating your first C program.

Slides:



Advertisements
Similar presentations
Chapter 11 Introduction to Programming in C
Advertisements

Introduction to C Programming
Introduction to C++ Programming. A Simple Program: Print a Line of Text // My First C++ Program #include int main( ) { cout
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.
Your First C++ Program Aug 27, /27/08 CS 150 Introduction to Computer Science I C++  Based on the C programming language  One of today’s most.
CS1061 C Programming Lecture 2: A Few Simple Programs A. O’Riordan, 2004.
1 Key Concepts:  Why C?  Life Cycle Of a C program,  What is a computer program?  A program statement?  Basic parts of a C program,  Printf() function?
Introduction to C Programming Overview of C Hello World program Unix environment C programming basics.
1 ICS103 Programming in C Lecture 2: Introduction to C (1)
About the Presentations The presentations cover the objectives found in the opening of each chapter. All chapter objectives are listed in the beginning.
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.
Introduction To C++ Programming 1.0 Basic C++ Program Structure 2.0 Program Control 3.0 Array And Structures 4.0 Function 5.0 Pointer 6.0 Secure Programming.
Chapter 3: Introduction to C Programming Language C development environment A simple program example Characters and tokens Structure of a C program –comment.
Using C Programming Language.  The programs that run on a computer are referred to as software.  You’ll learn key programming methodology that are enhancing.
Introduction to C/C++ Programming This lecture has major focus on programming, compilation.
By: Mr. Baha Hanene Chapter 3. Learning Outcomes We will cover the learning outcome 02 in this chapter i.e. Use basic data-types and input / output in.
Welcome In The World Of ‘C’.  TEXT BOOK: Programming in ANCI ‘C By: E Balagurusamy. TMH  Reference Books: 1) The ‘C Programming Language By: Kernighan.
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.
Chapter 3 Getting Started with C++
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
C++ Basics Structure of a Program. C++ Source Code Plain text file Typical file extension .CPP Must compile the C++ source code without errors before.
Creating your first C++ program
Programming With C.
History of C 1950 – FORTRAN (Formula Translator) 1959 – COBOL (Common Business Oriented Language) 1971 – Pascal Between Ada.
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.
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!
Computer Programming I Hour 2 - Writing Your First C Program.
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 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.
Agenda Computer Languages How to Write a Simple C Program
Anatomy of the simplest C Program Sen Zhang SUNY Oneonta Oneonta, NEW YORK © 2004
CMPE13Cyrus Bazeghi 1 Chapter 11 Introduction to Programming in C.
 2007 Pearson Education, Inc. All rights reserved. A Simple C Program 1 /* ************************************************* *** Program: hello_world.
Introducing Java Chapter 3 Review. Why Program in Java? Java, is an object-oriented programming language. OOP languages evolved out of the need to better.
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:
1 8/30/06CS150 Introduction to Computer Science 1 Your First C++ Program.
L071 Introduction to C Topics Compilation Using the gcc Compiler The Anatomy of a C Program Reading Sections
BIL 104E Introduction to Scientific and Engineering Computing Lecture 1.
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
Computer Science 210 Computer Organization
CSC201: Computer Programming
Introduction to C Language
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
Welcome In The World Of ‘C’.  TEXT BOOK: Programming in ANSI ‘C By: E Balagurusamy. TMH  Reference Books: 1) The ‘C Programming Language By: Kernighan.
Govt. Polytechnic,Dhangar
Introduction to C Topics Compilation Using the gcc Compiler
Creating your first C program
2008/09/29: Lecture 7 CMSC 104, Section 0101 John Y. Park
Your first C and C++ programs
Introduction to C Topics Compilation Using the gcc Compiler
An Overview of C.
Introduction to C Topics Compilation Using the gcc Compiler
Functions, Part 2 of 42 Topics Functions That Return a Value
Presentation transcript:

CMSC 1041 Introduction to C Creating your first C program

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.

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.

CMSC 1044 Invoking the Compiler Example cc pgm.c where pgm.c is the source file that contains a program

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.

CMSC Stages of Compilation 1Preprocessor - modifies the source code oHandles preprocessor directives oStrips comments and whitespace from the code

CMSC 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

CMSC 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

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

CMSC 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

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

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

CMSC 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

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

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

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

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

CMSC Right Brace l This right curly brace -- } --matches the left curly brace above. l It ends the function main ( ).

CMSC 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

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