Docsity

Fundamentals of Programming: Variables and Data Types, Lecture notes of C programming

Partial preview of the text

Download Fundamentals of Programming: Variables and Data Types and more Lecture notes C programming in PDF only on Docsity!

PROG

FUNDAMENTALS OF PROGRAMMING

Chapter 4

Variables and Data Types

Variables and Data Types

Topics

  • Variables
  • Constants
  • Data types
  • Declaration

Variables and Data Types

Variables

Example:

  • This is an example of programming expression.
  • x, y and z are variables.
  • Variables can represent numeric values, characters, character strings, or memory addresses.

z = x + y

Variables and Data Types

Variables

  • Variables store everything in your program.
  • The purpose of any useful program is to modify variables.
  • In a program every, variable has:
    • Name (Identifier)
    • Data Type
    • Size
    • Value

Variables and Data Types

Types of Variable

  • Local variables are those that are in scope within a specific part of the program (function, procedure, method, or subroutine, depending on the programming language employed).
  • Global variables are those that are in scope for the duration of the programs execution. They can be accessed by any part of the program, and are read- write for all statements that access them.

Variables and Data Types

Types of Variable

MAIN PROGRAM

Global Variables

Subroutine

Local Variable

Variables and Data Types

Rules in Naming a Variable

  • Following are some valid identifiers:
  • Following are some invalid identifiers:

x number1 a123 number_2 _xyz

print main space not allowed 123number name should not begin with number main reserved word

Variables and Data Types

Exercise

Explain these mathematical problems by using variables.

  1. Area of square
  2. Area of triangle
  3. Area of circle
  4. Average speed

Variables and Data Types

Constants

  • Example:
    • Calculate area of circle
  • Variable:
    • area, r
  • Constant

area = 3.142 * r * r

Variables and Data Types

Data Types

  • Data type is classification of a particular type of information.
  • Data types are essential to any computer programming language.
  • Without them, it becomes very difficult to maintain information within a computer program.
  • Different data types have different sizes in memory depending on the machine and compilers.

Variables and Data Types

Integer

  • A whole number, a number that has no fractional part.
  • The following are integers:
  • In contrast, the following are not integers:
  • There are often different sizes of integers available; for example, PCs support short integers, which are 2 bytes, and long integers, which are 4 bytes.

5.34 -1.0 1.3E4 "string"

Variables and Data Types

Floating-point

  • A number with a decimal point.
  • The following are floating-point numbers:
  • Floating-point representations are slower and less accurate than fixed-point representations, but they can handle a larger range of numbers.
  • It require more space.

Variables and Data Types

String

  • String is a sequence of character.
  • Example:
    • Kuala Lumpur
    • John White
    • Computer

Variables and Data Types

Boolean

  • This data type only have two possible values:
    • True
    • False
  • Use this data type for simple flags that track true/false conditions.