[email protected] data types session 2. [email protected] primitive data types int, float,...

11
[email protected] Data Types Session 2

Upload: kaleigh-vowels

Post on 12-Dec-2015

215 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Yantox_ska@yahoo.com Data Types Session 2. yantox_ska@yahoo.com  Primitive data types  int, float, double, char  Aggregate data types  Arrays come

[email protected]

Data Types

Session 2

Page 2: Yantox_ska@yahoo.com Data Types Session 2. yantox_ska@yahoo.com  Primitive data types  int, float, double, char  Aggregate data types  Arrays come

2

[email protected]

Primitive data types int, float, double, char

Aggregate data types Arrays come under this category Arrays can contain collection of int or float or char

or double dataUser defined data types

Structures and enum fall under this category.

Data types in C

Page 3: Yantox_ska@yahoo.com Data Types Session 2. yantox_ska@yahoo.com  Primitive data types  int, float, double, char  Aggregate data types  Arrays come

3

[email protected]

Simple Data Type int integer bil. bulat float floating point bil. pecahan char character hanya 1 digit char[pj] string lebih dari 1 digit

How to declare int jml_anak; float nilai_mhs,rata2; char sex,status; char nm_mhs[20],almt[35];

How to Declare

Page 4: Yantox_ska@yahoo.com Data Types Session 2. yantox_ska@yahoo.com  Primitive data types  int, float, double, char  Aggregate data types  Arrays come

4

[email protected]

Variables are data that will keep on changingDeclaration

<<Data type>> <<variable name>>;int a;

Definition<<varname>>=<<value>>;a=10;

Usage<<varname>>a=a+1; //increments the value of a by 1

Variables

Page 5: Yantox_ska@yahoo.com Data Types Session 2. yantox_ska@yahoo.com  Primitive data types  int, float, double, char  Aggregate data types  Arrays come

5

[email protected]

Should not be a reserved word like int etc..Should start with a letter or an underscore(_)Can contain letters, numbers or underscore. No other special characters are allowed

including spaceVariable names are case sensitive

A and a are different.

Variable names- Rules

Page 6: Yantox_ska@yahoo.com Data Types Session 2. yantox_ska@yahoo.com  Primitive data types  int, float, double, char  Aggregate data types  Arrays come

6

[email protected]

Variable Name for a memory object. Variable names must start with letters and contain letters, digits, and underscores. • a, b, i, j, counter, number_of_points, c1234, …

Type What the variable represents. Could be of integer, floating point number, character, string, and many others.• int, float, double, char, …

Declaration Tells compiler about variables and their type. • int i,j;• float sum;

Variables

Page 7: Yantox_ska@yahoo.com Data Types Session 2. yantox_ska@yahoo.com  Primitive data types  int, float, double, char  Aggregate data types  Arrays come

7

[email protected]

It’s NOT a string anymore.It is seemed as a series of characters, which is

a character array.The number of the character array is just the

same as the number of letters of the word or sentence plus one.

The one more element is “\0” which is printed as “\n” by gets and printed as “”(NULL) by other words.

What’s string in turbo C

Page 8: Yantox_ska@yahoo.com Data Types Session 2. yantox_ska@yahoo.com  Primitive data types  int, float, double, char  Aggregate data types  Arrays come

8

Numeric Types

int integer

float floating point

char charater (a very shot integer)

short or short int short integer

long or long int long integer

double long floating point

long double very long floating point

Page 9: Yantox_ska@yahoo.com Data Types Session 2. yantox_ska@yahoo.com  Primitive data types  int, float, double, char  Aggregate data types  Arrays come

9

[email protected]

Reading Inputs

1. #include <stdio.h>2. #include<conio.h>3. void main()4. { float bil1,bil2,hasil; //deklarasi variabel5. clrscr(); //membersihkan layar 6. printf("Please input the first number : "); scanf("%f",&bil1);7. printf("Please input the second number: "); scanf("%f",&bil2);8. hasil = bil1 * bil2; //proses9. printf(“bil1 * bil2 = %f\n“,hasil); //cetak hasil kali10. getch(); //berhenti sebentar sampai ditekan tombol11.}

printf(“Hasil Kali dari %f dengan %f adalah %f “,bil1,bil2,hasil);

Page 10: Yantox_ska@yahoo.com Data Types Session 2. yantox_ska@yahoo.com  Primitive data types  int, float, double, char  Aggregate data types  Arrays come

10

[email protected]

Assignment

1. /* Arithmetic operators */2. #include <stdio.h>3. #include<conio.h>4. void main()5. {6. int a,c;7. int b=4;8. a=3;9. c=a+b;10. printf(“Sum: %d + %d -> %d\n”,a,b,c);11. a++;b--;12. prinf(“Now a=%d and b=%d\n”,a,b);13.}

Page 11: Yantox_ska@yahoo.com Data Types Session 2. yantox_ska@yahoo.com  Primitive data types  int, float, double, char  Aggregate data types  Arrays come

11

[email protected]

Write a program to calculate the average of three floating point numbers.• Using scanf function

Task 2

Buat program untuk meng-input-kan tiga buah bilangan pecahan sembarang kemudian hitung dan cetak rata-rata dari tiga bilangan tersebut.