Principles of Programming CSEB134 : BS/20081 33 CHAPTER Fundamentals of the C Programming Language.

Slides:



Advertisements
Similar presentations
Designing a Program & the Java Programming Language
Advertisements

Fundamental of C Programming Language and Basic Input / Output Function Lecture 2.
An Introduction to Programming with C++ Fifth Edition Chapter 3 Completing the Problem-Solving Process and Getting Started with C++
Principles of Programming Chapter 3 Fundamental of C Programming Language and Basic Input/Output Function 1 NI S1 2009/10.
Programming Types of Testing.
Lecture 2 Introduction to C Programming
Introduction to C Programming
Principles of Programming Fundamental of C Programming Language and Basic Input/Output Function 1.
COSC 120 Computer Programming
1 ICS103 Programming in C Lecture 3: Introduction to C (2)
Structure of a C program
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.
C Programming Language 4 Developed in 1972 by Dennis Ritchie at AT&T Bell Laboratories 4 Used to rewrite the UNIX operating system 4 Widely used on UNIX.
Copyright © 2012 Pearson Education, Inc. Chapter 1: Introduction to Computers and Programming.
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.
Guide To UNIX Using Linux Third Edition
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 1 Introduction.
Principles of Programming - NI Chapter 3 Fundamental of C Programming Language and Basic Input/Output Function.
CHAPTER 1: INTORDUCTION TO C LANGUAGE
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.
Copyright 2003 Scott/Jones Publishing Brief Version of Starting Out with C++, 4th Edition Chapter 1 Introduction to Computers and Programming.
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.
Copyright © 2012 Pearson Education, Inc. Chapter 1: Introduction to Computers and Programming.
Chapter Introduction to Computers and Programming 1.
Introduction 01_intro.ppt
CSC 125 Introduction to C++ Programming Chapter 1 Introduction to Computers and Programming.
© Janice Regan, CMPT 128, Jan CMPT 128 Introduction to Computing Science for Engineering Students Creating a program.
UNIT 3 TEMPLATE AND EXCEPTION HANDLING. Introduction  Program errors are also referred to as program bugs.  A C program may have one or more of four.
Copyright © 2012 Pearson Education, Inc. Chapter 1: Introduction to Computers and Programming 1.
Copyright © 2012 Pearson Education, Inc. Chapter 1: Introduction to Computers and Programming.
Chapter 1: Introduction to Computers and Programming.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 1: Introduction to Computers and Programming.
Gator Engineering 1 Chapter 2 C Fundamentals Copyright © 2008 W. W. Norton & Company. All rights reserved.
Chapter 1: A First Program Using C#. Programming Computer program – A set of instructions that tells a computer what to do – Also called software Software.
Goals of Course Introduction to the programming language C Learn how to program Learn ‘good’ programming practices.
Chapter 3: Completing the Problem- Solving Process and Getting Started with C++ Introduction to Programming with C++ Fourth Edition.
Programming With C.
Computer Programming TCP1224 Chapter 3 Completing the Problem-Solving Process and Getting Started with C++
History of C 1950 – FORTRAN (Formula Translator) 1959 – COBOL (Common Business Oriented Language) 1971 – Pascal Between Ada.
/* Documentations */ Pre process / Linking statements Global declarations; main( ) { Local Declarations; Program statements / Executable statements; }
CHAPTER 2 PART #3 INPUT - OUTPUT 1 st semester King Saud University College of Applied studies and Community Service Csc
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.
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.
CC112 Structured Programming Lecture 2 1 Arab Academy for Science and Technology and Maritime Transport College of Engineering and Technology Computer.
1 Program Planning and Design Important stages before actual program is written.
CHAPTER 2 PART #3 C++ INPUT / OUTPUT 1 st Semester King Saud University College of Applied studies and Community Service CSC1101 By: Fatimah.
Brief Version of Starting Out with C++ Chapter 1 Introduction to Computers and Programming.
A.Abhari CPS1251 Topic 2: C Overview C Language Elements Variable Declaration and Data Types Statement Execution C Program Layout Formatting Output Interactive.
 Lecture 2 Introducing C. Lecture overview  Operator:=  Functions: main(), printf()  Putting together a simple C program  Creating integer-valued.
Copyright © 2014 Pearson Addison-Wesley. All rights reserved. Chapter 2 C++ Basics.
1 8/30/06CS150 Introduction to Computer Science 1 Your First C++ Program.
Chapter 1: Introduction to Computers and Programming.
CHAPTER 3 COMPLETING THE PROBLEM- SOLVING PROCESS AND GETTING STARTED WITH C++ An Introduction to Programming with C++ Fifth Edition.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 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.
CSC201: Computer Programming
Chapter 2 - 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.
Revision Lecture
ICS103 Programming in C Lecture 3: Introduction to C (2)
CHAPTER 3: Introduction to C Programming
Chapter 2 - Introduction to C Programming
Lecture3.
PROGRAMMING FUNDAMENTALS Lecture # 03. Programming Language A Programming language used to write computer programs. Its mean of communication between.
An Overview of C.
Chapter 1 c++ structure C++ Input / Output
Presentation transcript:

Principles of Programming CSEB134 : BS/ CHAPTER Fundamentals of the C Programming Language

Principles of Programming CSEB134 : BS/20082 Objectives C Program development environment Elements of the C language Program comments Preprocessor directives Functions Executable Statements General form of a C program Common Programming Errors: logic/design error, syntax error, run-time error

