 Lecture 2 Introducing C. Lecture overview  Operator:=  Functions: main(), printf()  Putting together a simple C program  Creating integer-valued.

Slides:



Advertisements
Similar presentations
Fundamental of C Programming Language and Basic Input / Output Function Lecture 2.
Advertisements

Principles of Programming Chapter 3 Fundamental of C Programming Language and Basic Input/Output Function 1 NI S1 2009/10.
Lecture 2 Introduction to C Programming
Introduction to C Programming
 2005 Pearson Education, Inc. All rights reserved Introduction.
 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 Programming
Principles of Programming Fundamental of C Programming Language and Basic Input/Output Function 1.
Structure of a C program
CS1061 C Programming Lecture 2: A Few Simple Programs A. O’Riordan, 2004.
If You Missed Last Week Go to Click on Syllabus, review lecture 01 notes, course schedule Contact your TA ( on website) Schedule.
 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
Introduction to C Topics Compilation Using the gcc Compiler
Principles of Programming - NI Chapter 3 Fundamental of C Programming Language and Basic Input/Output Function.
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.
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.
© 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.
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
Chapter 2 Overview of C Part I J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei University.
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.
Programming With C.
/* Documentations */ Pre process / Linking statements Global declarations; main( ) { Local Declarations; Program statements / Executable statements; }
About the Presentations The presentations cover the objectives found in the opening of each chapter. All chapter objectives are listed in the beginning.
CHAPTER 2 PART #3 INPUT - OUTPUT 1 st semester King Saud University College of Applied studies and Community Service Csc
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.
Week 1 Algorithmization and Programming Languages.
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.
Constants Numeric Constants Integer Constants Floating Point Constants Character Constants Expressions Arithmetic Operators Assignment Operators Relational.
Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments.
CHAPTER 1: INTRODUCTION C++ Programming. CS 241 Course URL: Text Book: C++ How to Program, DETITEL & DEITEL, eighth Edition.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
CHAPTER 2 PART #3 C++ INPUT / OUTPUT 1 st Semester King Saud University College of Applied studies and Community Service CSC1101 By: Fatimah.
FUNCTIONS. Midterm questions (1-10) review 1. Every line in a C program should end with a semicolon. 2. In C language lowercase letters are significant.
 2007 Pearson Education, Inc. All rights reserved. A Simple C Program 1 /* ************************************************* *** Program: hello_world.
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
Principles of Programming CSEB134 : BS/ CHAPTER Fundamentals of the C Programming Language.
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 3.
 2003 Prentice Hall, Inc. All rights reserved Basics of a Typical C++ Environment C++ systems –Program-development environment –Language –C++
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:
Introduction to Algorithmic Processes CMPSC 201C Fall 2000.
L071 Introduction to C Topics Compilation Using the gcc Compiler The Anatomy of a C Program Reading Sections
Java Programming Fifth Edition Chapter 1 Creating Your First Java Classes.
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.
Chapter 1: Introduction to computers and C++ Programming
Chapter 2 Introduction to C++ Programming
CSC201: Computer Programming
Chapter 2 - Introduction to C Programming
Introduction to C Topics Compilation Using the gcc Compiler
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
Introduction to C++ Programming
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
Programming Fundamentals Lecture #3 Overview of Computer Programming
Lecture3.
Chapter 2: Introduction to C++.
Chapter 2 - Introduction to C Programming
Lexical Elements & Operators
Introduction to C Programming
Chapter 1 c++ structure C++ Input / Output
Presentation transcript:

 Lecture 2 Introducing C

Lecture overview  Operator:=  Functions: main(), printf()  Putting together a simple C program  Creating integer-valued variables, assigning them values, and displaying those values onscreen  The newline character ‘\n’  How to include comments in your programs, create programs containing more than one function, and find program errors.  Debugging

C Development Environment Disk Phase 2 : Preprocessor program processes the code. Disk Compiler Phase 3 : Compiler creates object code and stores it on Disk. Preprocessor Disk Linker Phase 4 : Editor (Source) Phase 1 : Program is created using the Editor and stored on Disk. Disk Linker links object code with libraries, creates a.out and stores it on Disk

Binary code generation Source file Preprocessed Source File Object File Executable File Preprocessing Compile Link PreprocessorCompilerLinker Header File Library.exe (Windows).lib (Windows).h.c

Execution processes Loader Phase 5 : :.:. Primary Memory Loader puts Program in Memory C P U (execute) Phase 6 : :.:. Primary Memory CPU takes each instruction and executes it, storing new data values as the program executes.

A Simple Example of C I am a simple computer. My favorite number is 1 because it is first.

#include Directives and Header Files The effect of #include is the same as if you had typed the entire contents of the stdio.h file into your file at the point where the #include line appears.

The main() Function A C program begins execution with the function called main(). The int is the main() function's return type. That means that the kind of value main() can return is an integer.

The main() Function  Example of main function declaration int main(void) { return 0; } void main(void) { } main(void) { } main( ) { }  'main' is a C keyword. We must not use it for any other variable

Comments  The parts of the program enclosed in the /* */ symbols are comments. Using comments makes it easier for someone (including yourself) to understand your program.  // Here is a comment confined to one line. int rigue; // Such comments can go here, too. comment

