21
PROGRAMMING 1 BEB14103 1 Lecture 1 ZMZ JANUARY 2012

Zaridah lecture2

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 1: Zaridah lecture2

PROGRAMMING 1BEB14103

1Lecture 1

ZMZ JANUARY 2012

Page 2: Zaridah lecture2

DATA TYPES

2

Topic Outcomes

• The students are able to identify four

standard types of data in C

• The students are able to declare the

variables according to the data types

• The students are able to create C

programs with various of data types

Page 3: Zaridah lecture2

INTRODUCTION

3

There are four basic data types :

• Int

• Float

• Char

• Void

Data types

int float char void

Page 4: Zaridah lecture2

INTEGER

4

• Integer is number without the fraction part

• Support three different sizes (1 byte, 2

bytes and 4 bytes)

• An integer constant must have at least

one digit

Page 5: Zaridah lecture2

CONT..

5

• It must not have a decimal point

• It can be either positive or negative

• If no sign precedes an integer

constant it is assumed to be positive

• No commas or blanks are allowed

within an integer constant

• Examples :

426 +782 -8000 -7605

Page 6: Zaridah lecture2

CHAR

6

• The letters of the alphabet

• Most computers use the American StandardCode for Information Interchange (ASCII)

• The maximum length of a character constantcan be 1 character

• Examples :

• 'A'

• 'I'

• '5'

• '='

Page 7: Zaridah lecture2

FLOAT

7

• Number with a fractional part, such as 43.32

• Support three different sizes of floating point data types: float,

double and long double

• Double provides twice the precision of float

• Floats typically take up 4 bytes

• Doubles takes up 8 bytes

• Long double takes 10 bytes

Page 8: Zaridah lecture2

VOID

8

• The void type has no values therefore we cannot

declare it as variable as we did in case of integer

and float.

• The void data type is usually used with function

to specify its type.

• Like in in the first C program we declared

“main()” as void type because it does not return

any value.

Page 9: Zaridah lecture2

RANGE OF DATA TYPES IN C

9

Page 10: Zaridah lecture2

VARIABLES

10

• Variables are named memory

locations that have a type.

• Must be declared and defined

• Declaration is used to name an

object such as a variable

• Definitions are used to create the

object

• A variable cannot be type void

Page 11: Zaridah lecture2

VARIABLE DECLARATION

11

Page 12: Zaridah lecture2

CONT..

12

In order to use a variable in our program we must first

declare it

HOW? A declaration statement has the following format:

type variable name;

–type : what kind of data will be stored in that location

(integer?character? floating point?)

–variable name: what is the name of the variable?

–semi-colon : indicates this is a statement!

int num_students; // declaration

num_students= 22; // initialization

Page 13: Zaridah lecture2

EXAMPLE 2.1

13

#include <stdio.h>

main()

{

int num1, num2, sum;

printf(“Enter one decimal number:”);

scanf(“%d”, &num1);

printf(“Enter another decimal number:”);

scanf(“%d”, &num2);

sum = num1 + num2;

printf(“\nSum of %d and %d is %d ”, num1, num2, sum);

return 0;

}

Page 14: Zaridah lecture2

EXAMPLE 2.2

14

#include <stdio.h>

main()

{

float area_rectangle, width, length;

printf(“Enter the length:”);

scanf(“%f”, &length);

printf(“Enter the width:”);

scanf(“%f”, &width);

area_rectangle = length * width;

printf(“\nArea of the rectangular is %f ”, area_rectangle);

return 0;

}

Page 15: Zaridah lecture2

EXAMPLE 2.3

15

#include <stdio.h>

main()

{

double pi, height, radius, base, volume;

pi = 3.142;

printf(“Enter the height the cone:”);

scanf(“%lf”, &height);

printf(“Enter the radius of the cone:”);

scanf(“%lf”, &radius);

base = pi * radius * radius;

volume = (1.0/3.0) * base * height;

printf(“\nThe volume of a cone is %f ”, volume);

return 0;

}

Page 16: Zaridah lecture2

#define

16

/*You may also associate constant using #define preprocessor directive*/

#include <stdio.h>

#define pi 3.142

main()

{

double height, radius, base, volume;

printf(“Enter the height the cone:”);

scanf(“%lf”, &height);

printf(“Enter the radius of the cone:”);

scanf(“%lf”, &radius);

base = pi * radius * radius;

volume = (1.0/3.0) * base * height;

printf(“\nThe volume of a cone is %f ”, volume);

return 0;

}

Page 17: Zaridah lecture2

EXAMPLE 2.4

17

#include <stdio.h>

main()

{

char Letter;

Letter = 'x';

printf(“\nThe letter is %c ”, Letter);

return 0;

}

Page 18: Zaridah lecture2

STANDARD OUTPUT• printf Function

• prints information to the screen• requires two arguments

• control string• conversion specifier

Example• double angle = 45.5;• printf(“Angle = %.2f degrees \n”, angle);

Output:• Angle = 45.50 degrees

Page 19: Zaridah lecture2

STANDARD INPUT• scanf Function

• inputs values from the keyboard• required arguments

• control string• memory locations that correspond to

the specifiers in the control string

Example:

• double distance;• scanf("%lf", &distance);

Page 20: Zaridah lecture2

SUMMARY

20

• There are four standard data types can be use in

C program which are:

• Integer

• Float

• Char

• Void

• Everytime you want to use a variable, the

declaration must be made

• To associate the variables with the data types,

you can use standard input and output function

Page 21: Zaridah lecture2

REFERENCE

21

Hanly, J. R. & Koffman, E. B (2001). C

Program Design for Engineers. Addison

Wesley Longman.

Deitel, P & Deitel H (2008). C How to

Program. Pearson Education Inc.