C Symbols
C Symbols
C Symbols
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
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.
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.
Several of the basic types can be modified using one or more of these type modifiers:
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 <<
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:
of of of of of of of
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:
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:
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.