Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
What are Data Types?
3.
Primitive Data Types
3.1.
Bool
3.2.
Int
3.2.1.
Signed Int 
3.2.2.
Unsigned Int 
3.3.
Float
3.4.
String
4.
Composite Data Types
4.1.
Tuples
4.2.
Struct
4.3.
Pointers
4.4.
Arrays
5.
User-Defined Data Types
6.
Frequently Asked Questions
6.1.
Why are data types necessary?
6.2.
How struct identifies its members?
6.3.
How are arrays declared in the Carbon language?
6.4.
Do we have null pointers in the Carbon language?
6.5.
When can we use Tuples?
7.
Conclusion
Last Updated: Mar 27, 2024
Easy

Data Types in Carbon

Leveraging ChatGPT - GenAI as a Microsoft Data Expert
Speaker
Prerita Agarwal
Data Specialist @
23 Jul, 2024 @ 01:30 PM

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?

Introduction to data types

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:

  1. Primitive,
  2. Composite, and
  3. User-Defined

 

Moving Forward, let's understand them briefly.

Categories of data types

                                 

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Primitive Data Types

In Carbon language, there are four types of Primitive Data Types

  1. Bool
  2. Int
  3. Float
  4. 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.

  1. Signed Int (Carbon.Int(N))
  2. 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:

  1. Single Line String can be written in double quotes. 
Single Line String



2. Multi-Line String can be written between ‘ """ ‘

Multi-Line String


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

  1. Tuples
  2. Struct
  3. Arrays
  4. 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.

fn TupleFunction(a: i32, b: i32) -> (i32, i32) {
  return (1*a, 2*b);
}

 

We can also access tuple values using an index, an index starting from 0 to length-1. For example

fn TupleFunc(a: (i32, i32)) -> (i32, i32) {
  return (a[0]*1, a[1]*2);
}
Tuple Example


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.

Struct Example


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;
Pointers Example

Arrays

The Collection of elements is known as an array. We can access values in arrays using indexes. 

Array Example

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:

  1. Classes
  2. 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.

Classes Example


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.

Happy Coding!

Live masterclass