UniMAP Sem2-08/09 DKT121: Fundamental of Computer Programming1 Introduction to C – Part 2.

Slides:



Advertisements
Similar presentations
Types and Arithmetic Operators
Advertisements

Lecture 2 Introduction to C Programming
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.
Principles of Programming Fundamental of C Programming Language and Basic Input/Output Function 1.
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 3: Primitive Data Types.
1 9/20/06CS150 Introduction to Computer Science 1 Review: Exam 1.
Introduction to C Programming Overview of C Hello World program Unix environment C programming basics.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
JavaScript, Third Edition
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.
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 2 Getting Started in C Programming
A First Book of ANSI C Fourth Edition
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
C Tokens Identifiers Keywords Constants Operators Special symbols.
Chapter 2 Basic Elements of Java. Chapter Objectives Become familiar with the basic components of a Java program, including methods, special symbols,
CS1 Lesson 2 Introduction to C++ CS1 Lesson 2 -- John Cole1.
Sales person receive RM200/week plus 9% of their gross sales for that week. Write an algorithms to calculate the sales person’s earning from the input.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
CH2 – Using Data. Constant Something which cannot be changed Data Type Format and size of a data item Intrinsic Data Types Pg. 47 – Table 2-1 Basic ones.
Chapter 2: Using Data.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 2: Basic Elements of C++
UniMAP Sem1-07/08EKT120: Computer Programming1 Week2.
Lecture #5 Introduction to C++
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
C++ Programming: Basic Elements of C++.
Constants Numeric Constants Integer Constants Floating Point Constants Character Constants Expressions Arithmetic Operators Assignment Operators Relational.
Week 2 Introduction to Computer Programming/ C Programming Language 1 EKT120: Computer Programming.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
Lecture 2: Introduction to C Programming. OBJECTIVES In this lecture you will learn:  To use simple input and output statements.  The fundamental data.
CSCI 171 Presentation 2. Program Components main() #include Variable Definition Function Prototype Program Statements Function Call Function Definition.
Computer Engineering 1 st Semester Dr. Rabie A. Ramadan 3.
Introduction to Programming
VARIABLES, CONSTANTS, OPERATORS ANS EXPRESSION
KUKUM Sem1-05/06EKT120: Computer Programming1 Week 2.
CSCI 3133 Programming with C Instructor: Bindra Shrestha University of Houston – Clear Lake.
COMP Primitive and Class Types Yi Hong May 14, 2015.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
C++ for Engineers and Scientists Second Edition
Tokens in C  Keywords  These are reserved words of the C language. For example int, float, if, else, for, while etc.  Identifiers  An Identifier is.
UniMAP Sem II-10/11EKT120: Computer Programming1 Week 1 – Introduction to Computer and Algorithm (Part 2)‏
Lecture 2. Outline Sample programming question Sample C program Identifiers and reserve words Program comments Preprocessor directives Data types and.
UniMAP Sem I-09/10EKT150: Computer Programming1 Week 2 – Introduction to Computer and Algorithm.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
Java Basics. Tokens: 1.Keywords int test12 = 10, i; int TEst12 = 20; Int keyword is used to declare integer variables All Key words are lower case java.
UNIMAP Sem2-07/08EKT120: Computer Programming1 Week 2 – Introduction to Programming.
1 st Semester Module2 Basic C# Concept อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer Engineering.
Introduction to Algorithmic Processes CMPSC 201C Fall 2000.
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.
Bill Tucker Austin Community College COSC 1315
Chapter 2: Basic Elements of C++
Computer Programming BCT 1113
Week 1 – Introduction to Computer and Algorithm (Part 2)‏
BASIC ELEMENTS OF A COMPUTER PROGRAM
EKT150 INTRODUCTION TO COMPUTER PROGRAMMING
ITEC113 Algorithms and Programming Techniques
Revision Lecture
ICS103 Programming in C Lecture 3: Introduction to C (2)
Java Programming: From Problem Analysis to Program Design, 4e
By: Syed Shahrukh Haider
Variables In programming, we often need to have places to store data. These receptacles are called variables. They are called that because they can change.
Character Set The character set of C represents alphabet, digit or any symbol used to represent information. Types Character Set Uppercase Alphabets A,
Introduction to C Programming
Lecture3.
Session 1 – Introduction to Computer and Algorithm (Part 2)‏
DATA TYPES There are four basic data types associated with variables:
Introduction to C Programming
Presentation transcript:

