Learn more: Variables, constants and literals in programming C

0 views
Skip to first unread message

Guaranteed Website Visitors

unread,
Dec 31, 2021, 2:22:33 PM12/31/21
to Guaranteed Website Visitors


In this tutorial, you will learn about variables and their naming rules, different literals, and how to build constants in C programming .

Variables

In programming, a variable is a holder (storage location) for storing data.

A unique name (identifier) ​​must be assigned to each variable to indicate the storage location. The variable name is a symbolic representation of a place in memory.

int playerScore = 95;

Here the name of the player Score back is a variable of type int and is assigned to the value of the integer 95.

The value of a variable can be changed during the program, hence it is called a variable.

char ch = 'a';

// code

ch = 'l';

Rules for naming variables in C

1- Variable name can only contain uppercase and lowercase letters, number and underline ().

2- The first letter of the variable must be a letter or underline and can not start with a number.

3- There is no rule for the number of characters or the length of the variable name (ID). However, if the variable name has more than 31 characters, you may have problems with some compilers.

Tip: Always try to choose meaningful names for variables. For example: firstName is a better name than fn.

C is a type sensitive language. This means that the variable type must be defined and you can not change the type after the definition. Example:

int number = 5; // Integer variable

number = 5.5; // Error

double number; // Error

In this example, the variable number has an int type. This type only accepts integer values, and you can not store a floating value (5.5) in its variable. Similarly, you can not change the variable data type to double. In addition, to store decimal values ​​in C, you must define the type double or float.

Literals in C

Literals are data used to represent fixed values. You can use them directly in the code. For example: 1, 2.5, 'c' and…

1- Integer liters

A literal integer is an integer without a fraction or exponent. There are three types of integers in C programming:

  • Decimal (Grade 10)
  • Octal (base 8)
  • Hexadecimal (base 16)

Example:

  • Decimal: 0, -9, 22
  • Octal: 021, 077, 033
  • Hexadecimal: 0 x7f, 0x2a, 0x521

In C programming, octal numbers start with 0 and hexadecimal numbers with 0x.

2- Decimal literals

Decimal literal is a numerical constant that has a decimal or exponential form.

-2.0

0.0000234

-0.22 E-5

Note: E-5 = 10 -5

3- Literal character

A character letter is created by placing a character inside the quotation mark ('' single quotation mark ''). Such as: 'm', 'F', '2', '}' and…

4- Escape characters

There are characters that either can not be typed or have a special meaning in C programming. For example: new line (enter), tab, question mark and…. Escape characters are used to create these types of characters. Escape characters are control characters that are written in combination, starting with a back slash (\) followed by a letter or number to indicate a particular character.


Characters
Escape characters
Back or Backspace

\ b


New page (section)

\ f


New line

\ n


Back to the top of the line

\ r


Horizontal fever

\ t


Vertical fever

\ v


Back slash

\\


Single quotation or single quote

\ '


Double quotation or double quotes

\ ”


Question mark

\?


Null or empty character

\ ۰

For example n \ is used to go to the next line. Starting with a back slash (\) indicates a transition from the common use of subsequent characters by the compiler.

5- String literals

Is a sequence of characters enclosed in double quotes. Example,

"Good" // String constant

“” // Empty string constant

”” // String constant containing 6 characters spaced

"X" // String constant with one character

"Earth is round \ n" // Print the string and then go to the next line

Fixed

If you want to define a variable whose value can not be changed during the program, you can use the cost keyword. This organic keyword traffic estimator a constant value.

const double PI = 3.14;

Here PI is an identifier for a constant whose value cannot be changed.

const double PI = 3.14;

PI = 2.9; // Error

Additionally, you can define a constant using the #define preprocessor command.

#define PI 3.14

The difference between our store tutorials and free tutorials : First, the packs teach the latest version of the software with much more functionality. Secondly, the packs have been prepared by experts, in a completely project-oriented manner, by solving the challenges that you face in the course of practical and professional work, and by learning them, you will be fully prepared for the labor market! Free tutorials or paid tutorials Many sites have only been dubbed or equivalent by non-experts!

Reply all
Reply to author
Forward
0 new messages