Ch Chapter 4 Basic Data Types and Variables 4.1 Basic Data Types In C TABLE 4.1 Introduction to Basic Data Types in C Type SizeDescription char 1 byte Characters or small integer variables int 2 or 4 bytes Integer values float 4 bytes Floating point numbers double 8 bytes Floating point numbers
Ch CHARACTERS The Standard for Text: ASCII(American Standard Code for Information Interchange) was first defined by the American National Standards Institute in In this code, each letter of the alphabet, punctuation mark, and decimal number is assigned a unique 7-bit code number. With 7 bits, 128 unique symbols can be coded. See Table 4.2.
Ch TABLE 4.2 ASCII Character Format
Ch Defining Character Variables in C Program 4.1: C Program Containing Variables int main ( void ) { /* Beginning of the (main) block.*/ Char cAChar;/* We put variable definitions here.*/ cAChar = 65 ;/* Executable statement : assign the */ /* code for ‘A’ to “cAChar”*/ }/* End of (main) block */
Ch Character Constants cAChar = ‘A’ ;/*assign ‘A’ to character */ /* variable “cAChar” */
Ch Escape Characters
Ch Remark 4.2
Ch INTEGERS Consider for example the declarations int iPennies; int iCounter; Initial Values can also be supplied in definitions, as with int iCounter = 0; int iNickels = 5;
Ch Short and Long Integers short<=int<=long
Ch Unsigned Integers Ex: unsigned int uiPennies; unsigned uUpCounter;
Ch SINGLE AND DOUBLE PRECISION FLOATING POINT NUMBERS
Ch Floating Point Variables Floating point variables and constants: 3.4,-45.33,2.714, Exponential notation: 3.0e-25,4.5e+05, e+19,
Ch Figure 4.2
Ch Table 4.5
Ch Program 4.2 : Print size of some basic data types
Ch 接續 Program 4.2
Ch ENUMERATION DATA TYPES enum weekday { Mon, Tue, Wed, Thu, Fri, Sat, Sun }; or enum dayofweek { Mon=1, Tue, Wed, Thu, Fri, Sat, Sun }; Program 4.3
Ch
Ch VARIABLE ATTRIBUTES: TYPE, ADDRESS, NAME, AND VALUE int iVal = 10; Type Name Value
Ch Program 4.4
Ch
Ch VARIABLE NAMING CONVENTIONS A variable name cannot start with a digit, and it cannot have the same as a reserved word.
Ch An appropriate name name for our count variable: int iCount ; unsigned int uiCount ; unsigned uCount;
Ch