Variables and constants, identifiers in C language
- 24-07-2022
- Toanngo92
- 0 Comments
With the knowledge in pseudocode and algorithm diagram , we see that in order for the computer to solve a problem, the computer will need a resource to store the value after it is calculated or the value entered by the user, just like humans remember a calculation formula, before calculating an addition of 2 numbers, we need to memorize the 2 numbers used to add, and after we finish the addition, we need to memorize As a result, computers are similar, and people use the concept of variables and constants to represent resources stored inside computer memory.
Mục lục
The concept of variables (variable) in C . programming language
In programming, variables are used to store data, it can store a character, string of characters, a number, an array (list), object …
The variable nature does not store the content of the data, it stores the address of the cell in which the data is being stored and the variable name describes how we name that memory cell. For that reason, when you access the internal variables, the computer will rely on the address to get the correct data. Of course, later, after proficient programming, we can briefly understand that a variable stores a certain data value. To explain this, I will talk a little bit about data size in programming.
In programming, the smallest unit for measuring computer memory is bits, 1 byte = 8 bits, 1 kilobyte = 1024 bytes , 1 megabyte = 1024 kilobytes, 1 gigabyte = 1024 megabytes …. ,
With the larger the computer memory, the more data the computer can remember, in the concept of memory, we are interested in 4 concepts that should be grasped to be able to program C effectively:
Memory address (memory address)
Briefly explain this is the concept used to locate each memory area inside the computer and is represented by arithmetic, and the location of the device is always unique (we can think of a single member). street has many different houses, people use the address to locate each house separately)
Memory size allocated
For each different data type (data type) , the computer will need a certain amount of memory to be able to represent them, for a simple example we store a character, which will take up less space. more memory space than storing a string (a collection of characters or a character array), coming back to the idea, let's imagine a level 1 house will take less construction resources than A 4-storey house, and a 4-storey house will consume less resources than a dormitory or apartment building.
The value is stored inside the memory area (value)
While programming, to tell the computer we need a resource (variable, constant) to store, the computer will allocate us a suitable memory size, and will automatically search and determine Specifying a suitable and free memory location to store the value, and this concept related to the data type, will be further mentioned below and discussed in detail in the next article. But to make it easier to imagine, let's imagine in the above house, for example, with a level 4 house we can stay 1 person, with a 4-storey house, staying 4 people is suitable, and with an apartment, we will live a lot. people more than that.
Variable name, constant (variable, constant) :
Name the location of the computer memory area that has just been allocated. For example, my house is at 6/203 Truong Chinh, instead of calling the address, people in the neighborhood will call it Mr. Toan's house for short. So from now on, just calling Mr. Toan's house will be able to replace calling 6/203 Truong Chinh.
And by default, when the variable stores a value, it means that the memory has allocated a memory size with a certain size a certain location in memory (in this context, RAM memory), the reference refer to the picture below
Declare variables in C
Variable declaration syntax
variable_type variable_name; // kieudulieu ten_bien;
In there:
variable_type is the data type
variable_name is the name of the variable
The data type will be placed right before the variable name
Note when declaring variables: variables are only allowed to be declared once in the program, cannot be re-declared, in the next process using variables, we say that we are using the declared variable to perform exam.
See the example of variable declaration, and print variable data to the screen:
#include<stdio.h> #include<stdio.h> int main() { // khai bao bien so nguyen int songuyen; // gan gia tri cho so nguyen bang 5 songuyen = 5; // khai bao va gan luon gia tri cho so nguyen int songuyencach2 = 6; // khai bao bien kytu la ki tu va gan bang ky tu 'A'; char kytu = 'A'; // khai bao bien so thuc va gan gia tri bang 5.1 float sothuc = 5.1; printf("%d,%d,%c,%f",songuyen,songuyencach2,kytu,sothuc); }
Rules for declaring variables in C
In C language or many other programming languages, variable naming rules are always mentioned to remind programmers to understand the conventions when declaring variables. As follows
- Variable names can only have letters (both uppercase and lowercase), numbers, and underscores, and are contiguous.
- The first letter of a variable must be a letter or an underscore. (some compilers do not report an error, but always remember the norm is not to use numbers as the first letter when declaring variables)
- There is no rule about the length of variable names. However, you may run into problems in some compilers if the variable name is longer than 31 characters, try to declare the variable name short, semantically understandable is ok
- C language is case sensitive, so when declaring variables and using them, you need to write them correctly, avoid misspellings in variable declaration, just 1 wrong character, compiler will not understand and error message. Normally, the variable declaration style should follow the convention: firstVariable or my_Variable or _myVariable (first word in lowercase, letters of next word in variable should be uppercase).
Examples Some ways to declare variable names true and false
int !tenbien; // sai, tên biến không được phép chứa ký tự đặc biệt int 1tenbien; // sai, chữ cái đầu tien tên biến không được sử dụng số int ten bien; // sai, có khoảng cách giữa tên biến int -tenbien; // sai, không sử dụng dấu - cho tên biến int _tenbien; // đúng int tenbien; // đúng int tenbien1; // đúng
Some tips when naming variables:
Should have meaningful variable names, not too shortened so that the code is clean and easy to read and maintain. For example, setting firstName is cleaner than setting it to fn.
It is not recommended to use letters when naming variables, except in some situations where it is easy to understand and the structure is concise (in the case of using a counter variable in a loop).
After naming variables, we continue to use them by calling the variable names directly, for example:
int a; // khai báo biến a = 5; // sử dụng biến trong phép gán
In addition, we need to follow the rules of assigning data to variables, declaring what data type the variable is, we will have to assign corresponding value to the variable of that data type, for example, if the variable is a number, no can assign a single-character value to the variable, or, if the variable is an integer, cannot assign a real number to the variable. For example;
int number = 10; // khai báo biến có kiểu dữ liệu số nguyên number = 10.5; // lỗi - gán một số thực vào biến số nguyên double number; // lỗi - cố tình khai báo lại và đổi kiểu dữ liệu của biến
The concept of constants in C
Constant is still a variable, but the constant does not change the value. The value of the constant will usually be used to assign to the variable. It is perfectly possible to treat the number 6 , the character 'A' , the string literal "abcde" as a constant.
And c also provides a syntax for us to create a self-defined constant and a common usage context for storing fixed data types. For example, PI with a value of 3.14 is already fixed, so we will use the syntax of declaring a constant variable to represent it:
Syntax for declaring constants:
const double PI = 3.14;
Note, a constant variable once declared, cannot change its value like a regular variable.
const double PI = 3.14; PI = 2.9; //lỗi
Advice on naming constant variables : use the all-caps convention when naming constants, you will find in most other programming languages, that's the convention, we just follow to make sure Keep the code clean and easy to read.
Identifier in C
This is a general concept when naming variables, constants, functions, and these are user-defined, collectively known as identifiers. These identifiers can consist of one or more characters, the convention that the first character of the identifier is always a letter or a "_", with subsequent characters, which can be alphanumeric, as long as not match with special characters.