0% found this document useful (0 votes)
14 views

02 01 Data and Data Types

The document discusses data types in the C programming language. It begins by stating the objectives of explaining basic data concepts and using correct data types. It then outlines topics that will be covered, including primitive and composite data types. The document proceeds to explain various primitive data types like char, integers, floats, and their characteristics. It also discusses composite data types like arrays and structures. Other topics covered include Boolean values, strings, and choosing the best data type.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

02 01 Data and Data Types

The document discusses data types in the C programming language. It begins by stating the objectives of explaining basic data concepts and using correct data types. It then outlines topics that will be covered, including primitive and composite data types. The document proceeds to explain various primitive data types like char, integers, floats, and their characteristics. It also discusses composite data types like arrays and structures. Other topics covered include Boolean values, strings, and choosing the best data type.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

Data & Data Types

Procedural Programming

Mario Simaremare, S.Kom., M.Sc.


Program Studi Sarjana Sistem Informasi
Institut Teknologi Del
Objectives

• The objective of this session is the following:


• students are able to elaborate the basic concept of
data, data type, and their relationship.
• students are able to use the correct data type.

Procedural Programming 2
Please see this material first

Procedural Programming 3
Outlines

1. PEMVIS’ “Data & Data Types”.


2. Data types in C.
3. Primitive data types.
4. Composite data types.
5. Boolean?
6. String?
7. Choosing data type.

Procedural Programming 4
Data types in C

• Primitive data types.


• Built-in types.
• character, integer, floating-points, etc.

• Composite data types.


• User-created type.
• Build on top of either primitive or composite types.
• Array, structure, enumeration, etc.

• Some others, like void and pointer.


Procedural Programming 5
Primitives data types

• C supports a handful of primitive data types:


• char (8-bit), stores an ASCII value  character.
• int, typically reflecting the natural size of integers
on the host machine (can be either 16-bit or 32-bit).
• short, 16-bit version of int, also referred as short int.
• long, 32-bit version of int also referred as long int.
• Long long, 64-bit version of int.
• float (32-bit), single-precision floating point.
• double (64-bit) double-precision floating point.
• long double (80-bit) extended-precision floating point.

Procedural Programming 6
Primitives data types

• Signed or unsigned?
• Signed accepts both positive and negative values.
• Unsigned accepts only positives.

Procedural Programming 7
char

• char is a single byte data type.


• It is capable of holding one character in
the local character set, ASCII.
• See http://www.asciitable.com/
• By default it is unsigned.

char c1 = 'a';
char c2 = 98;
printf("c1: %c (%hhu); c2: %c (%hhu).\n", c1, c1, c2, c2);

Procedural Programming 8
Integer family

• int is either 16 or 32 or 64 bits data type.


• It is capable of holding a whole numerical value.
• For 16 bit, use short int, or simply short (default).
• For 32 bit, use long int, or simply long.
• For 64 bit, use long long.
• By default it is signed.

int i = -32;
short int si = 49;
unsigned long l = 98;
long long ll = 198203;
printf("i: %hi; si: %hi; l: %lu; ll: %lli.\n", i, si, l, ll);

Procedural Programming 9
Floating-point data types

• Available from 32 bits up to 80 bits data types.


• It is capable of holding a floating-point numerical value.
• For 32 bit, use float (6 digits precision).
• For 64 bit, use double (10 digits precision).
• For 80 bit, use long double (extended).

float f = -32.61;
double d = 98.76;
long double ld = 63126.115115115;
printf("f: %f; d: %lf; ld: %Lg.\n", f, d, ld);

Procedural Programming 10
Composite data types

• Multi-valued: array.
• It can hold multiple homogenous type of values.
• In the memory, the values are stored sequentially.
• Each value is accessible through the index (0-based).

• Record/structure.
• It is able to hold multiple values.
• It allows heterogeneous type of values.

Procedural Programming 11
Other data types

• Enumeration.
• Integer-based labels.

• Pointer.
• Represents memory location depends
on the CPU architecture 32-bit or 64-bit.

• void.
• Simply ‘nothing’.

Procedural Programming 12
Boolean?

• Boolean has two possible values: TRUE and FALSE.


• Unfortunately, C does not have Boolean data type.
TRUE
• Instead, C treats: FALSE
• Non-zero value as TRUE, and
• E.g. 9, 12, -3.2 are considered as TRUE,
• Zero value as FALSE.
• E.g. 0, 0.0 are considered as FALSE.

Procedural Programming 13
String?

• Unfortunately, C does not have String data type.

• C treats an array of chars like String with rules: "String"


• It ends with NULL or '\0'.

char name[10] = {'W', 'i', 'r', 'o', '\0'};


char secret[10] = {"212OKE"};
printf("name: %s; secret: %s.\n", name, secret);

Procedural Programming 14
Todos

• Choosing the best data type for a value?


Consider two things:
• The most appropriate characteristics.
• Space-efficient.
• E.g. What data type should be used for
• 11.999?
• 35,767?
• age?

Procedural Programming 15
References

• Kerninghan & Ritchie. The C Programming Language.


• Herbert Schildt. C: The Complete Reference.

Procedural Programming 16
Thank
you

Procedural Programming 17

You might also like