Programming Declarations. COMP102 Prog. Fundamentals I: Declarations/ Slide 2 General form of a C++ program // Program description #include directives.

Slides:



Advertisements
Similar presentations
1 C++ Syntax and Semantics The Development Process.
Advertisements

Introduction to programming using C++
Principles of Programming Fundamental of C Programming Language and Basic Input/Output Function 1.
Sizes of simple data types sizeof(char) = 1 size(short) = 2 sizeof(int) = 4 size(long) = 8 sizeof(char) = 1 size(short) = 2 sizeof(int) = 2 size(long)
1 9/10/07CS150 Introduction to Computer Science 1 Data Types Section 2.7 – 2.12 CS 150 Introduction to Computer Science I.
Structure of a C program
1 Fundamental Data Types. 2 Declaration All variables must be declared before being used. –Tells the compiler to set aside an appropriate amount of space.
1 9/8/08CS150 Introduction to Computer Science 1 Data Types Section 2.7 – 2.12 CS 150 Introduction to Computer Science I.
CS1061 C Programming Lecture 4: Indentifiers and Integers A.O’Riordan, 2004.
The Fundamentals of C++ Basic programming elements and concepts JPC and JWD © 2002 McGraw-Hill, Inc.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
CS150 Introduction to Computer Science 1
1 Midterm Review COMP 102. Tips l Eat a light meal before the exam l NO electronic devices (including calculators, dictionaries, phones, pagers, etc.)
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the structure of a C-language program. ❏ To write your first C.
Chapter 3: Introduction to C Programming Language C development environment A simple program example Characters and tokens Structure of a C program –comment.
Programming Languages
CSCI 1100/1202 January 16, Why do we need variables? To store intermediate results in a long computation. To store a value that is used more than.
Input & Output: Console
CS2311 Computer Programming Dr. Yang, Qingxiong (with slides borrowed from Dr. Xu, Henry) Lecture 2: Basic Syntax – Part I: Variables and Constants.
3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 1 Learning the C++ language 3. The Nuts and Bolts of C++ (4)
C Tokens Identifiers Keywords Constants Operators Special symbols.
Lecture 2 Introduction to Computer Programming CUIT A.M. Gamundani Presentation Layout Data types.
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.
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Java Programing Basics COMP.
CPS120: Introduction to Computer Science
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Java Programing Basics COMP.
CISC105 – General Computer Science Class 9 – 07/03/2006.
PHY 107 – Programming For Science. Announcements  Slides, activities, & solutions always posted to D2L  Note-taking versions before class, for those.
Course Title: Object Oriented Programming with C++ instructor ADEEL ANJUM Chapter No: 03 Conditional statement 1 BY ADEEL ANJUM (MSc-cs, CCNA,WEB DEVELOPER)
Pointers: Basics. 2 What is a pointer? First of all, it is a variable, just like other variables you studied  So it has type, storage etc. Difference:
Introduction to Programming
CHAPTER # 2 Part 2 PROGRAMS AND DATA 1 st semster King Saud University College of Applied studies and Community Service CSC1101 By: Asma Alosaimi.
CONSTANTS Constants are also known as literals in C. Constants are quantities whose values do not change during program execution. There are two types.
CSCI 3133 Programming with C Instructor: Bindra Shrestha University of Houston – Clear Lake.
Chapter 4 Literals, Variables and Constants. #Page2 4.1 Literals Any numeric literal starting with 0x specifies that the following is a hexadecimal value.
Java Programming, Second Edition Chapter Two Using Data Within a Program.
Types of C Variables:  The following are some types of C variables on the basis of constants values it has. For example: ○ An integer variable can hold.
Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments.
Lecture 3 Introduction to Computer Programming CUIT A.M. Gamundani Presentation Layout from Lecture 1 Background.
Programming Fundamentals. Summary of previous lectures Programming Language Phases of C++ Environment Variables and Data Types.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
1 Comments Allow prose or commentary to be included in program Importance Programs are read far more often than they are written Programs need to be understood.
COMP 102 Programming Fundamentals I Presented by : Timture Choi COMP102 Lab 011.
1 Chapter 2 C++ Syntax and Semantics, and the Program Development Process Programming in C++
Types Chapter 2. C++ An Introduction to Computing, 3rd ed. 2 Objectives Observe types provided by C++ Literals of these types Explain syntax rules for.
C++ Basics Programming. COMP104 Lecture 5 / Slide 2 Introduction to C++ l C is a programming language developed in the 1970s with the UNIX operating system.
Chapter 4: Variables, Constants, and Arithmetic Operators Introduction to Programming with C++ Fourth Edition.
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 3.
1 A more complex example Write a program that sums a sequence of integers and displays the result. Assume that the first integer read specifies the number.
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Java Programing Basics COMP.
Chapter 2 Variables and Constants. Objectives Explain the different integer variable types used in C++. Declare, name, and initialize variables. Use character.
Chapter # 2 Part 2 Programs And data
BASIC ELEMENTS OF A COMPUTER PROGRAM
Variables A variable is a placeholder for a value. It is a named memory location where that value is stored. Use the name of a variable to access or update.
Multiple variables can be created in one declaration
Fundamental Data Types
Variables ,Data Types and Constants
DATA HANDLING.
Introduction to the C Language
Chapter 2: Java Fundamentals
Chapter # 2 Part 2 Programs And data
Pointers Pointers point to memory locations
Fundamental Data Types
What Actions Do We Have Part 1
C Data Types and Variable
DATA TYPES There are four basic data types associated with variables:
C Programming Lecture-3 Keywords, Datatypes, Constants & Variables
Variables and Constants
Data in a C++ Program Numeric data and character data
Getting Started With Coding
Presentation transcript:

