Home arrow C programming arrow Data Types, Variables, and Constants

Language Translator

Hacking Zone

Hacking Tools
Attacking

Configure Windows

Windows Configuration

Novels

Mix Novels

Human Personality

Body Language
Data Types, Variables, and Constants PDF Print E-mail
Written by Hemanshu Patel   
Friday, 19 October 2007
Article Index
Data Types, Variables, and Constants
Page 2
Page 3
Page 4
Page 5
Page 6
Page 7
Page 8

Arrays

You may declare arrays of any data type, including classes. The general form of a singly dimensioned array is

type var-name[size];

where type specifies the data type of each element in the array and size specifies the number of elements in the array. For example, to declare an integer array x of 100 elements, you would write

int x[100];

This will create an array that is 100 elements long with the first element being 0 and the last being 99. For example, the following loop will load the numbers 0 through 99 into array x:

for(t=0; t<100; t++) x[t] = t;

Multidimensional arrays are declared by placing the additional dimensions inside additional brackets. For example, to declare a 10 × 20 integer array, you would write

int x[10][20];

Arrays can be initialized by using a bracketed list of initializers. For example,

int count[5] = { 1, 2, 3, 4, 5 };>

In C89 and C++, array dimensions must be specified by constant values. Thus, in C89 and C++, all array dimensions are fixed at compile time and cannot change over the lifetime of a program. However, in C99, the dimensions of a local array can be specified by any valid integer expression, including those whose values are known only at compile time. This is called a variable- length array. Thus, the dimensions of a variable-length array can differ each time its declaration statement is encountered.


Defining New Type Names Using typedef

You can create a new name for an existing type using typedef. Its general form is

typedef type newname;

For example, the following tells the compiler that feet is another name for int:

typedef int feet;

Now, the following declaration is perfectly legal and creates an integer variable called distance:

feet distance;

Constants

Constants, also called literals, refer to fixed values that cannot be altered by the program. Constants can be of any of the basic data types. The way each constant is represented depends upon its type. Character constants are enclosed between single quotes. For example 'a' and '+' are both character constants. Integer constants are specified as numbers without fractional components. For example, 10 and –100 are integer constants. Floating-point constants require the use of the decimal point followed by the number’s fractional component. For example, 11.123 is a floating-point constant. You may also use scientific notation for floating-point numbers.

There are two floating-point types: float and double. Also, there are several flavors of the basic types that are generated using the type modifiers. By default, the compiler fits a numeric constant into the smallest compatible data type that will hold it. The only exceptions to the smallest-type rule are floating-point constants, which are assumed to be of type double. For many programs, the compiler defaults are perfectly adequate. However, it is possible to specify precisely the type of constant you want.>

To specify the exact type of numeric constant, use a suffix. For floating-point types, if you follow the number with an F, the number is treated as a float. If you follow it with an L, the number becomes a long double. For integer types, the U suffix stands for unsigned and the L for long. Some examples are shown next:

Data Type

Constant Examples

int

1 123 21000 –234

long int

35000L –34L

unsigned int

10000U 987U

float

123.23F 4.34e–3F

double

123.23 12312333 –0.9876324

long double

1001.2L

C99 also allows you to specify a long long integer constant by specifying the suffix LL (or ll).

Hexadecimal and Octal Constants

It is sometimes easier to use a number system based on 8 or 16 instead of 10. The number system based on 8 is called octal and uses the digits 0 through 7. In octal, the number 10 is the same as 8 in decimal. The base 16 number system is called hexadecimal and uses the digits 0 through 9 plus the letters A through F, which stand for 10, 11, 12, 13, 14, and 15. For example, the hexadecimal number 10 is 16 in decimal. Because of the frequency with which these two number systems are used, C/C++ allows you to specify integer constants in hexadecimal or octal instead of decimal if you prefer. A hexadecimal constant must begin with a 0x (a zero followed by an x) or 0X, followed by the constant in hexadecimal form. An octal constant begins with a zero. Here are two examples:

int hex = 0x80; // 128 in decimal  int oct = 012;  // 10 in decimal

String Constants

C/C++ supports one other type of constant in addition to those of the predefined data types: a string. A string is a set of characters enclosed by double quotes. For example, "this is a test" is a string. You must not confuse strings with characters. A single-character constant is enclosed by single quotes, such as 'a'. However, "a" is a string containing only one letter. String constants are automatically null terminated by the compiler. C++ also supports a string class, which is described later in this book.>

Boolean Constants

C++ specifies two Boolean constants: true and false.

C99, which adds the _Bool type to C, does not specify any built-in Boolean constants. However, if your program includes the header <stdbool.h>, then the macros true and false are defined. Also, including <stdbool.h> causes the macro bool to be defined as another name for _Bool. Thus, it is possible to create code that is compatible with both C99 and C++. Remember, however, that C89 does not define a Boolean type.

>>Complex Constants

In C99, if you include the header <complex.h>, then the following complex and imaginary constants are defined.

_Complex_I

(const float _Complex) i

_Imaginary_I

(const float _Imaginary) i

I

_Imaginary_I (or _Complex_I if imaginary types are not supported)

Here, i represents the imaginary value, which is the square root of –1.

Backslash Character Constants

Enclosing character constants in single quotes works for most printing characters, but a few, such as the carriage return, are impossible to enter into your program’s source code from the keyboard. For this reason, CC++ recognizes several backslash character constants, also called escape sequences. These constants are listed here:

Code

Meaning

\b

Backspace

\f

Form feed

\n

Newline

\r

Carriage return

\t

Horizontal tab

\"

Double quote

\'

Single quote

\\

Backslash

\v

Vertical tab

\a

Alert

\N

Octal constant (where N is an octal constant)

\xN

Hexadecimal constant (where N is a hexadecimal constant)

\?

Question mark

The backslash constants can be used anywhere a character can. For example, the following statement outputs a newline and a tab and then prints the string “This is a test”.

cout << "\n\tThis is a test";




Digg!Reddit!Del.icio.us!Google!Live!Facebook!Slashdot!Netscape!Technorati!StumbleUpon!Spurl!Wists!Simpy!Newsvine!Blinklist!Furl!Fark!Blogmarks!Yahoo!Smarking!Netvouz!Shadows!RawSugar!Ma.gnolia!PlugIM!Squidoo!BlogMemes!FeedMeLinks!BlinkBits!Tailrank!linkaGoGo!Free social bookmarking plugins and extensions for Joomla! websites! title=
Comments
Add NewSearch
Only registered users can write comments!

Copyright (C) 2007 Alain Georgette / Copyright (C) 2006 Frantisek Hliva. All rights reserved.



 
< Prev   Next >
Your Ad Here

Donate us!!

Enter Amount:

RSS socialnet

Add to MyYahoo!
Subscribe in NewsGator Online
Add to Newsburst
Add to Google
Add to My AOL
Add to Pluck
Subscribe in FeedLounge
Add to Windows Live
Add to NetVibes
Subscribe in Rojo
Subscribe in Bloglines
Add to MyMSN
Add to Plusmo for your cellphone
Add to PageFlakes
Add to Technorati
Add to BlinkBits