Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
Do you know about the recently introduced language by Google? Do you know that it is similar to C++?
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.
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.
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.
Characteristics of Variables
There are four main characteristics of variables:
Identifier (var or let)
Data Location (variableName:)
Type (i32)
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
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!";
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);
Scopes of Variables
The scope of variables is defined as variables life in a program. There are two types of scopes:
Local
Global
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.