Download presentation
Presentation is loading. Please wait.
2
1 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
3
2 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
4
3 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
5
4 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
6
5 Fundamental data types (summary) Long form charsigned charunsigned char signed short intsigned intsigned long int unsigned short intunsigned intunsigned long int floatdoublelong double Short form charcharunsigned char short intlong unsigned shortunsignedunsigned long floatdoublelong double
7
6 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);
8
7 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.
9
8 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
10
9 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;
11
10 The data type char A character is stored as an 8-bit integer using the ASCII encoding. Example: charc = ’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 */
12
11 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
13
12 The data type char (cont’d) note that ASCII value of ‘7’ is 55 but not 7
14
13 Nonprinting & hard-to-print characters Preceded by the escape character backslash \ as it is used to “escape” the usual meaning of the character that follows it, e.g., /*print “ABC” */ printf (“\”ABC\””);
15
14 The use of getchar () & putchar () Input and output of character can be done by using getchar () and putchar () which are defined in 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);
16
15 Example: lower- to upper-case conversion #include /* for I/O handling */ #include /* for character handling */ void main(void) { int c; while ((c = getchar()) != EOF) if (islower(c)) putchar(toupper(c)); else putchar(c); }
17
16 Macros and functions in 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)
18
17 Macros and functions in (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
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.