UniMAP Sem2-08/09 DKT121: Fundamental of Computer Programming1 Introduction to C – Part 2

UniMAP Sem2-08/09 DKT121: Fundamental of Computer Programming2 Outline Identifiers and reserve words Program comments Preprocessor directives Data types and type declarations Sample programming question Sample C program Operators Formatted input and output Program debugging

UniMAP Sem2-08/09 DKT121: Fundamental of Computer Programming3 Identifiers & Reserve Words Identifiers labels for program elements case sensitive can consists of capital letters[A..Z], small letters[a..z], digit[0..9], and underscore character _ First character MUST be a letter or an underscore No blanks Reserve words cannot be identifiers Reserve words already assigned to a pre-defined meaning eg: delete, int, main, include, double, for, if etc.

UniMAP Sem2-08/09 DKT121: Fundamental of Computer Programming4 Program comments Starts with /* and terminate with */ OR Character // start a line comment, if several lines, each line must begin with // Comments cannot be nested /* /* */*/

UniMAP Sem2-08/09 DKT121: Fundamental of Computer Programming5 Preprocessor directives An instruction to pre-processor Standard library header (p154,Deitel) E.g. #include for std input/output #include Conversion number-text vise-versa, memory allocation, random numbers #include string processing

UniMAP Sem2-08/09 DKT121: Fundamental of Computer Programming6 Data Types & Mem. Alloc. Data TypeDescription Size (bytes) char A single character. Internally stored as a coded integer value (refer to ASCII table ). 1 int Integer quantity. Can be represented in signed or unsigned form (with the unsigned keyword). 4 float Floating-point number. Set of real numbers.4 double A more precise version of float. Has larger dynamic range and better representation of decimal points. 8 bool Boolean representation of logic states. Can only be assigned true (1) or false (0). 1

UniMAP Sem2-08/09 DKT121: Fundamental of Computer Programming7 Type declarations float income; float net_income;  int index =0, count =0;  char ch=‘a’, ch2;  const float epf = 0.1, tax = 0.05; float income, net_income; Declare and initialize Named constant declared and initialized

UniMAP Sem2-08/09 DKT121: Fundamental of Computer Programming8 Types of operators Types of operators are: Arithmetic operators (+, -, *, /, %) Relational operators (>, =, <=, !=) Logical operators (&&, ||) Compound assignment operator (+=, -=, *=, /=, %=) Binary operators: needs two operands Unary operators: single operand Bitwise operators: executes on bit level

UniMAP Sem2-08/09 DKT121: Fundamental of Computer Programming9 Sample Programming Question Write a program that calculate nett income.Your program should read income from user. Given tax rate is 10% and epf rate is 5% from income. Steps: Analyze the problem Use algorithm Convert to actual codes

UniMAP Sem2-08/09 DKT121: Fundamental of Computer Programming10 Sample C Program // Program name : program1.c //Programmer : Yasmin //This program reads income and calculate nett income //after epf and tax deduction #include int main(void) { float income, net_income; const float epf=0.1, tax=0.05; printf(“Enter income : “); scanf(“%f”, &income); net_income=income-(epf*income) – (tax*income); printf(“\nNett income : %5.2f”, net_income); return 0; } The terms void indicates we receive nothing from OS and return an integer to OS Variable & constant declaration begin end Return 0(int) to OS body Comments Preprocessor directives

UniMAP Sem2-08/09 DKT121: Fundamental of Computer Programming11 Arithmetic Operators Used to execute mathematical equations The result is usually assigned to a data storage (instance/variable) using assignment operator ( = ) E.g sum = marks1 + marks2;

UniMAP Sem2-08/09 DKT121: Fundamental of Computer Programming12 Arithmetic Operators C OperationArithmetic Operator Algebraic expression C expression Addition + f + 7 Subtraction - p – c p - c Multipication * bm b * m Division / x / y Remainder (Modulus) % r mod s r % s

UniMAP Sem2-08/09 DKT121: Fundamental of Computer Programming13 Exercise on arithmetic operators Given x = 20, y = 3 z = x % y = 20 % 3 = 2 (remainder)

