C++Introduction to C++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,

Here is a full example of this usage,

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.

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.

Here is a full example to this usage

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,

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,

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.

close

Oh hi there 👋
It’s nice to meet you.

Sign up to receive awesome C++ content in your inbox, every day.

We don’t spam! Read our privacy policy for more info.

About author

33+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux and some other operating systems. Dr. Yilmaz Yoru was born in 1974, Eskisehir-Turkey. He graduated from the department of Mechanical Engineering of Eskisehir Osmangazi University in 1997. One year later he started to work in the same university as an assistant. He received his MSc and PhD degrees from the same department of the same university. He has married and he is a father of a son. Some of his interests are Programming, Thermodynamics, Fluid Mechanics and Artificial Intelligence. He also likes the graphical 2D & 3D design and high-end innovations.
Related posts
C++Introduction to C++Learn C++

What is A Void Function In The C Programming language?

C++Introduction to C++Learn C++

How To Run A Program Written In The C Language

C++Introduction to C++Learn C++

How To Compile And Run A C Program

C++Introduction to C++Learn C++

How To Exit A C Program In Power Shell