Braces, Bodies, and Blocks  In general, all C functions use braces to mark the beginning as well as the end of the body of a function.  Their presence is mandatory! Only braces ( { } ) work for this purpose, not parentheses (( )) and not brackets ([ ]). there must be just as many opening braces as closing braces

Declarations  This line declares two things.  Somewhere in the function, you have a variable called doors.  The int proclaims doors as an integer— that is, a number without a decimal point or fractional part.  The word int is a C keyword identifying one of the basic C data types  The word doors in this example is an identifier—that is, a name you select for a variable, a function, or some other entity.  Each statement in C needs to be terminated with semicolon (;)

Data types  C deals with several kinds (or types) of data:  integers,  characters,  and floating point, etc. Declaring a variable to be an integer or a character type makes it possible for the computer to store, fetch, and interpret the data properly.

Four Good Reasons to Declare Variables  Putting all the variables in one place makes it easier for a reader to grasp what the program is about.  Thinking about which variables to declare encourages you to do some planning before plunging into writing a program.  What information does the program need to get started?  What exactly do I want the program to produce as output?  What is the best way to represent the data?  Declaring variables helps prevent one of programming's more subtle and hard-to-find bugs—that of the misspelled variable name. RADIUS1 = 20.4; CIRCUM = 6.28 * RADIUSl; One letter l

Assignment  Assign the value 1 to the variable num: num = 1;  Earlier int num; line set aside space in computer memory for the variable num, and the assignment line stores a value in that location.

The printf() Function  printf("I am a simple ");  printf("computer.\n");  printf("My favorite number is %d because it is first.\n", num);  The parentheses () signify that printf is a function name.  The material enclosed in the parentheses is information passed from the main () function to the printf () function. Arguments

The printf() Function  printf("computer.\n"); The \n symbol means to start a new line the same function as pressing the Enter key of a typical keyboard.

Return Statement  return 0;  This return statement is the final statement of the program. The int in int main(void) means that the main() function is supposed to return an integer.

The Structure of a Simple Program preprocessor instructions declaration statement function name with arguments assignment statement function statements

Tips on Making Your Programs Readable  A readable program is much easier to understand, and that makes it easier to correct or modify. 1. Choose meaningful variable names; 2. Use comments; 3. Use blank lines to separate one conceptual section of a function from another. The following is correct, but ugly, code:Good code:

Multiple functions I will summon the butler function. You rang, sir? Yes. Bring me some tea and writeable CD- ROMS.

C Tokens  In a passage of text, individual words and punctuation marks are called tokens. C has six types of tokens.

Keywords autodogotosignedunsigned breakdoubleifsizeofvoid caseelseintstaticvolatile charenumlongstructwhile constexternregisterswitch continuefloatreturntypedef defaultforshortunion  Every C word is classified as either a keyword or an identifier.  All keywords have fixed meanings

Rules of identifiers  Identifiers refer to the names of variables, functions and arrays. These are user-defined names.  First character must be an alphabet (or underscore)  Must consist of only letters, digits or underscore.  Only first 31 characters are significant.  Cannot use a keyword.  Must not contain white space.  You should use meaningful names for variables  (such as car_count instead of x3 if your program counts sheep.).  The characters at your disposal are lowercase letters, uppercase letters, digits, and the underscore (_). The first character must be a letter or an underscore.

RulesExample Can contain a mix of characters and numbers. However it cannot start with a number H2o First character must be a letter or underscoreNumber1; _area Can be of mixed cases including underscore character XsquAre my_num Cannot contain any arithmetic operatorsR*S+T … or any other punctuation Cannot be a C keyword/reserved wordstruct; printf; Cannot contain a spaceMy height … identifiers are case sensitiveTax != tax Rules of identifiers

Constants  Constants in C refer to fixed values that do not change during the execution of a program.

Three types of errors: Syntax  Syntax Errors - a violation of the C grammar rules, detected during program translation (compilation).  You make a syntax error when you don’t follow C’s rules. It is similar to a grammatical error in English.  Example: Sing to likes he  He likes to sing

Three types of errors: Run-time  Run-time error - 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

 Semantic Errors and Logic errors are errors in meaning and design  Consider “The flying box thinks greenly.” The syntax is fine, but sentence does not mean anything.  In C the program following syntactically correct but wrong 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 Three types of errors: Semantic

Introducing Debugging Find errors!

Missing closing */ Should be { Should be } Missing ; Introducing Debugging

 Is the program syntactically correct?  What about semantics?  Will program operate as you expect? Introducing Debugging

 Is the program syntactically correct?  What about semantics?  Will program operate as you expect? Introducing Debugging n = 5, n squared = 25, n cubed = 625

Introducing Debugging Output

 By tracing the program step-by-step manually (on the paper), keeping track of variable you monitor the program state.  Add extra printf() statements throughout to monitor the values of selected variables at key points in the program.  Use debugger, that is a program that runs another program step-by-step and examine the value of the program’s variable. Introducing Debugging

 Find errors QUIZ