Principles of Programming CSEB134 : BS/20083 The system software necessary to develop C application program are bundled into an integrated development environment (IDE) A typical IDE contains text editor, C compiler, preprocessor, libraries, other tools The C programming environment has 2 components: Language – features to carry out basic operations (e.g.: store data in variables, compare 2 data values, perform arithmetic operation, etc.) Libraries – routines to carry out operations not part of the language (Standard libraries – e.g.: #include & programmer-defined libraries) C Program Development Environment

Principles of Programming CSEB134 : BS/20084 IDE – Microsoft Visual C++

Principles of Programming CSEB134 : BS/20085 Prepare Compile Link Execute Steps: These steps depend on the computer, the OS, and the C IDE that you will use.

Principles of Programming CSEB134 : BS/ Prepare program (and data files) Use a text editor to type your source program statements. Assign a name to your source program file (.C) and save the file into disk storage. (optional) Use the text editor to create a data files. Assign names to the data files (.txt or.dat) and save the files 2. Compile Perform by a compiler. C compiler has 2 separate programs: the preprocessor and the translator. Preprocessor reads the source code and prepares the code for the translator. Translator translates the code into machine language.

Principles of Programming CSEB134 : BS/ Link (“Build”) As we will see later, a C program is made up of many functions. The linker assembles all the functions into final executable program (creates exe file). 4. Execute If successful, it is ready for execution and get the output otherwise debug the program

Principles of Programming CSEB134 : BS/20088 Output: A Simple C Program Example

Principles of Programming CSEB134 : BS/20089 Elements of the C language /* Example Case: Apples Author: Mdm Badariah Solemon */ #include int main() { int Qty; double Cost=0.0, Total=0.0; printf ("Enter quantity of apples purchased (in Kg):"); scanf("%d", &Qty); printf ("Enter the cost per Kg of apples (in RM per Kg):"); scanf("%lf",&Cost); Total = Cost * Qty; printf("\nTotal cost of apples = %.2f\n",Total); return 0; } preprocessor directive variable comment special symbol reserved word punctuation standard header file statement

Principles of Programming CSEB134 : BS/ Program Comments Statement in a program intended for documentation and clarification purposes Have no effect on program execution. Comments are inserted into the code using /* and */ to surround comment or // to begin comment lines. /* This is a comment */ // This is known as comment line /* The comments can span into more than one lines */

Principles of Programming CSEB134 : BS/ Preprocessor directives The C program line that begins with # provides an instruction to the C preprocessor It is executed before the actual compilation is done. Two most common directives : #include #define In our example (#include ) identifies the header file for standard input and output needed by the printf().

Principles of Programming CSEB134 : BS/ Functions Every C program has a function main The function main usually calls input/output functions – printf(), scanf(), etc. These input/output functions are discussed in Chapter 4. The word main is a C reserved word. We must not use it for declaring any other variable or constant. 4 common ways of main declaration: int main(void) { return 0; } void main(void) { } main(void) { } main( ) { }

Principles of Programming CSEB134 : BS/ The curly braces { } identify a segment / body of a program The start and end of a function The start and end of the selection or repetition block. Since the opening brace { indicates the start of a segment with the closing brace indicating the end of a segment, there must be just as many opening braces as closing braces } (this is a common mistake of beginners)

Principles of Programming CSEB134 : BS/ Statements A specification of an action to be taken by the computer as the program executes. Each statement in C needs to be terminated with semicolon (;) Example: #include void main(void) { printf(“I love programming\n”); printf(“You will love it too once ”); printf(“you know the trick\n”); }

Principles of Programming CSEB134 : BS/ Statement has two parts : Declaration The part of the program that tells the compiler the names of memory cells in a program Executable statements Program lines (excluding comments) that are converted to machine language instructions and executed by the computer. A list of statements may be enclosed in braces { } (known as compound statement) int age, total=0.0;

Principles of Programming CSEB134 : BS/ General form of a C program preprocessor directives main function heading { declarations executable statements } #include void main(void) { // statement(s); }

Principles of Programming CSEB134 : BS/ C statements can extend over more than one line. Example: printf (“\n Your coins are worth %d dollars and %d cents.\n”, dollar, change); You can write more than one statement on a line (but not recommended). Example: printf(“Enter your name:”); scanf(“%”,&Name);

Principles of Programming CSEB134 : BS/ Common Programming Errors Debugging - Process removing errors from a program Three (3) kinds of errors : 1. Syntax Error a violation of the C grammar rules, detected during program translation (compilation). statement cannot be translated and program cannot be executed

Principles of Programming CSEB134 : BS/200819

Principles of Programming CSEB134 : BS/ Run-time errors An attempt to perform an invalid operation, detected during program execution. Occurs when the program directs the computer to perform an illegal operation, such as dividing a number by zero. The computer will stop executing the program, and displays a diagnostic message indicates the line where the error was detected ** ** depends on IDE – Ms Visual Studio: warning

Principles of Programming CSEB134 : BS/200821

Principles of Programming CSEB134 : BS/ Logic Error/Design Error An error caused by following an incorrect algorithm Very difficult to detect - it does not cause run-time error and does not display message errors. The only sign of logic error – incorrect program output Can be detected by testing the program thoroughly, comparing its output to calculated results To prevent – carefully desk checking the algorithm and written program before you actually type it

Principles of Programming CSEB134 : BS/ Summary You learned about: C Program development environment Elements of the C language Program comments Preprocessor directives Functions Executable Statements General form of a C program Common Programming Errors: syntax error, run-time error, logic/design error.