Variable, Special Chanracters and Operators in C++ Language

Variables are used to store value for various uses. The variable can contain different types of data. C++ provides different data types through which a variable can be created and used to store the data of that type in it.

Note: Assumption is made that user/reader will be using 32bit machine for learning C++

Variable Type:
  • int
  • unsigned int
  • long
  • unsigned long
  • float
  • double
  • char
  • string
  • bool
Quick Notes on variables:
  • When assigning a large data type to a smaller data type, there will data lose.
    • Converting a larger value type into a smaller value type is called demotion,
    • whereas converting values in the opposite direction is known as promotion.
    • Mixed-mode expressions are not a good idea. Avoid forcing C++ to do your
      conversions for you.
  • You can declare a variable simply unsigned. The int is implied.
  • You can declare a variable and initialize it in the same statement:
  • A STRING variable is nothing but a array of characters.  
  • CONSTANT is a number of character that doesn't change.You can prefix the constant word before each data type
  • true is different from "true" in C++, the later one is a string and the first one is a bool value. But TRUE has no meaning. Weird :).
  • A float variable supports about 6 digits of accuracy .
  • A double supports 13 digits of accuracy .

There are basically two types of operators available. They are unary operator and binary operator.

Precedence      Operator         Meaning
1                     + (unary)         Effectively does nothing
1                     - (unary)         Returns the negative of its argument
2                     ++ (unary)         Increment
2                     -- (unary)         Decrement
3                     * (binary)         Multiplication
3                     / (binary)         Division
3                     % (binary)         Modulo
4                     + (binary)         Addition
4                     - (binary)         Subtraction
5                     =, *=,%=,+=,-=     (special) Assignment types

Quick Notes on Operators:
  • The modulus operator (%) works much like division, except it produces the remainder after division instead of the quotient.
  • The minus operator changes the sign of its argument. Positive numbers become negative and vice versa.
  • Increment and Decrement operators can be prefix or post-fix
    • Post fix (i++) assign the value and then increments the variable. 
    • Prefix (++i) increments the value and then assigns to a variable. 

Logical Operators:
Logical operators are very much used in C++, then are

Operator                 Meaning
==                         Equality;
!=                          Inequality;
>, <                       Greater than, less than;
>=, <=                  Greater than or equal to, less than or equal to;
&&                       AND;
||                            OR;
!                            NOT;

Bitwise logical operators:
The bitwise logical operators operate on their arguments at the bit level. Operations that work on numbers one bit at a time, hence the term bit-wise operators.

Operator       Function
~                     NOT
&                    AND
|                      OR
^                     XOR

Special Characters:
There are various special characters that are available for C plus plus language which are be used along with string or character variables.

Note: Assumption is made that user/reader will be using 32bit machine for learning C++

‘\n’ newline
‘\t’ tab
‘\0’ null
‘\\’ backslash

Quick Notes for Special Characters:
  • Paths in C++ are represented in C++ string as C:\\FolderA\\File.
  • The special value endl inserts a newline.
Share on Google Plus

About Varuna

I am Electronics and Communication Engineer. Having 5 years of IT experience in a PSU and MNCs companies. I have been working on projects which use C, C++, HTML5, JavaScript, JQuery, AngularJS and QT as programming language both in Windows and Linux. I want to learn new things and also share things which i learn through this blog.

Please feel free to share your knowledge and thoughts with the blog members.

Thank you.
    Blogger Comment
    Facebook Comment


Post a Comment