C Language / Lesson 1 | Next Page |
Find in this page : Data Types Constants Scalars & aggregates
A first program in C
| ||||||||||||||||||||||||||||||||||||||
| ||||||||||||||||||||||||||||||||||||||
Data Types G e n e r a l C type declarations have the general form: [class][type] declarator [=initializer] [, declarator [=initializer] ...]All variables must be declared before they can be used. A variable name (identifier) can be modified with :
Binary Integer Types char Character Data Type The char type declares an object to be of "plain" binary type which is large
enough to store an ASCII character. In ASCII character set the size of a character
code is eight bits (1 Byte). A char may contain any 8-bit representation,
not just the ASCII characters. The data type char is signed by default and therefore
the range of values of a char is -128 to 127, inclusive. Usually however we
prefer to use chars as unsigned integers. This can be done using the modifier
unsigned (see below). In Microsoft C "plain" chars may be defined by default as unsigned using the /J compile-time switch. Conclusion #1: sizeof(char) is 1. Note : The reserved word sizeof(... ) is called 'a pre-processor operator' and is a syntax exception because it looks like a function i.e it has a name like any identifier and is followed by parentheses. sizeof(...) returns the allocated size in bytes by a defined variable or of a known data type as in the above example Examples char value, birth_date[6]; char action_code = 12; char selected_lang = 'G'; char *pointer_to_char;
Note : A character constant may be assigned to a character variable as well as shown in the above example ( 'G' ). It is very important to make clear that a character constant in C is totally different from a string constant.
int Integer Data Type The int type declares an object to be of "plain" integral type. The size (and representation) of an int object is implementation-defined. An int is the same or greater in size than a
short int, and the same or less than the size of a long int (see below). In most DOS/Windows C compilers, an int is short int and thus the size of an int object is two bytes and ints are signed. ( That is, int is equivalent to signed short). The range of values of a 2-byte signed int is -32768 to 32767 inclusive.
Examples
int rec_count = 0; int bit_pattern = 0X7FFF; int counters[6], *ip;
short Short Integer Data Type The keyword short may be applied to the int type. The short int type declares an object to be of type int and size 16-bits (2 bytes). short int must be the same or less in size than an int.The keyword "unsigned" may be used as a prefix to short int, to identify the short integer as being unsigned. If short is used without the keyword int, the int is assumed. Examples short int value = 6; short x,y,z; short int counters[6]; short *ip; Conclusion : sizeof(short) is 2. long Long Integer Data Type
The keyword long applied to the int type, declares an object to be of integer binary type. The size of a long int object is four bytes and its range of values is -2147483648 to 2147483647, inclusive. A long int is guaranteed to be the same or greater in size than an int.
However, usually the int type is the same as short.
Long constants An integral constant with the suffix L (or l) is interpreted as a long int constant, as is any integral constant that is too big to be represented in an int.
Examples
long int value = 6L; Type Modifiers: Keywords that Modify Data Types
Type modifiers are keywords that can be used to prefix a data type in a declaration. They modify the type by changing its storage size (e.g. short and long) or the way in which it is interpreted (e.g. signed and unsigned.) etc.
Examples unsigned long ul;
Floating pointTypes All type presented above are binary integer types that is they don't accept real numbers as values (containing a fractional part) Following are the types that do this job. However it must be mentioned that there is nothing really special about floating point types in C : They are treated as in most languages (Pascal, Basic etc) and they are mainly used for numeric figures which must support the relevant characteristics in order to be involved in math calulations. That is why an emphasis must be given in C to the binary integer type where C does a lot more than any other language and in more optimized ways, starting from text processing and parsing to bit-wise manipulations. float Single precision floating point Data Type This type declares 4-byte floating point variables. It must be mentioned that unlike integer data types, ranges is not the important issue here since due to the presence of the exponent part of floating point numbers (see note below) the range is usally wider that required. What matters here is how may significant digits can a float variable hold. The answer is that single precision numbers can have approximately 6 significant digits. For example number 12345678 has 8 significant digits and doesn't therefore fit in a single precision variable, whereas number 123000000 although bigger than the former has only 3 significant digits and does fit in a single precision variable. Examples of float variable definitions double Double precision floating point Data Type
This type is just another floating point type with all the features of the one previouly described. However it is bigger 8-byte and thus it can declare variable which will contain values with more significant digits (approximately 15 ).
G e n e r a l The word 'scalar' was borrowed from Physics where a scalar is a measure with
no dimension or direction i.e. it doesn't have a direction - it is not a vector.
The same definition can be used in C language with the difference that the words
dimension and direction are used with a different
meaning. area = Array (12.3 , 3.26) In C, arrays are called aggregates. An aggregate is therefore a variable with multiple values. It can also have multiple dimensions. A variable containing only one value is not an aggregate but it is a scalar. Previously in this page definitions like these short int myvalue = 6; short int counters[6]; defined scalars and aggregates. Variable myvalue is a scalar and variable counters is an aggregate. How do I know? the definition of counters has a pair of square brackets following the name of the variable. What about multidimension aggregates ? The above variable counters is a one dimension aggregate: It contains only one row which can contain 6 values. Let's see the syntax for multi-dimension definitions in C
The above are definitions of a 2D character aggregate and a 3D integer aggreagate. In the example of the grade variable , this contains 12 rows of 26 elements each. The 0 base headache In C the first element of an aggregate for example x[12] is x[0].
Obviously the last element is x[11]. One must be very careful because these
subfixes confuse the newcomer. And the situation becomes more desperate when
we learn that a string is stored in a character aggregate since
there is no data type in C called string or so. char message[6]= "Hello" ; is stored in memory like this
This means that
Note : \0 is one character and is written like this to distiguish from the printable 0 which as you can see in the ASCII table corresponds to the value 48 (30 hex) and not to 0 . |