• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Zaridah lecture2

Zaridah lecture2






Total Views
Views on SlideShare
Embed Views



0 Embeds 0

No embeds



Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
Post Comment
Edit your comment

    Zaridah lecture2 Zaridah lecture2 Presentation Transcript

    • ZMZ JANUARY 2012 PROGRAMMING 1 BEB14103Lecture 1 1
    • DATA TYPES 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 2
    • INTRODUCTION Data types int float char voidThere are four basic data types : • Int • Float • Char • Void3
    • INTEGER• 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 digit4
    • CONT..• 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 -76055
    • CHAR• The letters of the alphabet• Most computers use the American Standard Code for Information Interchange (ASCII)• The maximum length of a character constant can be 1 character• Examples : • A • I • 5 • = 6
    • FLOAT• 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 7
    • VOID• 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. 8
    • VARIABLES• 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 10
    • CONT..In order to use a variable in our program we must firstdeclare itHOW? 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; // declarationnum_students= 22; // initialization 12
    • EXAMPLE 2.1#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;} 13
    • EXAMPLE 2.2#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;} 14
    • EXAMPLE 2.3#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;} 15
    • #define/*You may also associate constant using #define preprocessor directive*/#include <stdio.h>#define pi 3.142main(){ 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;} 16
    • EXAMPLE 2.4#include <stdio.h>main(){char Letter;Letter = x;printf(“nThe letter is %c ”, Letter);return 0;} 17
    • STANDARD OUTPUT• printf Function • prints information to the screen • requires two arguments • control string • conversion specifierExample• double angle = 45.5;• printf(“Angle = %.2f degrees n”, angle);Output:• Angle = 45.50 degrees
    • STANDARD INPUT• scanf Function • inputs values from the keyboard • required arguments • control string • memory locations that correspond to the specifiers in the control stringExample:• double distance;• scanf("%lf", &distance);
    • SUMMARY• 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 20
    • REFERENCEHanly, J. R. & Koffman, E. B (2001). CProgram Design for Engineers. AddisonWesley Longman.Deitel, P & Deitel H (2008). C How toProgram. Pearson Education Inc.21