Practice
Resources
Contests
Online IDE
New
Free Mock
Scaler
Practice
Improve your coding skills with our resources
Contests
Compete in popular contests with top coders
Scaler
Explore Offerings by SCALER

Begin your success journey!

Sign Up using
Full name *
Email *
Password *

By creating an account, I acknowledge that I have read and agree to InterviewBit’s Terms and Privacy Policy .

Welcome back!

Log In using
Email *
Password *

Variable and Types

C++ provides a rich set of built-in as well as user defined data types. Most of the built-in data types are described as follows:

  • Int ("%d"): 32 Bit integer
  • Long ("%ld"): 64 bit integer
  • Char ("%c"): Character type
  • Boolean (either true or false)
  • Float ("%f"): 32 bit real value
  • Double ("%lf"): 64 bit real value

The different types of variables define their bounds. A char can range only from -128 to 127, whereas a long can range from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807. This is because a char is represented using only 8 bits of information whereas long uses 64 bits of information.

To print float and double up to specific number of decimal places use:

cout<<std::fixed << std::setprecision(number of decimal places)

setprecision()  is available in the iomanip library.

User Defined data types are structures (struct) and classes (class). This will be covered later in the course.

Type Modifiers

The above types can be modified using the following type modifiers: signed and unsigned short and long.

Typedefs

Typedefs allow for creating new names (think of them as aliases) for existing types. Following is the simple syntax to define a new type using typedef:

typedef int item
item number = 0 // number is a integer variable
// item is equivalent to int, since we have use typedef and created new name for int as item

Variables

In programming, a variable is a container (storage area) to hold data.

To indicate the storage area, each variable should be given a unique name (identifier). For example,

int val = 10;
// Here, val is a variable of the int data type, and we have assigned an integer value 10 to it.
// Also, we can change the value of the variable
val = 15;

Try the following example in the editorial below.

You have to input 5 space-separated values: int, long, char, float and double respectively.

Print each value on a new line in the same order it is received as input. Note that the floating point value should be correct up to 3 decimal places and the double to 9 decimal places.

Example Input:

5 1234567891231 z 24.23 1214523.028352 

Example Output:

5
1234567891231
z
24.230
1214523.028352000

Start solving Variable and Types on Interview Code Editor
Hints
  • Complete Solution

Discussion


Loading...
Click here to start solving coding interview questions
Free Mock Assessment
Fill up the details for personalised experience.
All fields are mandatory
Current Employer *
Enter company name *
Graduation Year *
Select an option *
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
Phone Number *
OTP will be sent to this number for verification
+1 *
+1
Change Number
Phone Number *
OTP will be sent to this number for verification
+1 *
+1
Change Number
Graduation Year *
Graduation Year *
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
*Enter the expected year of graduation if you're student
Current Employer *
Company Name *
Please verify your phone number
Edit
Resend OTP
By clicking on Start Test, I agree to be contacted by Scaler in the future.
Already have an account? Log in
Free Mock Assessment
Instructions from Interviewbit
Start Test