Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Last Updated: Mar 27, 2024
Difficulty: Easy

Variables in Carbon

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

Introduction

Do you know about the recently introduced language by Google? Do you know that it is similar to C++?

Introduction to Carbon Language


In this article, we will discuss the recently introduced Carbon Language. We will understand how we can declare variables in this language and the scope of these variables. So, let’s get started by understanding Carbon language.

What is Carbon? 

Carbon is a programming language. This language was introduced in July 2022, and it is still in the development process. We can also say that Carbon is a successor language of C++. Moving forward, let's understand more about Carbon.

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

Variables in Carbon

Whenever we want to store data, it requires a particular memory location. In order to call or use these stored data, we need to give these locations a name known as variables. We must choose useful variable names to make them understandable. Let's suppose you have to store the score of a student. Then, for storing the score, its useful variable can be like score or marks, but if we take its variable name as s or m, then it might not be understood by the reader. We can also change variable names in our program according to our needs.                            

Variables in Carbon

Rules of Writing Variables

For declaring variables there are some rules which we must follow in order to avoid any errors.

  • The variable name must be a letter.
  • Variable names cannot start with a digit. It must start with an alphabet. 
  • Variable names can never be the same as keyword names.
  • Names also don't include spaces between letters.
  • Variable names are case sensitive, which means that score and Score are considered as different variable names.

If we do not follow these rules for writing variable names then we will get errors as shown below.

Error for wrong Variable name

Characteristics of Variables 

There are four main characteristics of variables:

  1. Identifier (var or let)
  2. Data Location (variableName:)
  3. Type (i32)
  4. Value 

Moving forward, let’s understand them briefly.

Declarations of Variables

In Carbon language, a variable is declared using the var keyword. If you want to declare constant variables, you can use the let keyword. In Carbon, we end the declaration of a variable with a colon: character. In Carbon, i32 can be used for declaring 32-bit integer type, and i64 is used for declaring 64-bit integer type.

For example, if we want to declare a var variable with a variable name “a” and a constant variable with a variable name both of integer type, then we can write it like this.

var a: i32
let b: i32
var Variable
let Variable


In Carbon language, we can also declare auto variables using the keyword auto. These variables can automatically change their type according to their assigned value.

For example, If we want to declare an auto variable with variable name c so we can write it like this:

var c: auto = "Hello Ninjas!";
Declaring auto Variable

So, our compiler will automatically get to know that it is a string and will execute it accordingly. Let's run this code and check its output. You can go to Compiler Explorer to run Carbon code.

var c: auto = "Hello Ninjas!";
print(c);
auto Variable

Scopes of Variables

The scope of variables is defined as variables life in a program. There are two types of scopes:

  • Local
  • Global
Scopes of Variables

Let's understand it more briefly.

Whenever we write a program, we have a main function in our program, which is executed first by our compiler, so if we declare variables inside our main function or any other function which we made, then that variable is only accessible in that function, which directly means that we have declared our variables locally. This means that:

Local variables are declared within a function and are in use within that function only, not outside it. Whereas, Global variables are those variables that we can declare globally outside the function and are accessible throughout the program.

Frequently Asked Questions

Why is carbon language built?

Carbon language is made to make codes easier to read and write. It is built to enhance the performance of the software.

When did Carbon language is introduced?

Carbon language was introduced in July 2022 by a Google engineer.

Is Carbon language Bi-directional?

Carbon language is Bi-directional as it allows us to call C++ language code from Carbon and also Carbon language code to C++.

Is Carbon language an open source?

Carbon Language is an open-source language.

Is Carbon language an interpreted language?

Carbon Language is a byte-code-interpreted language.

Conclusion

In this article, we have understood Carbon language. We have learned how we can declare and use variables in this language. We have also learned about the Scopes of variables. To read more about Variables, refer to the below-mentioned article.

If you find this article helpful, then you can read many such useful articles on our platform Code Studio. You can also practice coding questions in multiple languages on our platform. Coding Ninjas provides you with a pathway to crack good companies at a good package. So, keep practicing and keep learning with us.

Happy Coding!

Topics covered
1.
Introduction
2.
What is Carbon? 
3.
Variables in Carbon
4.
Rules of Writing Variables
5.
Characteristics of Variables 
6.
Declarations of Variables
7.
Scopes of Variables
8.
Frequently Asked Questions
8.1.
Why is carbon language built?
8.2.
When did Carbon language is introduced?
8.3.
Is Carbon language Bi-directional?
8.4.
Is Carbon language an open source?
8.5.
Is Carbon language an interpreted language?
9.
Conclusion