Data Types in C. A Data Type A data type is –A set of values AND –A set of operations on those values A data type is used to –Identify the type of a variable.

Презентация:



Advertisements
Похожие презентации
Unit II Constructor Cont… Destructor Default constructor.
Advertisements

Operator Overloading Customised behaviour of operators Chapter: 08 Lecture: 26 & 27 Date:
Basic Input - Output. Output functions printf() – is a library function that displays information on-screen. The statement can display a simple text message.
Data Variable and Pointer Variable Pass by Reference Pointer Arithmetic Passing Array Using Pointers Dynamic Allocation.
A class is just a collection of variables--often of different types--combined with a set of related functions. The variables in the class are referred.
Object Oriented Programming Ashraf Zia Lecturer Abdul Wali Khan University, Mardan. Lecture - 2.
Inner Classes. 2 Simple Uses of Inner Classes Inner classes are classes defined within other classes The class that includes the inner class is called.
2005 Pearson Education, Inc. All rights reserved. 1 Object-Oriented Programming: Interface.
Sequences Sequences are patterns. Each pattern or number in a sequence is called a term. The number at the start is called the first term. The term-to-term.
1/27 Chapter 9: Template Functions And Template Classes.
1 Chapter 4 Macro Processors. 2 Introduction A macro instruction (abbreviated to macro) is simply a notational convenience for the programmer. A macro.
Operators and Arithmetic Operations. Operators An operator is a symbol that instructs the code to perform some operations or actions on one or more operands.
Michael Marchenko. In mathematics, a sequence is an ordered list of objects (or events). Like a set, it contains members (also called elements, or terms),
© 2009 Avaya Inc. All rights reserved.1 Chapter Two, Voic Pro Components Module Two – Actions, Variables & Conditions.
1 Arrays and Strings Lecture: Design Problem l Consider a program to calculate class average Why?? ?
Object Oriented Programming Ashraf Zia Lecturer Abdul Wali Khan University, Mardan. Lecture - 1.
Factorial in mathematics, the factorial of a non-negative integer n, denoted by n!, is the product of all positive integers less than or equal to n. For.
1/30 Chapter 8: Dynamic Binding And Abstract classes.
Pointers Part I (Fundamentals) Computer Programming I Lecture 12 Copyright (C) 2004 by Wong Ya Ping updated : ver080415(KCLee)
A Short Review Arrays, Pointers and Structures. What is an Array? An array is a collection of variables of the same type and placed in memory contiguously.
Транксрипт:

Data Types in C

A Data Type A data type is –A set of values AND –A set of operations on those values A data type is used to –Identify the type of a variable when the variable is declared –Identify the type of the return value of a function –Identify the type of a parameter expected by a function

A Data Type (continued) When the compiler encounters a declaration for a variable, it sets up a memory location for it An operator used on a variable or variables is legal only if –The operator is defined in that programming language for a variable of that type –The variable or variables involved with the operator are of the same or compatible types

Rules for Constructing Identifiers in C Capital letters A-Z, lowercase letters a-z, digits 0- 9, and the underscore character First character must be a letter or underscore Usually only the first 32 characters are significant There can be no embedded blanks Keywords cannot be used as identifiers Identifiers are case sensitive Identifiers refer to the names of data types, constants, variables, and functions

Two Classifications of Data Types Built-in data types –Fundamental data types ( int, char, double, float, void, pointer ) –Derived data types (array, string, structure) Programmer-defined data types –Structure –Union –Enumeration

Fundamental Data Types void – used to denote the type with no values int – used to denote an integer type char – used to denote a character type float, double – used to denote a floating point type int *, float *, char * – used to denote a pointer type, which is a memory address type

Uses of Fundamental Data Types int elevationIndicator; char inputSymbol; float totalCost; int main (void) { double grossProduct; int *temperatureValuePtr; grossProduct = ; inputSymbol = 'a'; return (0); } // End main

Derived Data Types Array – a finite sequence (or table) of variables of the same data type String – an array of character variables Structure – a collection of related variables of the same and/or different data types. The structure is called a record and the variables in the record are called members or fields

Uses of Derived Data Types int elevationTable[20]; char inputSymbols[] = "Hello World"; struct operationsStruct { double heatReading; int temperatureValue; float speedMeter; char actionCode; }; // End struct struct operationsStruct currentOperations;

Records (Structures) A record permits a programmer to handle a group of variables as one variable The fields (members) of a record can be any built- in or programmer-defined data type A record can have values assigned to and read from it just like the built-in variable types A record can also be passed as an argument to a function and serve as the return value for a function

The typedef Keyword and Records The typedef keyword can be used to create a synonym for a data type. It is most often used to simplify the naming and use of record types (i.e., structure types). typedef struct { double heatReading; int temperatureValue; float speedMeter; char actionCode; } operationsRecordType; operationsRecordType currentOperations; operationsRecordType futureOperations;

Basic Operations on Records currentOperations.speedMeter = 245.6; currentOperations.temperatureValue = 67; currentOperations.actionCode = 'z'; latestReading = currentOperations.heatReading; statusFactor = currentOperations.speedMeter * currentOperations.temperatureValue; futureOperations = currentOperations; printf("Temp: %d Code: %c\n", futureOperations.temperatureValue, futureOperations.actionCode);