Guide To UNIX Using Linux Third Edition

Slides:



Advertisements
Similar presentations
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
Advertisements

C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
The Web Warrior Guide to Web Design Technologies
Lecture 2 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.
1 CS 161 Introduction to Programming and Problem Solving Chapter 9 C++ Program Components Herbert G. Mayer, PSU Status 10/20/2014.
COSC 120 Computer Programming
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.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 12 Separate Compilation Namespaces Simple Make Files (Ignore all class references.
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?
Chapter 8: Introduction to High-level Language Programming Invitation to Computer Science, C++ Version, Third Edition.
Guide To UNIX Using Linux Third Edition
Introduction to C Programming
Chapter 3: Introduction to C Programming Language C development environment A simple program example Characters and tokens Structure of a C program –comment.
Copyright 2003 Scott/Jones Publishing Brief Version of Starting Out with C++, 4th Edition Chapter 1 Introduction to Computers and Programming.
CMSC 104, Version 8/061L18Functions1.ppt Functions, Part 1 of 4 Topics Using Predefined Functions Programmer-Defined Functions Using Input Parameters Function.
Computer Science 210 Computer Organization Introduction to C.
Chapter Introduction to Computers and Programming 1.
Chapter 6: User-Defined Functions I Instructor: Mohammad Mojaddam
C++ for Engineers and Scientists Third Edition
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single.
Chapter 6: Modularity Using Functions. In this chapter, you will learn about: – Function and parameter declarations – Returning a single value – Returning.
A Guide to Unix Using Linux Fourth Edition
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.
Program A computer program (also software, or just a program) is a sequence of instructions written in a sequence to perform a specified task with a computer.
Chapter 06 (Part I) Functions and an Introduction to Recursion.
Chapter 6: User-Defined Functions
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
Project 1 Due Date: September 25 th Quiz 4 is due September 28 th Quiz 5 is due October2th 1.
© 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.
Course Title: Introduction to C++ Course Instructor: ADEEL ANJUM Chapter No: 01 1 BY ADEEL ANJUM (MCS, CCNA,WEB DEVELOPER)
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!
C Tutorial - Program Organization CS Introduction to Operating Systems.
C++ Basics C++ is a high-level, general purpose, object-oriented programming language.
CSC141 Introduction to Computer Programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture - 6.
Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments.
Functions Math library functions Function definition Function invocation Argument passing Scope of an variable Programming 1 DCT 1033.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
Announcements Assignment 1 due Wednesday at 11:59PM Quiz 1 on Thursday 1.
An overview of C Language. Overview of C C language is a general purpose and structured programming language developed by 'Dennis Ritchie' at AT &T's.
Principles of Programming CSEB134 : BS/ CHAPTER Fundamentals of the C Programming Language.
Functions, Part 1 of 3 Topics  Using Predefined Functions  Programmer-Defined Functions  Using Input Parameters  Function Header Comments Reading 
Chapter 7 - Functions. Functions u Code group that performs single task u Specification refers to what goes into and out of function u Design refers to.
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.
Chapter 1: Introduction to Computers and Programming.
Chapter 1 slides1 What is C? A high-level language that is extremely useful for engineering computations. A computer language that has endured for almost.
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.
User-Written Functions
Computer Science 210 Computer Organization
Chapter 7: User-Defined Functions II
CSC201: Computer Programming
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
User-Defined Functions
Computer Science 210 Computer Organization
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
File I/O in C Lecture 7 Narrator: Lecture 7: File I/O in C.
Chapter 2 - Introduction to C Programming
Homework Applied for cs240? (If not, keep at it!) 8/10 Done with HW1?
Chapter 2 - Introduction to C Programming
Programming Languages and Paradigms
Compiler vs linker The compiler translates one .c file into a .o file
C Language B. DHIVYA 17PCA140 II MCA.
Introduction to C Programming
Presentation transcript:

Guide To UNIX Using Linux Third Edition Chapter 10: Developing UNIX/Linux Applications in C and C++ Guide To UNIX Using Linux Third Edition

Objectives Understand basic elements of C programming Debug C programs Create, compile, and test C programs Use the make utility to revise and maintain source files Guide to UNIX Using Linux, Third Edition

Objectives (continued) Identify differences between C and C++ programming Create a simple C++ program Create a C++ program that reads a text file Create a C++ program that demonstrates how C++ enhances C functions Guide to UNIX Using Linux, Third Edition

Introducing C Programming C is the language in which UNIX was developed and refined C uses relatively short, isolated functions to break down large complex tasks into small and easily resolved subtasks C’s function-oriented design allows programmers to create their own functions to interact with the predefined system functions Guide to UNIX Using Linux, Third Edition

Creating a C Program A C program consists of separate bodies of code known as functions The functions call each other as needed and work to solve the problem for which the program was originally designed Guide to UNIX Using Linux, Third Edition

Creating a C Program (continued) Guide to UNIX Using Linux, Third Edition

Creating a C Program (continued) Guide to UNIX Using Linux, Third Edition

The C Library The core C language is very small C library consists of functions for many common activities including: File, screen, and keyboard operations String operations Memory allocation and control Math operations Guide to UNIX Using Linux, Third Edition

Program Format A program includes 1 or more functions Each program must have a main() function Format for main is: int main() { } Guide to UNIX Using Linux, Third Edition

Including Comments Comments begin with /* and end with */ Compiler ignores everything in the comment Comments can be anywhere in the file Guide to UNIX Using Linux, Third Edition

Using the Preprocessor #include Directive Using the preprocessor #include directive allowed for the output shown here Guide to UNIX Using Linux, Third Edition

Specifying Data Types Guide to UNIX Using Linux, Third Edition

Specifying Data Types (continued) Guide to UNIX Using Linux, Third Edition

Characters and Strings Characters are represented internally in a single byte of computer memory Character constants must be enclosed in single quotes Strings are represented as arrays of characters String constants must be enclosed in double quotes Guide to UNIX Using Linux, Third Edition

Variables Variables must be declared before use Declarations begin with a data type followed by one or more variable names The scope of a variable is the part of the program in which the variable is accessible Global variables can be accessed anywhere within a program Guide to UNIX Using Linux, Third Edition

Using Math Operators Guide to UNIX Using Linux, Third Edition

Generating Formatted Output with printf() The output of a simple C program Guide to UNIX Using Linux, Third Edition

Generating Formatted Output with printf() (continued) Guide to UNIX Using Linux, Third Edition

Using the C Compiler Command for C compiler in Linux is gcc In some UNIX systems, it is cc Default executable file produced is a.out Specify another name using the –o option Information on options and use available at man gcc Guide to UNIX Using Linux, Third Edition

if Statements and C Loops Use if statements to allow the program to make decisions depending on whether a condition is true or false Three looping mechanisms: for loop while loop do-while loop Guide to UNIX Using Linux, Third Edition

Using C Loops A C for loop generated the output in this program Guide to UNIX Using Linux, Third Edition

Defining Functions When a function is defined, its name is declared and its statements are written The data type of the return value must be declared in the function definition Or “void” if no return value Function prototypes must be included in programs to tell the compiler about functions that will be defined later Guide to UNIX Using Linux, Third Edition

Using Function Arguments A value passed to a function is an argument Arguments are stored in special automatic variables Can be referred to from anywhere in the function Guide to UNIX Using Linux, Third Edition

Using Function Return Values Functions can return values Returned values can be used in assignment statements such as y=triple(x) Must declare the type of the return value in the function definition Guide to UNIX Using Linux, Third Edition

Working with Files in C Files are continuous streams of data Typically stored on disk File pointers point to predefined structures that contain information about the file Before using a file, it must be opened The library function for this is fopen When done with a file, it must be closed The library function for this is fclose Guide to UNIX Using Linux, Third Edition

Working with Files in C (continued) File I/O uses various library functions fgetc performs character input fputc performs character output During an input operation, it is essential to test for the end of a file feof tests for the end-of-file marker Guide to UNIX Using Linux, Third Edition

Using the make Utility to Maintain Program Source Files Some programs have many files of source code Once compiled, the separate object files are linked into executable code As program is updated, only need to recompile files that have changed Guide to UNIX Using Linux, Third Edition

Using the make Utility to Maintain Program Source Files (continued) make utility helps tracks what needs to be recompiled by using the time stamp field for each source file A control file, called the makefile, lists the source files and their relationship to each other Guide to UNIX Using Linux, Third Edition

Using the make Utility to Maintain Program Source Files (continued) The make utility follows a set of rules General rule definition includes A target file One or more dependencies An action that creates the target Guide to UNIX Using Linux, Third Edition

Debugging Your Program The compiler identifies some types of errors in a program Common errors include: incorrect syntax, missing semicolons, case-sensitive errors Steps to correct syntax errors: Write down the error Edit the source file Save and recompile the file Guide to UNIX Using Linux, Third Edition

Creating a C Program to Accept Input Use the scanf function to accept input from the keyboard scanf uses a control string with format specified in a manner similar to printf scanf can accept multiple inputs, but remember that this can lead to difficult and cumbersome code Guide to UNIX Using Linux, Third Edition

Creating a C Program to Accept Input (continued) Guide to UNIX Using Linux, Third Edition

Creating a C Program to Accept Input (continued) Guide to UNIX Using Linux, Third Edition

Creating a C Program to Accept Input (continued) An example of using C to accept keyboard input Guide to UNIX Using Linux, Third Edition

Introducing C++ Programming C++ adds object-oriented capabilities to C C and C++ are similar in many ways C++ uses functions, as does C, but includes the use of functions as methods for objects Guide to UNIX Using Linux, Third Edition

Introducing C++ Programming (continued) The major differences between C and C++ C follows procedural principles, whereas C++ follows object-oriented principles C++ introduces “objects” A collection of data and a set of operations called methods to manipulate the data Guide to UNIX Using Linux, Third Edition

Creating a Simple C++ Program Using C++ instead of C to create a program Guide to UNIX Using Linux, Third Edition

How C++ Enhances C Functions Function overloading allows C++ to use the same function name for different types of arguments Guide to UNIX Using Linux, Third Edition

Chapter Summary C programs often consist of separate files called program modules that are compiled separately into object code and linked together to make up the program The core C language is small, relying on libraries for extended functionality UNIX was developed and refined in C Guide to UNIX Using Linux, Third Edition

Chapter Summary (continued) A compiler translates source code into object code A linker links all object code files plus library functions into an executable file The make utility maintains source files Only changed files need to be recompiled Guide to UNIX Using Linux, Third Edition

Chapter Summary (continued) C is procedural and C++ is object-oriented Function overloading: C++ offers a way to define a function to handle multiple sets of arguments Guide to UNIX Using Linux, Third Edition