UniMAP Sem2-08/09 DKT121: Fundamental of Computer Programming14 Relational and Logical Operators Previously, relational operator: >, =, <=, ==, != Previously, logical operator: &&, || Used to control the flow of a program Usually used as conditions in loops and branches

UniMAP Sem2-08/09 DKT121: Fundamental of Computer Programming15 More on relational operators Relational operators use mathematical comparison (operation) on two data, but gives logical output e.g1 let say b = 8, if (b > 10) e.g2 while (b != 10) e.g3 if(kod == 1) print(“Pegawai”); Reminder: Don’t confuse == (relational op.) with = (assignment op.)

UniMAP Sem2-08/09 DKT121: Fundamental of Computer Programming16 More on logical operators Logical operators are manipulation of logic e.g1 let say b=8, c=10, if ((b > 10) && (c<10)) e.g2 while ((b==8) ||(c > 10)) e.g3 if ((kod == 1) && (salary > 2213))

UniMAP Sem2-08/09 DKT121: Fundamental of Computer Programming17 Truth table for && (logical AND) operator exp1exp2exp1 && exp2 false truefalse truefalse true

UniMAP Sem2-08/09 DKT121: Fundamental of Computer Programming18 Truth table for || (logical OR) operator exp1exp2exp1 || exp2 false true falsetrue

UniMAP Sem2-08/09 DKT121: Fundamental of Computer Programming19 Compund assignment operator To calculate value from expression and store it in variable, we use assignment operator (=) Compound assignment operator combine binary operator with assignment operator E.g. val +=one; is equivalent to val = val + one; E.g. count = count -1; is equivalent to count -=1; count--; --count;

UniMAP Sem2-08/09 DKT121: Fundamental of Computer Programming20 count=count-2; count -=2; CANNOT USE count--; --count;

UniMAP Sem2-08/09 DKT121: Fundamental of Computer Programming21 Unary Operators Obviously operating on ONE operand Commonly used unary operators Increment/decrement { ++, -- } Arithmetic Negation { - } Logical Negation { ! } Usually using prefix notation Increment/decrement can be both a prefix and postfix

UniMAP Sem2-08/09 DKT121: Fundamental of Computer Programming22 Unary Operators (Eg.) Increment/decrement { ++, -- } prefix:value incr/decr before used in expression postfix:value incr/decr after used in expression Logical Negation { ! } bool isDinnerTime = true; bool isLunchTime = !isDinnerTime; val=5; printf(“%d”, ++val); Output: 6 val=5; printf(“%d”, --val); Output: 4 val=5; printf(“%d”, val++); Output: 5 val=5; printf(“%d”, val--); Output: 5

UniMAP Sem2-08/09 DKT121: Fundamental of Computer Programming23 Operator Precedence OperatorsPrecedence ! + -first * / %second + -third = >fourth == !=fifth &&sixth ||seventh =last

UniMAP Sem2-08/09 DKT121: Fundamental of Computer Programming24 Formatted Output with printf

UniMAP Sem2-08/09 DKT121: Fundamental of Computer Programming25 int %d float %f char %c long float %lf String %s etc Month= 12 Expense= 111.1

UniMAP Sem2-08/09 DKT121: Fundamental of Computer Programming26 Formatted Output with printf- cont

UniMAP Sem2-08/09 DKT121: Fundamental of Computer Programming27 Formatted input with scanf

UniMAP Sem2-08/09 DKT121: Fundamental of Computer Programming28 Formatted input with scanf- cont

UniMAP Sem2-08/09 DKT121: Fundamental of Computer Programming29 Program debugging Syntax error Mistakes caused by violating “grammar” of C C compiler can easily diagnose during compilation Run-time error Called semantic error or smart error Violation of rules during program execution C compiler cannot recognize during compilation Logic error Most difficult error to recognize and correct Program compiled and executed successfully but answer wrong

UniMAP Sem2-08/09 DKT121: Fundamental of Computer Programming30 Program debugging-syntax error snapshot

UniMAP Sem2-08/09 DKT121: Fundamental of Computer Programming31 Program debugging-run time error snapshot

UniMAP Sem2-08/09 DKT121: Fundamental of Computer Programming32 Program debugging-logic error snapshot

UniMAP Sem2-08/09 DKT121: Fundamental of Computer Programming33 End Introduction to C - Part 2 Q & A!