Site icon Learn C++

What Does & (the ampersand) Mean In A C++ App?

What Does the ampersand Mean In A C++ App

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.

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,

[crayon-6763002781b77688286585/]

Here is a full example of this usage,

[crayon-6763002781b80983253174/]

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.

[crayon-6763002781b81092902345/]
[crayon-6763002781b82059070524/]

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.

[crayon-6763002781b83177729166/]

Here is a full example to this usage

[crayon-6763002781b84878960718/]

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,

An example to this Logical operator is given as below,

[crayon-6763002781b85856271292/]

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,

[crayon-6763002781b88495086442/]

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.

Exit mobile version