In most cases, an ampersand symbol (&) is used as an ‘AND’ logic symbol. In a C or C++ app, we use the &
operand as an ‘AND’ logic in bitwise operations. We also use the double ampersand &&
operand as an ‘AND’ symbol in other logical operations. So, how we can use ampersands in C++? What does the & ampersand mean in a C++ app? Let’s learn this.
Here are the most common usages of & operand.
Table of Contents
How to declare a reference to a type with & in a C++ app?
We can use the &
symbol when we declare a variable, at the left-hand side of a variable declaration. That means that address of this reference type is same as the declared type. Any types can be used in any type of declarations, local variables, class members, method parameters etc. For example,
1 2 3 4 |
std::string mystring; std::string& mystring2 = mystring; |
Here is a full example of this usage,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
#include <iostream> #include <tchar.h> #include <string> int _tmain(int argc, _TCHAR* argv[]) { std::string mystring("LearnCPlusPLus"); std::string& mystring2 = mystring; std::cout << mystring << std::endl; std::cout << mystring2 << std::endl; getchar(); return 0; } |
If you wonder about the difference between C++ pointers and references here is your guide,
How can we get the address of a variable with & in a C++ app?
In a C++ app the & symbol is used as an address operator. When using this operand on the right-hand side of a variable, we point the address of this variable, not itself. This is generally useful with pointers. For example, you can set address of a string definition to a new pointer string.
1 2 3 4 5 6 |
std::string mystring; std::string *mystring2; mystring2 = &mystring; |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
#include <iostream> #include <tchar.h> #include <string> int _tmain(int argc, _TCHAR* argv[]) { std::string mystring("LearnCPlusPLus"); std::string *mystring2; mystring2 = &mystring; std::cout << mystring << std::endl; std::cout << static_cast<const std::string >( *mystring2) << std::endl; getchar(); return 0; } |
If you are looking more about pointers and memory address of a variable here is your guide,
How can we use the & ampersand as a bit-wise operator in a C++ app?
Use & as a Bitwise AND Operator
The & (bitwise AND) in C or C++ takes two numbers as operands and does AND on every bit of two numbers. The result of AND is 1 only if both bits are 1.
1 2 3 4 5 |
bool a=true, b=true, c; c = a & b; |
Here is a full example to this usage
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
#include <iostream> #include <tchar.h> #include <string> int _tmain(int argc, _TCHAR* argv[]) { bool a=true, b=true, c; c = a & b; if(c) std::cout << "a and b is true, thus c is true"; getchar(); return 0; } |
If you wonder about other bitwise operators you can check this post,
How do I use “&&” in a conditional expression in a C++ app?
We can use && together as an AND operator as we mentioned before,
In both the C & C++ programming language,
- && is used as AND,
- || is used as OR,
- ! symbol is used as NOT.
An example to this Logical operator is given as below,
1 2 3 |
if( a>40 && b<60 ) { } |
This means if(a>40)
is true
AND if(b<60)
is true
logically result is true
. Thus, this example if clause works like if(true) { }
. This is how && operand works here.
Here is the full example,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
#include <iostream> #include <tchar.h> #include <string> int _tmain(int argc, _TCHAR* argv[]) { int a = 45, b = 57; if( a>40 && b<60 ) std::cout << "a is greather than 40 AND b is lower than 60"; getchar(); return 0; } |
If you wonder about all operators in your C++ app please check here,
C++ Builder is the easiest and fastest C and C++ IDE for building simple or professional applications on the Windows, MacOS, iOS & Android operating systems. It is also easy for beginners to learn with its wide range of samples, tutorials, help files, and LSP support for code. RAD Studio’s C++ Builder version comes with the award-winning VCL framework for high-performance native Windows apps and the powerful FireMonkey (FMX) framework for cross-platform UIs.
There is a free C++ Builder Community Edition for students, beginners, and startups; it can be downloaded from here. For professional developers, there are Professional, Architect, or Enterprise versions of C++ Builder and there is a trial version you can download from here.
Design. Code. Compile. Deploy.
Start Free Trial
Free C++Builder Community Edition