Programming Declarations

COMP102 Prog. Fundamentals I: Declarations/ Slide 2 General form of a C++ program // Program description #include directives int main(){ constant declarations variable declarations executable statements return 0; }

COMP102 Prog. Fundamentals I: Declarations/ Slide 3 C++ Data Type A type defines a set of values and a set of operations that can be applied on those values. The set of values for each type is known as the domain for the type. C++ contains 5 standard types:

COMP102 Prog. Fundamentals I: Declarations/ Slide 4 void The void type has no values and no operations. In other words, both the set of values and the set of operations are empty. Although this might seem unusual, we will see later that it is a very useful data type.

COMP102 Prog. Fundamentals I: Declarations/ Slide 5 Integer An integer type is a number without a fractional part. It is also known as an integral number. C++ supports three different sizes of the integer data type: short int, int and long int. sizeof(short int)<= sizeof(int)<= sizeof(long int) Short int int long int

COMP102 Prog. Fundamentals I: Declarations/ Slide 6 Integer The type also defines the size of the field in which data can be stored. In C++, even though the size is machine dependent, most PCs use the integer sizes shown below. TypeSignByte Size Number of Bits Min ValueMax Value short intSigned unsigned intSigned unsigned ,147,483, ,147,483,647 4,294,967,295 long intSigned unsigned ,147,483, ,147,483,647 4,294,967,295

COMP102 Prog. Fundamentals I: Declarations/ Slide 7 Floating Point A floating-point type is a number with a fractional part, such as The C++ language supports three different sizes of floating-point: float, double and long double. sizeof(float)<= sizeof(double)<= sizeof(long double) float double long double

COMP102 Prog. Fundamentals I: Declarations/ Slide 8 Floating Point typeByte sizeNumber of Bits float432 double864 long double1080 Although the physical size of floating-point types is machine dependent, many computers support the sizes shown below.

COMP102 Prog. Fundamentals I: Declarations/ Slide 9 Declarations l Constants and variables must be declared before they can be used. l A constant declaration specifies the type, the name and the value of the constant. l A variable declaration specifies the type, the name and possibly the initial value of the variable. l When you declare a constant or a variable, the compiler: 1. Reserves a memory location in which to store the value of the constant or variable. 2. Associates the name of the constant or variable with the memory location. (You will use this name for referring to the constant or variable.) l For more on declarations, see and choose English--> C++ --> Declarations.

COMP102 Prog. Fundamentals I: Declarations/ Slide 10 Constant declarations l Constants are used to store values that never change during the program execution. l Using constants makes programs more readable and maintainable. Syntax: const = ; Examples: const double US2HK = 7.8; //Exchange rate of US$ to HK$ const double HK2TW = 3.98; //Exchange rate of HK$ to TW$ const double US2TW = US2HK * HK2TW; //Exchange rate of US$ to TW$

COMP102 Prog. Fundamentals I: Declarations/ Slide 11 l Variables are used to store values that can be changed during the program execution. l A variable is best thought of as a container for a value y Syntax: ; = ; Examples: int sum; int total = 3445; char answer = ' y ' ; double temperature = -3.14; Variable declarations

COMP102 Prog. Fundamentals I: Declarations/ Slide 12 A variable has a type and it can contain only values of that type. For example, a variable of the type int can only hold integer values. l Variables are not automatically initialized. For example, after declaration int sum; the value of the variable sum can be anything (garbage). l Thus, it is good practice to initialize variables when they are declared. l Once a value has been placed in a variable it stays there until the program deliberately alters it. Variable declarations

COMP102 Prog. Fundamentals I: Declarations/ Slide 13 Character data l A variable or a constant of char type can hold an ASCII character (see Appendix A of the textbook). l When initializing a constant or a variable of char type, or when changing the value of a variable of char type, the value is enclosed in single quotation marks. Examples: const char star = '*'; char letter, one = '1';

COMP102 Prog. Fundamentals I: Declarations/ Slide 14 Computers are Easy! l "Using a computer is just like riding a bike, except you don't have to wear the tight shorts and funny helmet. A water bottle is also a bad idea. Just about everyone agrees that computing is very simple, or will be in only a few more days. –David Lubar, 1995