Home » Syntax References

Variable declaration syntax in C/C++ language

Learn: What is be the correct form a variable declaration in C/C++ programming language? Here you will find the syntax and examples of variable declarations in C/C++.

A variable is the name of memory blocks, whose value can be changed at anytime (runtime), we can declare a variable by using following syntax:

[storage_class] data_type variable_name [=value];

Here, [storage-class] and [=value] are optional.

Note: if storage classis "static", initialization value must be provided.

Declaration Example 1:

auto int age = 10;

age is an automatic integer variable (learn more about auto (automatic): storage classes in C language), it’s initial value is 10, which can be changed at anytime. (i.e. you may change the value of age at any time).

Declaration Example 2:

int age=10;

If we do not specify storage class, variable’s default storage class will be an automatic (auto). Thus, declaration example 1 and 2 are same.

Declaration Example 3:

int age;

This declaration is same as declaration example 1 and 2 without initial value, age will have either 0 or garbage value based on the different compilers.

Other examples:

float percentage; //a float variable 
char gender; //a character variable 
//an array of characters, maximum number of characters will be 100
char name [100];  
int marks [5]; //an array of integers to store marks of 5 subjects

Must read:

  1. variable declaration value in C language
  2. Basic data types in C language


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.