Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
Have you ever known how a compiler or interpreter understands the type of variable? Do you know how we define the type of variables?
In this article, we will discuss data types and what their types are, and we will also understand how we can use these data types while doing code using the Carbon programming language.
What are Data Types?
When a program is running, the interpreter must understand that the data need to be used.
In Carbon Language, there are three categories of Data Types:
Primitive,
Composite, and
User-Defined
Moving Forward, let's understand them briefly.
Primitive Data Types
In Carbon language, there are four types of Primitive Data Types
Bool
Int
Float
String
Bool
Bool stands for Boolean. In conditional loops, we can use the Bool type to check for True and False. This data type specifies one bit of information.
var a: bool = True;
Int
Int stands for Integer. It is used to store numbers. Basically, this data type is preferred when we create variables to store numeric values. Int data type is defined in two further types.
Signed Int (Carbon.Int(N))
Unsigned Int (Carbon.UInt(N))
N is the width of the bit.
Signed Int
This bit type is defined using Carbon.Int(N) where N is a positive multiple of 8. The overflow in a signed integer is considered a programming error.
For an 8-bit Integer, we can use i8
For 16 bit Integer, i16
For 32 bit Integer, i32
For 64 bit Integer, i64
For 128 bit Integer, i128
For 256 bit Integer, i256
Unsigned Int
This bit type is defined using Carbon.uInt(N). These types usually wrap around overflow, thus only recommended to use when such semantics are required.
For an 8 bit unsigned Integer, we can use u8
For 16 bit unsigned Integer, u16
For 32 bit unsigned Integer, u32
For 64 bit unsigned Integer, u64
For 128 bit unsigned Integer, u128
For 256 bit unsigned Integer, u256
Float
Float is a floating point data type. In Carbon language, the Float data type supports both decimal and hexadecimal values. This is used to store double-precision floating point values. In carbon, they are named as type literals, basically denoted as Carbon.float(N) or fN where N must be a multiple of 8.
var a: f16 = 321.654
var b: f32 = 321.654e+12
var c: f64 = 321.654e-55
var d: f128 = 0x32.Ap+64
String
The string is a sequence of bytes used to contain UTF-8 encoded text. String data type allows declaring strings in two ways:
Single Line String can be written in double quotes.
2. Multi-Line String can be written between ‘ """ ‘
There is one more datatype related to String, i.e., StringView - This is used only when we need a read-only instance of our string.
Composite Data Types
In Carbon language, composite data types are further of four types
Tuples
Struct
Arrays
Pointers
Tuples
A Collection of values of a fixed size of different types is known as a Tuple. It is a structural type. The syntax is (<Expression>,<Expression>).
For example,
Var a: (i32, i32) = (1,2)
The Empty tuple set is represented as (). We can say that a is the tuple of two 32 bit Integers. Let’s see an example of two different types:
Var b: (i32, f32) = (1,2.0)
We can say that b is the tuple of a 32-bit Integer and one 32-float Integer. Moving forward, let’s understand a new term, Tuple of Expressions. If we take values like 1*2, then we can call it a Tuple Of Expressions.
Var b: (i32, i32) = (1*2,2*3)
We can also declare Tuple as the return type of function.
Also, for binary operations, both types of tuples should have the same number of expressions. Their operations should be defined basis the types of corresponding expressions.
Struct
A Struct can also be called as structural data. In a struct, we can access values using their name. If we want to return multiple values, then in such cases, we can use struct.
Pointers
To store the address of a variable, pointers are used. Let’s see an example to understand it more clearly.
var a: i32 = 5;
var p: i32* = &a;
var p2: i32* = &*p;
Arrays
The Collection of elements is known as an array. We can access values in arrays using indexes.
User-Defined Data Types
Whenever we define a data type from an existing data type, then we call it a user-defined Data Type. There are two types of user-defined data types in Carbon:
Classes
Choice Types
Classes are user-defined data types. In object-oriented programming, we make classes. A class can be declared with a class keyword. A class consists of variables and methods.
Choice Types Whenever we want to store data of different types, then we can use Choice Types. This contains a name and cases, which are separated by commas. Also, each case in this type has a name and an optional parameters if required.
Syntax
choice Result {
pass(value:i32),
Fail (error: string)
}
Frequently Asked Questions
Why are data types necessary?
Data Types are necessary because they help interpreters or compilers to understand the type of any value.
How struct identifies its members?
Struct identifies its members using names.
How are arrays declared in the Carbon language?
In Carbon language, using array type and array size, we can declare arrays. For example, var arr: [i32; 10] = (1,2,3,4,5,6,7,8,9,10);
Do we have null pointers in the Carbon language?
In Carbon language, we don’t have null pointers.
When can we use Tuples?
Whenever we want to use values in multiple coordinates, then in such cases, we can use tuples in Carbon language.
Conclusion
In this article, we have discussed data types in the Carbon language. We have understood how we can use primitive, Composite, and user-defined data types. You can also read the below-mentioned articles if you want to learn more about Data types.
I hope this article helped you to understand Data types. You can read more such articles on our platform, Code studio. You can also prepare for product-based companies on our platform, Coding Ninjas. If you are preparing for technical interviews, then you can daily practice questions on Coding Ninjas and enhance your knowledge.