Computer Science Department FTSM Variables and Constants Knowledge: Understand the concept of storage location representation as identifiers Skill: Identify.

Slides:



Advertisements
Similar presentations
Variables in C Amir Haider Lecturer.
Advertisements

Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 2 Simple C Programs.
Computer Programming w/ Eng. Applications
1 C++ Syntax and Semantics The Development Process.
 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.
1 ICS103 Programming in C Lecture 3: Introduction to C (2)
Software Development Method. Assignments Due – Homework 0, Warmup Reading – Chapter 2 –
Structure of a C program
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.
Variables, Data Types, & Arithmetic Expressions CSC 1401: Introduction to Programming with Java Lecture 3 Wanda M. Kunkle.
1 Key Concepts:  Data types in C.  What is a variable?  Variable Declaration  Variable Initialization  Printf()  Scanf()  Working with numbers in.
CS150 Introduction to Computer Science 1
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the structure of a C-language program. ❏ To write your first C.
Basic Input/Output and Variables Ethan Cerami New York
Basic Elements of C++ Chapter 2.
Computer Science Department FTSM Input and Output Knowledge: Understand various types of input and output syntaxes Skill: Develop a program to read/capture.
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.
Chapter 2 : Overview of C By Suraya Alias. /*The classic HelloWorld */ #include int main(void) { printf(“Hello World!!"); return 0; }
Goals of Course Introduction to the programming language C Learn how to program Learn ‘good’ programming practices.
Chapter 2 Overview of C Part I J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei University.
UniMAP Sem2-08/09 DKT121: Fundamental of Computer Programming1 Introduction to C – Part 2.
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,
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
King Saud University College of applied studies and community services CSC 1101 Computer Programming I Lecture 2.
CPS120: Introduction to Computer Science
UniMAP Sem1-07/08EKT120: Computer Programming1 Week2.
Chapter 2. C++ Program Structure C++ program is a collection of subprograms Subprograms in C++ are called FUNCTIONS Each function performs a specific.
Constants Numeric Constants Integer Constants Floating Point Constants Character Constants Expressions Arithmetic Operators Assignment Operators Relational.
1 INTRODUCTION TO PROBLEM SOLVING AND PROGRAMMING.
Lecture 2: Introduction to C Programming. OBJECTIVES In this lecture you will learn:  To use simple input and output statements.  The fundamental data.
Course Title: Object Oriented Programming with C++ instructor ADEEL ANJUM Chapter No: 03 Conditional statement 1 BY ADEEL ANJUM (MSc-cs, CCNA,WEB DEVELOPER)
BASICS CONCEPTS OF ‘C’.  C Character Set C Character Set  Tokens in C Tokens in C  Constants Constants  Variables Variables  Global Variables Global.
CC112 Structured Programming Lecture 2 1 Arab Academy for Science and Technology and Maritime Transport College of Engineering and Technology Computer.
CSCI 3133 Programming with C Instructor: Bindra Shrestha University of Houston – Clear Lake.
Overview of C. C—a high-level programming language developed in 1972 by Dennis Ritchie at AT&T Bell Laboratories. We will discuss: –the elements of a.
Constants, Variables and Data types in C The C character Set A character denotes any alphabet, digit or special symbol used to represent information.
Arithmetic Expressions in C++. CSCE 1062 Outline Data declaration {section 2.3} Arithmetic operators in C++ {section 2.6} Mixed data type arithmetic in.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 2A Reading, Processing and Displaying Data (Concepts)
1 09/03/04CS150 Introduction to Computer Science 1 What Data Do We Have.
 constant represented by a name, just like a variable, but whose value cannot be changed  The const keyword precedes the type, name, and initialization.
Principles of Programming CSEB134 : BS/ CHAPTER Fundamentals of the C Programming Language.
UNIMAP Sem2-07/08EKT120: Computer Programming1 Week 2 – Introduction to Programming.
Chapter 4: Variables, Constants, and Arithmetic Operators Introduction to Programming with C++ Fourth Edition.
A Sample Program #include using namespace std; int main(void) { cout
1 C Syntax and Semantics Dr. Sherif Mohamed Tawfik Lecture Two.
C Language Elements Preprocessor Directives # (sign for preprocessor directive commands) #include Standard header file (.h) Library.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the structure of a C-language program. ❏ To write your first C.
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 Topics The Basics of a C++ Program Data Types
Computer Science Department
User Interaction and Variables
Introduction to the C Language
Basic Elements of C++.
Revision Lecture
ICS103 Programming in C Lecture 3: Introduction to C (2)
Lecture2.
Basic Elements of C++ Chapter 2.
Introduction to the C Language
Introduction to the C Language
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
CS111 Computer Programming
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
Introduction to C Programming
C Programming Lecture-3 Keywords, Datatypes, Constants & Variables
Getting Started With Coding
Presentation transcript:

Computer Science Department FTSM Variables and Constants Knowledge: Understand the concept of storage location representation as identifiers Skill: Identify and define data types in C programs

TK1913-C Programming2 TK1913-C Programming 2 Introduction Consider the following example: #include void main() { printf(“Welcome to UKM\n”); } #include void main() { printf(“Welcome to UKM\n”); } Let’s recap… Which one is the preprocessor instruction? Which one is the main function?

