Creating your first C program

Slides:



Advertisements
Similar presentations
Chapter 11 Introduction to Programming in C
Advertisements

Write a program step by step. Step 1: Problem definition. Given the coordinate of two points in 2-D space, compute and print their straight distance.
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. 
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 2: Your First Program.
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?
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.
Introduction to C/C++ Programming This lecture has major focus on programming, compilation.
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
Creating your first C++ program
Programming With C.
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.
Compilation & Linking Computer Organization I 1 November 2009 © McQuain, Feng & Ribbens The Preprocessor When a C compiler is invoked, the.
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 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.
CMSC 1041 Introduction to C Creating your first 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.
1 Types of Programming Language (1) Three types of programming languages 1.Machine languages Strings of numbers giving machine specific instructions Example:
1 C Syntax and Semantics Dr. Sherif Mohamed Tawfik Lecture Two.
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
CSCE 206 Structured Programming in C
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
Completing the Problem-Solving Process
Introduction to C Language
Chapter 2, Part I Introduction to C Programming
Introduction to C Language
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
Getting Started with C.
Introduction to C Topics Compilation Using the gcc Compiler
The Preprocessor Based on Chapter 1 in C++ for Java Programmers by Weiss When a C compiler is invoked, the first thing that happens is that the code is.
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.
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
Chapter 11 Introduction to Programming in C
Common C Programming Errors, GDB Debugging
Chapter 11 Introduction to Programming in C
Govt. Polytechnic,Dhangar
Introduction to C Topics Compilation Using the gcc Compiler
2008/09/29: Lecture 7 CMSC 104, Section 0101 John Y. Park
Your first C and C++ programs
Homework Applied for cs240? (If not, keep at it!) 8/10 Done with HW1?
Chapter 11 Introduction to Programming in C
CS IA: Procedural Programming CS IB: Object-Oriented Programming
Introduction to Computer Programming
Introduction to C Topics Compilation Using the gcc Compiler
EECE.2160 ECE Application Programming
More Loops Topics Relational Operators Logical Operators for Loops.
An Overview of C.
Introduction to Programming - 1
Introduction to C Topics Compilation Using the gcc Compiler
Chapter 1 c++ structure C++ Input / Output
Chapter 2 part #1 C++ Program Structure
SPL – PS1 Introduction to C++.
Presentation transcript:

Creating your first C program 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 code is also called source code A file containing source code is called a source file The source file is designed for people After a source file has been created, the programmer must invoke the C compiler.

Writing C Programs The source file is designed for people, not computers The algorithms expressed in the source file must be translated into executable machine language To complete this translation, the programmer must invoke the C compiler. A compiler is a program that automatically translates source code to machine code.

The Compiler Object Code Compiler Source Code

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 cc compiler, because the error messages given by the cc compiler are easier for beginners to understand.

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

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. Both the cc and the gcc compilers name the executable file a.out To execute the program, type a.out at the Unix prompt. Although we call this “compiling a program”, what actually happens is more complicated.

3 Stages of Compilation 1: Preprocessor Modifies the source code Substitutes user-defined abbreviations #define CSE_NUM CMSC104 #define INST_NAME “Mr. Olsen” Handles preprocessor directives #define COMPILER_VER 2.2.7 Strips comments and whitespace from the code

3 Stages of Compilation 2: Compiler Translates the modified source code into object code (machine code that does not yet have the libraries necessary to run) Parser - breaks program into lexical units and checks for errors Code Generator - makes the object code Optimizer - may change the code to be more efficient

Parsing Based on syntax (“lexical unit”) a = b + c; printf(“This is a sample”); if(a == b){ c = x + y; }

3 Stages of Compilation 3: Linker Linker - combines the object code of our program with other object code to produce the executable file. The 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 object files other libraries

Compilation Diagram Editor < Compiler Preprocessor Parser Source File myprog.c Compiler Preprocessor Parser Code Generator Optimizer Object File myprog.o Other Obj’s Run-time library Other libraries Linker Executable file a.out

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

Incremental Approach to Writing Code 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.

Incremental Approach to Writing Code (continued) 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.

A Simple C Program /* Filename: hello.c Author: Peter Olsen Date written: 10/9/99 Description: This program prints the greeting “Hello, World!” */ #include <stdio.h> /* Tells how to call IO lib */ main ( ) { printf (“Hello, World!\n”) ; }

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

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

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.

main ( ) Every program has a function called main, where execution begins --- and where execution should end. The parenthesis following main indicate to the compiler that it is a function.

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.

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 entity. Notice that this line ends with a semicolon. All statements in C end with a semicolon.

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

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: Naming conventions Use of whitespace Use of Braces Comments

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

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

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(), braces are in column 1

Use of Whitespace (continued) All executable statements are indented at least one tab stop. How deep should my tabs be ? Typically 3 or 4 spaces. 2 is not enough for good readability, more than four causes indentation to be too deep.