English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
In this tutorial, you will learn about variables and the rules for naming variables. You will also learn about different literals in C programming and how to create constants.
In programming, variables are containers (storage areas) used to save data.
To specify the storage area, each variable should be assigned a unique name (Identifier). Variable names are just symbolic representations of storage locations. For example:
int playerScore = 95;
Here, playerScore is an int type variable. Here, an integer value is assigned to this variable95.
The value of a variable can be changed, so the name can also be changed.
char ch = 'a'; // some code ch = 'l';
Variable names can only contain letters (both uppercase and lowercase), numbers, and underscores.
The first letter of a variable should be a letter or an underscore.
There is no specific length specified for variable names (identifiers). However, if the variable name exceeds31If there are more than 78 characters, you may encounter problems in some compilers.
Note:You should always try to assign meaningful names to variables. For example: firstName is a better variable name than fn.
C language is a strongly typed language. This means that once a variable type is declared, it cannot be changed. For example:
int number = 5; //integer variable number = 5.5; // error double number; // error
Here, the type of the number variable is int. You cannot assign a floating-point (decimal) value5.5to this variable. Moreover, you cannot redefine the data type of a variable as double (double precision floating point). By the way, to store a decimal value in C, you need to declare its type as double or float.
Visit this page to learn more aboutDifferent types of data that variables can storeMore information.
Literals are data used to represent fixed values. They can be used directly in the code. For example:1,2.5, 'c' etc.
here1,2.5and 'c' are literals. Why? You cannot assign different values to these items.
Integers are numeric literals that have no fractional or exponential parts (associated with numbers). There are three types of integer literals in C programming language:
Decimal (with10as the base)
Octal (with8as the base)
Hexadecimal (with16as the base)
For example:
Decimal: 0, -9, 22 etc Octal: 021, 077, 033 etc Hexadecimal: 0x7f, 0x2a, 0x521 etc
In C programming, octal numbers start with 0, and hexadecimal numbers start with 0x.
Floating-point literals are numeric literals in fractional or exponential form. For example:
-2.0 0.0000234 -0.22E-5
Note: E-5 = 10-5
Character literals are created by enclosing a single character in single quotes. For example: 'a', 'm', 'F', ''2such as '}
Sometimes, in C programming, it is necessary to use characters that cannot be typed or have special meanings. For example: newline (input), tab, question mark, etc.
To use these characters, escape sequences are used.
Escape sequence | Character |
---|---|
\b | Backspace |
\f | Form feed |
\n | Newline |
\r | Carriage return |
\t | Horizontal tab |
\v | Vertical tab |
\\\ | Backslash |
\' | Single quote |
\" | Double quote |
\? | Question mark |
\0 | Null character |
For example: \n is used for a newline. The backslash \ causes the compiler to fail to process the character normally.
String literals are a series of characters enclosed in double quotes. For example:
"good" //String constant "" //A null string constant " " //A string constant with six spaces "x" //A string constant with a single character. "Earth is round\n" //Print a string with a newline character
If you want to define a variable that cannot be changed, you can use the const keyword. This will create a constant. For example,
const double PI = 3.14;
Note that we have added the keyword const.
Here, PI is a symbolic constant; its value cannot be changed.
const double PI = 3.14; PI = 2.9; //Error
You can also define constants using the #define preprocessor directive. We willC MacrosLearn it in the (macro) tutorial... .