TK1913-C Programming3 TK1913-C Programming 3 C Program Structure Preprocessor Instruction Pengisytiharan globl void main (void) { } Pengisytiharan setempat Statement Global Declaration Local Declaration Still remember this diagram?

TK1913-C Programming4 TK1913-C Programming 4 Identifiers Identifiers are: Variable Constant Function Others

TK1913-C Programming5 TK1913-C Programming 5 Identifiers Syntax Rules: Consist of only letters, digits and underscores Cannot begin with a digit Cannot use C reserved words Try not to use/redefine C standard identifiers What are C reserved words? What are C standard identifiers?

TK1913-C Programming6 TK1913-C Programming 6 C Reserved Word A word that has special meaning in C C Standard Identifier A word having special meaning but may be redefined (but is not recommended!!) Examples of reserved word: int, void, double, return Examples of standard identifiers: printf, scanf Examples of reserved word: int, void, double, return Examples of standard identifiers: printf, scanf Identifiers

TK1913-C Programming7 TK1913-C Programming 7 Variable A name associated with a memory cell whose value can change Needs to be declared: variable_type variable_name; variable_type variable_name; int x; Example: int x; int entry_time, charge; int entry_time, charge; int x; Example: int x; int entry_time, charge; int entry_time, charge;

TK1913-C Programming8 TK1913-C Programming 8 Variable Types of variable: char Character: char An individual character value – a letter, a digit or a symbol (e.g. ‘A’, ‘4’, ‘*’) int Integer: int Whole numbers (e.g. +16, 568, -456) float Float: float A real number which has a decimal point (e.g. 8.00, ) double High-level Float: double

TK1913-C Programming9 TK1913-C Programming 9 Variable Variable Declaration: Example 1: char letter; char letter; letter letter is a character-type variable Example 1: char letter; char letter; letter letter is a character-type variable Example 2: float matric; float matric; matric matric is a ??? variable Example 2: float matric; float matric; matric matric is a ??? variable

TK1913-C Programming10 TK1913-C Programming 10 Variable Example: Calculate and display the price of a number of apples if the quantity in kg and price per kg are given. quantityprice_per_kgInput:quantity and price_per_kg priceOutput:price pricequantityprice_per_kgProcess:price = quantity * price_per_kg Example: Calculate and display the price of a number of apples if the quantity in kg and price per kg are given. quantityprice_per_kgInput:quantity and price_per_kg priceOutput:price pricequantityprice_per_kgProcess:price = quantity * price_per_kg

TK1913-C Programming11 TK1913-C Programming 11 Variable Example: int quantity; float price_per_kg; float price; Example: int quantity; float price_per_kg; float price; Why did we declare it as int? Why did we declare them as float?

TK1913-C Programming12 TK1913-C Programming 12 Example: int number1, number2; number1 = 25; number2 = 23; number1 = number2; … Example: int number1, number2; number1 = 25; number2 = 23; number1 = number2; … Variable - Value Assignment number1 ? number2 ? 2523

TK1913-C Programming13 TK1913-C Programming 13 Variable - Value Assignment Algorithm variable  expression Syntax variable = expression; Rules Expression’s type must be the same as variable’s type Valid Example:Invalid Example: int x; int y; x = 12;y = 5.75; Valid Example:Invalid Example: int x; int y; x = 12;y = 5.75;

TK1913-C Programming14 TK1913-C Programming 14 Variable - Value Assignment Example: int quantity; float price_per_kg, price; quantity = 5; price_per_kg = 4.50; price = quantity * price_per_kg; … Example: int quantity; float price_per_kg, price; quantity = 5; price_per_kg = 4.50; price = quantity * price_per_kg; … How does this program work?

TK1913-C Programming15 TK1913-C Programming 15 Example: int quantity; float price_per_kg, price; quantity = 2; price_per_kg = 4.50; price = quantity * price_per_kg; … Example: int quantity; float price_per_kg, price; quantity = 2; price_per_kg = 4.50; price = quantity * price_per_kg; … Variable - Value Assignment quantity ? price_per_kg ? price ?

TK1913-C Programming16 TK1913-C Programming 16 Variable - Value Assignment Example: int number1, number2; number1 = 25; number2 = 23; number1 = number2; … Example: int number1, number2; number1 = 25; number2 = 23; number1 = number2; … How does this program segment work?

TK1913-C Programming17 TK1913-C Programming 17 Constant A value that will not change Consists of: Float (e.g F 1.2e-5) Integer (e.g ) Character(e.g. ‘z’ ‘3’ ‘$’ ‘\n’) String (e.g. “UKM” “1” “5a”)

TK1913-C Programming18 TK1913-C Programming 18 Exercise To practice what you’ve learned, try exercises on page 83 (Latih Diri) from Pengaturcaraan C

TK1913-C Programming19 TK1913-C Programming 19 End of Lecture 4 What’s next? …INPUT AND OUTPUT on the way … If you want to excel: revise chapter 4 revise chapter 4 read chapter 5 & 6 for next week … read chapter 5 & 6 for next week … Otherwise, you may watch tv, sleep etc. as a preparation for next week classes.