Wednesday, August 22, 2018

Datatypes in C Programming

Datatypes in C Programming

Datatypes in C Program DATA TYPES  C supports several different types of data, each of which may be represented differently within the computer’s memory. The basic data types are listed below. Typical memory requirements are also given. (The memory requirements for each data type will determine the permissible range of values for that data type. Note that the memory requirements for each data type may vary from one C compiler to another.)  int integer quantity 2 bytes or one word (varies from one compiler to another) char single character 1 byte float floating-point number (i.e., a number containing 1 word (4 bytes) a decimal point andor an exponent) double double-precision floating-point number (i.e., more 2 words (8 bytes) significant figures, and an exponent which may be larger in magnitude)  C compilers written for personal computers or small minicomputers (i.e., computers whose natural word size is less than 32 bits) generally represent a word as 4 bytes (32 bits). The basic data types can be augmented by the use of the data type qualijiers short, long, signed and unsigned. For example, integer quantities can be defined as short int, long int or unsigned int (these data types are usually written simply as short, long or unsigned, and are understood to be integers). The interpretation of a qualified integer data type will vary from one C compiler to another, though there are some commonsense relationships. Thus, a short int may require less memory than an ordinary int or it may require the same amount of memory as an ordinary int, but it will never exceed an ordinary int in word length. Similarly, a long int may require the same amount of memory as an ordinary int or it may require more memory, but it will never be less than an ordinary int . If short int and int both have the same memory requirements (e.g., 2 bytes), then long int will generally have double the requirements (e.g., 4 bytes). Or if int and long int both have the same memory requiremements (e.g., 4 bytes) then short int will generally have half the memory requirements (e.g., 2 bytes). Remember that the specifics will vary from one C compiler to another. An unsigned int has the same memory requirements as an ordinary int. However, in the case of an ordinary int (or a short int or a long int), the leftmost bit is reserved for the sign. With an unsigned int, all of the bits are used to represent the numerical value. Thus, an unsigned int can be approximately twice as large as an ordinary int (though, of course, negative values are not permitted). For example, if an ordinary int can vary from -32,768 to +32,767 (which is typical for a 2-byte int), then an unsigned int will be allowed to vary from 0 to 65,535. The unsigned qualifier can also be applied to other qualified ints, e.g., unsigned short int or unsigned long int. The char type is used to represent individual characters. Hence, the char type will generally require only one byte of memory. Each char type has an equivalent integer interpretation, however, so that a char is a really a special kind of short integer (see Sec. 2.4). With most compilers, a char data type will permit a range of values extending from 0 to 255. Some compilers represent the char data type as having a range of values extending from -128 to +127. There may also be unsigned char data (with typical values ranging from 0 to 255), or signed char data (with values ranging from -128 to +127). Some compilers permit the qualifier long to be applied to float or to double, e.g., long float, or long double. However, the meaning of these data types will vary from one C compiler to another. Thus, long float may be equivalent to double. Moreover, long double may be equivalent to double, or it may refer to a separate, “extra-large” double-precision data type requiring more than two words of memory. 


CONSTANTS 
There are four basic types of constants in C. They are integer constants,floating-pointconstants, character constants and string constants (there are also enumeration constants, which are discussed in Sec. 14.1). Moreover, there are several different kinds of integer and floating-point constants, as discussed below. Integer and floating-point constants represent numbers. They are often referred to collectively as numeric-type constants. The following rules apply to all numeric-type constants. 1. Commas and blank spaces cannot be included within the constant. 2. The constant can be preceded by a minus (-) sign if desired. (Actually the minus sign is an operator that changes the sign of a positive constant, though it can be thought of as a part of the constant itself.) 3. The value of a constant cannot exceed specified minimum and maximum bounds. For each type of constant, these bounds will vary from one C compiler to another. Let us consider each type of constant individually. 
Integer Constants 
An integer constant is an integer-valued number. Thus it consists of a sequence of digits. Integer constants can be written in three different number systems: decimal (base lO), octal (base 8) and hexadecimal (base 16). Beginning programmers rarely, however, use anything other than decimal integer constants. A decimal integer constant can consist of any combination of digits taken from the set 0 through 9. If the constant contains two or more digits, the first digit must be something other than 0. 
EXAMPLE 2.4 Several valid decimal integer constants are shown below. 
0 1 743 5280 32767 9999 

The following decimal integer constants are written incorrectly for the reasons stated. 
12,245 illegal character (, ). 36.0 illegal character (.). 10 20 30 illegal character (blank space). 123-45-6789 illegal character (-). 0900 the first digit cannot be a zero. 
An octal integer constant can consist of any combination of digits taken from the set 0 through 7. However the first digit must be 0,in order to identiQ the constant as an octal number. 
EXAMPLE 2.5 Several valid octal integer constants are shown below. 
0 01 0743 077777 

The following octal integer constants are written incorrectly for the reasons stated. 
743 Does not begin with 0. 05280 Illegal digit (8). 0777.777 Illegal character ( .). 


  • 0Blogger Comment
  • Facebook Comment

Post a Comment