CS110: Data Types in C: V. Kamakoti 20th January 2008
CS110: Data Types in C: V. Kamakoti 20th January 2008
Lecture 8
V. Kamakoti
20th January 2008
Datatypes
Types of Data you can use in C without
explicit definition by the user
Arithmetic
Integer domain
Real domain
Strings
Length one - character
Length greater than one - array of characters
Sequence of characters
Todays Lecture Contents
Built-in Types in C
You can define your own data types
Construct called typedef
Later in this course
User defined data type
Creative Problem
Built-in Data types
int
char
float
double
Sizes of each vary depending on
Architecture and compiler
Variations
int
signed, unsigned, long, short
float
single and double precision
Size of short int less than or equal to just
int which is less than or equal to long int
The builtin function sizeof() shall return the
size of the types.
Example Program
#include <stdio.h>
/* Program to print sizeof all datatypes */
main() {
printf("sizeof int is %d\n",sizeof(int));
printf("sizeof short int is %d\n",sizeof(short int));
printf("sizeof long int is %d\n",sizeof(long int));
printf("sizeof unsigned is %d\n",sizeof(unsigned));
printf("sizeof short unsigned is %d\n",sizeof(short unsigned));
printf("sizeof long unsigned is %d\n",sizeof(long unsigned));
printf("sizeof float is %d\n",sizeof(float));
printf("sizeof double is %d\n",sizeof(double));
}
Output
Apple I Mac system - Intel Core 2 Duo
Architecture
Compiler gcc-4.0.1 version
sizeof int is 4
sizeof short int is 2
sizeof long int is 4
sizeof unsigned is 4
sizeof short unsigned is 2
sizeof long unsigned is 4
sizeof float is 4
sizeof double is 8
What does this mean?
int i; // Allocated to memory 0x1000
It has 4 bytes
B3,B2,B1,B0
B3 is most significant byte 0x1003
B0 is least significant byte stored in 0x1000
B1 is stored in 0x1001
B2 is stored in 0x1002
This is called little-endian storage - Intel
machines use this
What does this mean?
int i; // Allocated to memory 0x1000
It has 4 bytes
B3,B2,B1,B0
B3 is most significant byte 0x1000
B0 is least significant byte stored in 0x1003
B1 is stored in 0x1002
B2 is stored in 0x1001
This is called big-endian storage - SUN
processors - Sparc use this
Constants in C
Remember the #define PI in the Area of the
circle problem?
That was a real constant
0, 1, 245, 623, 987 - are decimals
0, 01, 0245, 0623 - are octals
Begin with a 0
So 125 is not equal to 0125
0987 is illegal - why?
0x0, 0x1, 0xABCD - are hexadecimals
0X0, 0X1, 0XABCD - are also correct way of
representing them.
Wrong ways for integer constants
12,245
no comma to enhance readability
36.0
no dots //Not integer constant
10 20 30
no space in between
123-45-6789
no hyphen to enhance readability
0900
not decimal
Wrong ways for octal constants
743
Should start with 0
05280
Illegal digit 8
0777.77
No dots allowed for octal integers
Unsigned and Long Integer
Constants
50000U - decimal unsigned
123456789L - decimal long
123456789UL - decimal unsigned long
0123456L - octal long
0777777U - octal unsigned
0x5000ABCU - hexa unsigned
0XFFFFFUL - hexa unsigned long
Floating Point Constants
3X 105 3 X 105
300000. 30E4
3e5 30.E+4
3e+5 300e3
3E5 .
3.0e+
.3e6
0.3E6
Floating Point Constants
5.026 X 10-17
5.026E-17
.5026E-16
50.26E-18
.0005026E-13