170 likes | 185 Views
This lecture covers the fundamental data types in C, including character processing, automatic type conversion, and type casting. It also discusses the macros and functions in ctype.h for character handling.
E N D
Lecture 7 • Fundamental data types in C • Data type conversion: • Automatic • Casting • Character processing • getchar() • putchar() • Macros on ctype.h • Readings: Chapter 6, Section 1-12, Chapter 5
Fundamental data types • There are 5 basic data types in C: • char: characters • int: integers • float, double: floating values • void: empty value • char and int are collectively known as integral types • Variants of these types can be formed by adding type modifiers: long, short, signed, unsigned
short and long • Affect size of storage and hence range of values • Use short to conserve space • Use long to have larger range • Exact effect depends on compilers • Applicable to int: short int, int, long int • long is applicable to double too; thus the various floating point types are: float, double, long double
signed and unsigned • Applicable to char: char, signed char, unsigned char • Applicable to integers: • Integers are signed by default, thus signed int is the same as int • unsigned types can only store non-negative integers • Can be used with other modifiers. E.g., unsigned short int, unsigned long int
Fundamental data types (summary) • Long form char signed char unsigned char signed short intsigned int signed long int unsigned short int unsigned int unsigned long int float double long double • Short form char char unsigned char short int long unsigned short unsigned unsigned long float double long double
The sizeof operator • Used to find the number of bytes needed to store an object (which can be a variable or a data type) • Its result is typically returned as an unsigned integer • It is called a compile-time operator because its result depends on compiler • E.g. int len1, len2; float x; len1 = sizeof(int); len2 = sizeof(x);
Automatic Type Conversions • Automatic type conversions are performed when: • a value is assigned to a variable of a different type • a value is passed to a function parameter of a different type • a value is returned from a function of a different type • evaluating an expression with operands of different type • When evaluating an expression, the operands are promoted to the “highest” type in the expression.
Hierarchy of Built-in Data Types • long double • double • float • unsigned long int • long int • unsigned int • int • unsigned short int • short int • unsigned char • char
Casts • Explicit type conversions called casts is supported in C • If x is an int, then (double) x is an expression with value x and type double. Type and value of x itself remain unchanged. • Casts can be applied to expressions, e.g, int x=4, y=3; double z; Z = (double)x/y;
The data type char • A character is stored as an 8-bit integer using the ASCII encoding. • Example: char c = ’a’; Internally, c is stored as the following bit pattern 0 1 1 0 0 0 0 1 which is equivalent to a decimal 97 • Variables of any integral type can be used to store characters. int i=’a’; printf(“%d\n”,i); /* 97 */
The data type char (cont’d) • Any integral expression can be printed as a character or an integer E.g.: printf(“%c\n”,95+2); /* ‘a’ */ printf(“%d\n”,95+2); /* 97 */ • Depending on the compiler, the type char is equivalent to either signed char or unsigned char • signed char and unsigned char are used less often than char in typical C programs
The data type char(cont’d) • note that ASCII value of ‘7’ is 55 but not 7
Preceded by the escapecharacter backslash \ as it is used to “escape” the usual meaning of the character that follows it, e.g., /*print “ABC” */ printf(“\”ABC\””); Nonprinting & hard-to-print characters
The use of getchar() & putchar() • Input and output of character can be done by using getchar() and putchar() which are defined in <stdio.h> • getchar() reads a character from the keyboard. It returns the character read if successful, and EOF otherwise. • The function prototype is: int getchar(void); • putchar() writes a character to the screen. It returns the character printed if successful, and EOF otherwise. • The function prototype is: int putchar(int);
Example: lower- to upper-case conversion #include <stdio.h> /* for I/O handling */ #include <ctype.h> /* for character handling */ void main(void) { int c; while ((c = getchar()) != EOF) if (islower(c)) putchar(toupper(c)); else putchar(c); }
The header file ctype.h contains a set of macros and functions for character handling The macros usually takes an argument of type int and return an int value that is either nonzero (true) or zero (false) Macros and functions in <ctype.h>
Macros and functions in <ctype.h> (cont’d) • Note that the value of c stored in memory is not changed after the execution of the functions and macros defined in ctype.h