Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

C Symbols

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

Pervaiz Ahmed Noonari Information Technology QAU Islamabad C++ Symbols 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15.

16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. SYMBOL { } // << >> ; \ % \n > < >= <= != == a++ a-| || ^ && ~ :: ?: \0 DESCRIPTION Left brace Right brace Single-line comment Double quotations Stream Insertion Operator Stream Extraction Operator Semicolon (or statement terminator) Escape character (or Back slash) Modulus Operator New line character Greater than Less than Greater than or equal to Less than or equal to Not equal to Equal to Increment Decrement Bitwise OR Logical OR Exclusive OR Logical AND Not Scope Conditional Null character C++ Variables Types 1. 2. 3. 4. VARIABLE int float long double CAPACITY

(int can be used for decimal number, as float variable)

A scope is a region of the program and broadly speaking there are three places where variables can be declared: 1. 2. 3. Inside a function or a block which is called local variables, In the definition of function parameters which is called formal parameters. Outside of all functions which is called global variables.

We will learn what is a function and it's parameter in subsequent chapters. Here let us explain what are local and global variables.

Local Variables:
Variables that are declared inside a function or block are local variables. They can be used only by statements that are inside that function or block of code. Local variables are not known to functions outside their own. Following is the example using local variables:

Global Variables:
Global variables are defined outside of all the functions, usually on top of the program. The global variables will hold their value throughout the lifetime of your program. A global variable can be accessed by any function. That is, a global variable is available for use throughout your entire program after its declaration. Following is the example using global and local variables: A program can have same name for local and global variables but value of local variable inside a function will take preference.

Initializing Local and Global Variables:

When a local variable is defined, it is not initalised by the system, you must initalise it yourself. Global variables are initalised automatically by the system when you define them as follows: Data Type int char float double pointer 0 '\0' 0 0 NULL Initialser

It is a good programming practice to initialize variables properly otherwise, sometime program would produce unexpected result.

Primitive Built-in Types:


C++ offer the programmer a rich assortment of built-in as well as user defined data types. Following table list down seven basic C++ data types: Type Boolean Character Integer Floating point Double floating point Valueless Wide character Bool Char Int Float Double Void wchar_t Keyword

Several of the basic types can be modified using one or more of these type modifiers:

signed unsigned short long

The following table shows the variable type, how much memory it takes to store the value memory, and what is maximum and minimum vaue which can be stored in such type of variables. Type char unsigned char signed char int unsigned int signed int short int unsigned short int signed short int long int signed long int unsigned long int float double long double wchar_t Typical Bit Width 1byte 1byte 1byte 4bytes 4bytes 4bytes 2bytes Range Range 4bytes 4bytes 4bytes 4bytes 8bytes 8bytes 2 or 4 bytes Typical Range -127 to 127 or 0 to 255 0 to 255 -127 to 127 -2147483648 to 2147483647 0 to 4294967295 -2147483648 to 2147483647 -32768 to 32767 0 to 65,535 -32768 to 32767 -2,147,483,647 to 2,147,483,647 same as long int 0 to 4,294,967,295 +/- 3.4e +/- 38 (~7 digits) +/- 1.7e +/- 308 (~15 digits) +/- 1.7e +/- 308 (~15 digits) 1 wide character

The sizes of variables might be different from those shown in the above table, depending on the compiler and the computer you are using. Following is the example which will produce correct size of various data type on your cmputer.

#include <iostream> using namespace std; int main() { cout << cout << cout << cout << cout << cout << cout <<

"Size "Size "Size "Size "Size "Size "Size

of of of of of of of

char : " << sizeof(char) << endl; int : " << sizeof(int) << endl; short int : " << sizeof(short int) << endl; long int : " << sizeof(long int) << endl; float : " << sizeof(float) << endl; double : " << sizeof(double) << endl; wchar_t : " << sizeof(wchar_t) << endl;

return 0; }
This example uses endl which inserts a new-line character after every line and << operator is being used to pass multiple values out to the screen. We are also using sizeof() function to get size of various data types. When the above code is compiled and executed, it produces following result which can vary from machine to machine:

Size Size Size Size Size Size Size

of of of of of of of

char : 1 int : 4 short int : 2 long int : 4 float : 4 double : 8 wchar_t : 4

typedef Declarations:
You can create a new name for an existing type using typedef. Following is the simple syntax to define a new type using typedef:

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;

Enumerated Types:
An enumerated type declares an optional type name and a set of zero or more identifiers that can be used as values of the type. Each enumerator is a constant whose type is the enumeration. To create an enumeration requires the use of the keyword enum. The general form of an enumeration type is:

enum enum-name { list of names } var-list;


Here, The enum-name is the enumeration's type name. The list of names is comma separated. For example, the following code defines an enumeration of colors called colors and the variable c of type color. Finally, c is assigned the value "blue".

enum color { red, green, blue } c; c = blue;

By default, the value of the first name is 0, the second name has the value 1, the third has the value 2, and so on. But you can give a name a specific value by adding an initializer. For example, in the following enumeration, green will have the value 5.

enum color { red, green=5, blue };


Here blue will have a value of 6 because each name will be one greater than the one that precedes it.

You might also like