In this blog, we will be going to understand carbon programming language concepts with some examples and will cover all the literal which will be used in this new programming language.
What is Carbon Programming Language?
Carbon language is a programming language which is proposed by Chandler Carruth who is a Tech Lead In programming language At Google.
As we know that C++ programming language is also good in performance but According to Chandler Carruth, Carbon language is a Successor of the C++ language and carbon language is created to improve further performance, scalability, and many more that will be discussed in this article only.
Currently, Carbon Language is in an Experimental project, So after completion of the experimental phase Carbon language provides interoperability, inheritance, and migration from existing c++ code to carbon easily. It means that if the C++ codebase is huge then we will be able to switch from C++ language to Carbon language easily.
Advantages Of Carbon Language:-
As we know Carbon Language is a Successor of C++ Programming language after Implementing Carbon language instead of C++ we will be able to achieve :
- Critical Performance In Software.
- Software and Langauge Evolution.
- Our code will become easy to read, write and understand.
- Practical safety and testing mechanisms
- Fast and scalable development
- Modern os platforms, hardware architectures, and environments
- Interoperability and Migration from existing C++ code
- Interoperability: don’t inherit all of the c++ tech-debt
- Migration: can provide immediate, dramatic improvements
- Language evolution: tool-based upgrades as carbon improves.
How to run Carbon Language Online:-
Currently, carbon language is in the experimental phase but we can create some sample programs like the addition of two numbers or many more using an online interpreter on the below-mentioned site:
https://carbon.compiler-explorer.com/
After clicking the above link below code will appear that returns the square of a given number in Square() functions.
package sample api;
fn Square(x: i32) -> i32 {
return x * x;
}
fn Main() -> i32 {
return Square(12);
}
Let’s discuss the element above code line by line:-
- package sample API:- Here package is a declaration of a package named as a sample.
- fn Square(x: i32) -> i32 {
- fn: fn stands for functions in carbon which is a keyword used to declare functions in carbon language.
- Square(x: i32)
- Square is the user-defined function name with one parameter in it
- x: i32
- x is the variable and “i32” is the integer data type in python with 32-bit storage space.
- return x*x; : the return type is the same as in c++ where value is returned to called functions in this case Main.
- fn Main() -> i32:
- Main() is the function that is executed whenever the program is compiled.
- i32: i32 is the return type but for the main function 0 will be returned everytime.
- return Square(12):
- Now the Square function is called with parameter 12 in it
- output:-
- 144
Fundamentals Of Carbon Programming Language:-
- Data Types In Carbon Language:
- Integer: In carbon language, there are mainly two types of Integers one is Signed Integers and another is Unsigned Integers.
- Signed Integers can be declared as i8, i16, i32, i64, i128, and i256.
- Unsigned Integers can be declared as u8, u16, u32, u64, u128, and u256.
- note: numbers after i and u represents a total number of bit a variable can hold not byte example 1byte = 8bits.
- Float: In carbon language declared float datatype can be f8, f16, f32, f64, f128, f256
- String: In carbon language, there are mainly two ways to store string data into variables.
- Single Line:- For a single line we use a single quotation mark at the beginning and end of the string. for example: var a: auto = “Hello World”;
- Muli-Line:- For muli-line we use double quotation marks at the beginning and end of the string like “”” some string on the first line and some string on the last line “””.
- Integer: In carbon language, there are mainly two types of Integers one is Signed Integers and another is Unsigned Integers.
- Variables In Carbon Language:
- we can declare variables using var keyword let discuss it below
- var siteName: auto = “www.codewithdc.com”;
- var is the keyword for the declaration of the variable.
- siteName is the variable name or variable identifier.
- note colon is used for declaring variable data type.
- auto is the keyword that stands for automatically and it means that variable data type will be declared automatically.
- note carbon language statement ends with a semicolon.
- var siteName: auto = “www.codewithdc.com”;
- we can declare variables using var keyword let discuss it below
- Further tutorials on carbon are coming soon…
Status Of Carbon Programming Language:-
Carbon programming language is in the experimental project and but we can try its element and creates some sort of programs.