codegurukool.com · Free coding tutorials & online compiler

Home › C++ Tutorial › C++ variables

C++ variables

Named storage.

Declare with a type: int age = 20;. Use const for fixed values and auto to let the compiler infer the type.

Commands in this lesson

const

Definition: Declares a variable that cannot be reassigned.

Syntax:

const name = value;

Example:

const pi = 3.14;
console.log(pi);

Example code

#include <iostream>
using namespace std;
int main() {
  int age = 20;
  double price = 9.5;
  const char grade = 'A';
  auto n = 7;
  cout << age << " " << price << " " << grade << " " << n << endl;
  return 0;
}

Run this code in the online editor

← C++ outputC++ user input →