Home > Backend Development > C++ > Explain the rules of variable declaration and variables in C language

Explain the rules of variable declaration and variables in C language

王林
Release: 2023-08-28 19:37:03
forward
1192 people have browsed it

Explain the rules of variable declaration and variables in C language

First let us understand what a variable is.

Variable

  • #It is the name of the memory location used to store data values.

  • Variables can take on different values ​​at different points in time during execution.

  • Programmers can choose meaningful variable names that reflect their function or nature in the program.

For example, sum (sum), avg (average), total (total), etc.

Variable naming rules

The rules for variable naming are as follows:

  • The variable name must start with a letter.

  • In the ANSI standard, the maximum length of a variable is 31 characters. However, many compilers only consider the first eight characters.

  • Uppercase and lowercase letters are different. For example: total, TOTAL, and Total are 3 different variables.

  • Variable names cannot be keywords.

  • No spaces allowed.

Variable declaration

The following is the syntax and example of variable declaration:

Syntax

The syntax of variable declaration is as follows:

Datatype v1,v2,… vn;
Copy after login

Where, v1, v2,...vn are names of variables.

For example,

int sum;
float a,b;
Copy after login

Variables can be declared in two ways −

  • local declaration − 'Local declaration' is to declare the variable inside the main code block, and its value is only Valid within this code block.

  • Global declaration − ‘Global declaration’ is to declare a variable outside the main code block, and its value is valid throughout the entire program.

Example

The following is a sample program for local and global variable declaration in C language −

int a, b; /* global declaration*/
main ( ){
   int c; /* local declaration*/
   - - -
}
Copy after login

Example

The following is a C program for finding the selling price (SP) and cost price (CP) of goods −

Online demonstration

#include<stdio.h>
int main(){
   float CostPrice, SellingPrice, Amount; //variable declaration
   //costprice & sellingprice are variables and
   //float is a datatype
   printf("</p><p> product cost price: ");
   scanf("%f", &CostPrice);
   printf("</p><p> product selling price : ");
   scanf("%f", &SellingPrice);
   if (SellingPrice > CostPrice){
      Amount = SellingPrice - CostPrice;
      printf("</p><p> Profit Amount = %.4f", Amount);
   }
   else if(CostPrice > SellingPrice){
      Amount = CostPrice - SellingPrice;
      printf("</p><p> Loss Amount = %.4f", Amount);
   }
   else
      printf("</p><p> No Profit No Loss!");
   return 0;
}
Copy after login

Output

The output is as follows−

product cost price : 240
product selling price : 280
Profit Amount = 40.0000
Copy after login

The above is the detailed content of Explain the rules of variable declaration and variables in C language. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template