Lecture 2 Introduction to Computer Programming CUIT 101 1 A.M. Gamundani 2012 - - Presentation Layout Data types.

Slides:



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

C++ Basics Variables, Identifiers, Assignments, Input/Output.
CS0007: Introduction to Computer Programming Console Output, Variables, Literals, and Introduction to Type.
Primitive Data Types There are a number of common objects we encounter and are treated specially by almost any programming language These are called basic.
Java Syntax Part I Comments Identifiers Primitive Data Types Assignment.
CSE202: Lecture 2The Ohio State University1 Variables and C++ Data Types.
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 9/8/08CS150 Introduction to Computer Science 1 Data Types Section 2.7 – 2.12 CS 150 Introduction to Computer Science I.
22-Jun-15 Introduction to Primitives. 2 Overview Today we will discuss: The eight primitive types, especially int and double Declaring the types of variables.
If You Missed Last Week Go to Click on Syllabus, review lecture 01 notes, course schedule Contact your TA ( on website) Schedule.
CS1061 C Programming Lecture 4: Indentifiers and Integers A.O’Riordan, 2004.
Overview of C++ Chapter 2 in both books programs from books keycode for lab: get Program 1 from web test files.
CS150 Introduction to Computer Science 1
Basic Input - Output. Output functions  printf() – is a library function that displays information on-screen. The statement can display a simple text.
A Variable is symbolic name that can be given different values. Variables are stored in particular places in the computer ‘s memory. When a variable is.
Variables and Data Types
VARIABLES AND TYPES CITS1001. Types in Java the eight primitive types the unlimited number of object types Values and References The Golden Rule Scope.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 CST 221 OBJECT ORIENTED PROGRAMMING(OOP) ( 2 CREDITS.
CSE 1301 Lecture 2 Data Types Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.
CPS120: Introduction to Computer Science
Lecture #5 Introduction to C++
Primitive Variables.
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals. Procedural Constructs.
CSCI 130 Chapter 3. Variables & Names Variable Declarations: –reserve a storage location in memory –identify the name of the variable –identify the type.
CHAPTER # 2 Part 2 PROGRAMS AND DATA 1 st semster King Saud University College of Applied studies and Community Service CSC1101 By: Asma Alosaimi.
Primitive Variables.
CONSTANTS Constants are also known as literals in C. Constants are quantities whose values do not change during program execution. There are two types.
Copyright Curt Hill Variables What are they? Why do we need them?
Chapter 3 – Variables and Arithmetic Operations. Variable Rules u Must declare all variable names –List name and type u Keep length to 31 characters –Older.
Chapter 4 Literals, Variables and Constants. #Page2 4.1 Literals Any numeric literal starting with 0x specifies that the following is a hexadecimal value.
Chapter 7 C supports two fundamentally different kinds of numeric types: (a) integer types - whole numbers (1) signed (2) unsigned (b) floating types –
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.
CSI 3125, Preliminaries, page 1 Data Type, Variables.
Chapter2 Constants, Variables, and Data Types. 2.1 Introduction In this chapter, we will discuss –constants (integer, real, character, string, enum),symbolic.
1 Week 5 l Primitive Data types l Assignment l Expressions l Documentation & Style Primitive Types, Assignments, and Expressions.
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
1 09/03/04CS150 Introduction to Computer Science 1 What Data Do We Have.
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.
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.
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
CMSC 104, Version 8/061L09VariablesInC.ppt Variables in C Topics Naming Variables Declaring Variables Using Variables The Assignment Statement Reading.
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 3.
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
Course Contents KIIT UNIVERSITY Sr # Major and Detailed Coverage Area
User Interaction and Variables
Variables Mr. Crone.
BASIC ELEMENTS OF A COMPUTER PROGRAM
Revision Lecture
Variables ,Data Types and Constants
2008/10/01: Lecture 8 CMSC 104, Section 0101 John Y. Park
CMSC 104, Section 4 Richard Chang
Variables in C Topics Naming Variables Declaring Variables
Variables Numbers can be stored and retrieved while a program is running if they are given a home. The way that integers and decimal numbers are stored.
C++ Data Types Data Type
Variables in C Topics Naming Variables Declaring Variables
Chapter # 2 Part 2 Programs And data
Primitive Types and Expressions
© A+ Computer Science - Variables & Data Types © A+ Computer Science - ©A+ Computer Science
Variables in C Topics Naming Variables Declaring Variables
Variables in C Topics Naming Variables Declaring Variables
C Programming Lecture-3 Keywords, Datatypes, Constants & Variables
Variables in C Topics Naming Variables Declaring Variables
Variables and Constants
Variables in C Topics Naming Variables Declaring Variables
Getting Started With Coding
Presentation transcript:

Lecture 2 Introduction to Computer Programming CUIT A.M. Gamundani Presentation Layout Data types

C Data Types Summary A.M. Gamundani

Integer types:  Integers are whole numbers with a range of values, range of values are machine dependent.  Generally an integer occupies 2 bytes memory space and its value range limited to to (that is, to ).  A signed integer use one bit for storing sign and rest 15 bits for number.  To control the range of numbers and storage space, C has three classes of integer storage namely: short int, Int long int. 3 A.M. Gamundani

Integer types: Integers are whole numbers with a range of values, range of values are machine dependent. Generally an integer occupies 2 bytes memory space and its value range limited to to (that is, to ). A signed integer use one bit for storing sign and rest 15 bits for number. To control the range of numbers and storage space, C has three classes of integer storage namely: short int, int and long int. All three data types have signed and unsigned forms. A short int requires half the amount of storage than normal integer. Unlike signed integer, unsigned integers are always positive and use all the bits for the magnitude of the number. Therefore the range of an unsigned integer will be from 0 to The long integers are used to declare a longer range of values and it occupies 4 bytes of storage space. Syntax: int ; like int num1; short int num2; long int num3; Example: 5, 6, 100, A.M. Gamundani

5

6

Variables  A variable is a named data storage location in your computer's memory.  By using a variable's name in your program, you are, in effect, referring to the data stored there. 7 A.M. Gamundani

Variable Names  In C, variable names must adhere to the following rules: The name can contain letters, digits, and the underscore character (_). The first character of the name must be a letter. The underscore is also a legal first character, but its use is not recommended. Case matters (that is, upper- and lowercase letters). Thus, the names count and Count refer to two different variables. C keywords can't be used as variable names. A keyword is a word that is part of the C language. 8 A.M. Gamundani

Variable Names - Examples Variable NameLegality PercentLegal y2x5__fg7hLegal annual_profitLegal _1990_taxLegal but not advised savings#accountIllegal: Contains the illegal character # Double Illegal: Is a C keyword 9winter Illegal: First character is a digit 9 A.M. Gamundani

Dos and Don’ts of Variable Names  DO use variable names that are descriptive.  DO adopt and stick with a style for naming your variables.  DON'T start your variable names with an underscore unnecessarily.  DON'T name your variables with all capital letters unnecessarily. 10 A.M. Gamundani

Numeric Variable Types  C provides several different types of numeric variables that have different numeric values and varying memory storage requirements.  Use appropriate variable types, to ensure that your program runs as efficiently as possible.  C's numeric variables fall into the following two main categories: Integer variables hold values that have no fractional part (that is, whole numbers only). Integer variables come in two flavors: signed integer variables can hold positive or negative values, whereas unsigned integer variables can hold only positive values (and 0). Floating-point variables hold values that have a fractional part (that is, real numbers). 11 A.M. Gamundani

C's numeric data types Table. 12 A.M. Gamundani Variable TypeKeywordBytes RequiredRange Characterchar1-128 to 127 Integerint to Short integershort to Long integerlong4-2,147,483,648 to 2,147,438,647 Unsigned characterunsigned char10 to 255 Unsigned integerunsigned int20 to Unsigned short integerunsigned short20 to Unsigned long integerunsigned long40 to 4,294,967,295 Single-precisionfloat41.2E-38 to floating-point3.4E38 1 Double-precisiondouble82.2E-308 to floating-point1.8E Approximate range; precision = 7 digits. 2 Approximate range; precision = 19 digits.

Variable Declarations  Before you can use a variable in a C program, it must be declared.  A variable declaration tells the compiler the name and type of a variable and optionally initializes the variable to a specific value.  If your program attempts to use a variable that hasn't been declared, the compiler generates an error message. A variable declaration has the following form: typename varname; 13 A.M. Gamundani

Example (s) int count, number, start; /* three integer variables */ float percent, total; /* two float variables */ 14 A.M. Gamundani

The typedef Keyword  used to create a new name for an existing data type.  In effect, typedef creates a synonym.  For example, the statement typedef int integer;  creates integer as a synonym for int.  You then can use integer to define variables of type int, as in this example: integer count;  Note that typedef doesn't create a new data type; it only lets you use a different name for a predefined data type. 15 A.M. Gamundani

Initializing Numeric Variables  When you declare a variable, you instruct the compiler to set aside storage space for the variable.  However, the value stored in that space--the value of the variable--isn't defined.  Before using a variable, you should always initialize it to a known value. You can do this independently of the variable declaration by using an assignment statement, as in this example: int count; /* Set aside storage space for count */ count = 0; /* Store 0 in count */  Note that this statement uses the equal sign (=), which is C's assignment operator  You can also initialize a variable when it's declared. int count = 0;double percent = 0.01, taxrate = 28.5;  Be careful not to initialize a variable with a value outside the allowed range. int weight = ;unsigned int value = -2500;  The C compiler doesn't catch such errors. 16 A.M. Gamundani

Dos and Don’ts of Numeric Variables  DO understand the number of bytes that variable types take for your computer.  DO use typedef to make your programs more readable.  DO initialize variables when you declare them whenever possible.  DON'T use a variable that hasn't been initialized. Results can be unpredictable.  DON'T use a float or double variable if you're only storing integers. Although they will work, using them is inefficient.  DON'T try to put numbers into variable types that are too small to hold them.  DON'T put negative numbers into variables with an unsigned type 17 A.M. Gamundani

To be continued A.M. Gamundani