0% found this document useful (0 votes)
42 views

PDF - CodeQuotient

The document is a tutorial on variables and identifiers in C/C++. It discusses variables as named locations in memory that hold values, and constants as fixed values that cannot be altered. It also discusses data types for variables, declaring variables, and examples of character, integer, and floating-point constants. The tutorial further explains that identifiers are the names given to variables, functions, and other user-defined items, and it provides rules for valid identifier names in C/C++.

Uploaded by

Deepesh Rohilla
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views

PDF - CodeQuotient

The document is a tutorial on variables and identifiers in C/C++. It discusses variables as named locations in memory that hold values, and constants as fixed values that cannot be altered. It also discusses data types for variables, declaring variables, and examples of character, integer, and floating-point constants. The tutorial further explains that identifiers are the names given to variables, functions, and other user-defined items, and it provides rules for valid identifier names in C/C++.

Uploaded by

Deepesh Rohilla
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

8/27/23, 6:25 PM PDF | CodeQuotient

Tutorial Link https://codequotient.com/tutorials/Variables and


Identifiers/5a1d944752795c1b16c0abe5

TUTORIAL

Variables and Identifiers

Chapter
1. Variables and Identifiers

Topics

1.2 Variable
1.3 Constants

1.7 Identifiers

In C/C++ programs, the most fundamental element is expression.


Expression can be formed by mixing data and operators. Data can be
represented by variables, constants etc. whereas operators are
defined in every programming languages. C/C++ languages supports
various data types. All these topics are explained below: -

Variable

A variable is a named location in memory. It is used to hold a value


during the execution of program. All variables needs to be declared
before they used. Every variable has a specific data type associated
with it. Which shows the type of data it can hold. To declare a
variable, we can use as below:

data_type var_name;

https://codequotient.com/tutorialpdf/5a1d944752795c1b16c0abe5 1/5
8/27/23, 6:25 PM PDF | CodeQuotient

data_type is a valid data type and modifier and var_name is the


name of the variable declared.

Constants

Constants refer to fixed values that the program may not alter.
Constants can be of any of the basic data types. The way each
constant is represented depends upon its type. Constants are also
called literals.

Character constants are enclosed between single quotes. For


example, 'a' and '%' are both character constants. Integer constants
are specified as numbers without fractional components. For
example, 10 and –100 are integer constants. Floating-point constants
require the decimal point followed by the number's fractional
component. For example, 11.123 is a floating-point constant. C also
allows you to use scientific notation for floating-point numbers. For
example,

1 #include <stdio.h> C
2
3 int main()
4 {
5 char c = 'A'; // character constant
6 int i = 50; // integer constant
7 double df = 3.5; // double constant
8
9 printf("c = %c\n", c);
10 printf("i = %d\n", i);
11 printf("df = %lf\n", df);
12
13 return 0;
14 }
15

1 C++
2 #include<iostream>
3 #include<cstdio>
4 using namespace std;

https://codequotient.com/tutorialpdf/5a1d944752795c1b16c0abe5 2/5
8/27/23, 6:25 PM PDF | CodeQuotient

5
6 int main()
7 {
8 char c = 'A'; // character constant
9 int i = 50; // integer constant
10 double df = 3.5; // double constant
11
12 cout<<"c = "<<c<<endl;
13 cout<<"i = "<<i<<endl;
14 cout<<"df = "<<df;
15 return 0;
16 }
17

When a constant value starts with 0, it is considered as octal


number. So if we use as,

int a = 070;

and then want to print the value of a in decimal it is not 70, it is the
decimal equivalent of 70 base 8 which is 56. Also following is a
syntax error,

int a = 078;

as you are writing an octal constant, but in octal only digits from 0 to
7 can be used, hence 8 is not a valid digit in octal number system.

1 #include <stdio.h> C
2 int main()
3 {
4 int a = 070;
5 printf("a = %d", a);
6
7 return 0;
8 }

1 C++
2 #include<iostream>

https://codequotient.com/tutorialpdf/5a1d944752795c1b16c0abe5 3/5
8/27/23, 6:25 PM PDF | CodeQuotient

3 #include<cstdio>
4 using namespace std;
5
6 int main()
7 {
8 int a = 070;
9 cout<<"a = "<<a;
10
11 return 0;
12 }
13

Identifiers

In C/C++, the names of variables, functions, labels, and various other


user-defined items are called identifiers. The length of these
identifiers can vary from one to several characters. The first
character must be a letter or an underscore, and subsequent
characters must be either letters, digits, or underscores. Here are
some correct and incorrect identifier names:

Correct : employee_name, emp_phone, _empid, _number123

Incorrect : 1empno, emp-number, first&last, high..bal


codequotient.com

In an identifier, upper and lowercase are treated as distinct. Hence


count, Count and COUNT are three separate identifiers.

https://codequotient.com/tutorialpdf/5a1d944752795c1b16c0abe5 4/5
8/27/23, 6:25 PM PDF | CodeQuotient

Tutorial by codequotient.com | All rights reserved, CodeQuotient 2023

https://codequotient.com/tutorialpdf/5a1d944752795c1b16c0abe5 5/5

You might also like