Data Types
data types constitute the semantics and characteristics of storage of data elements.
Main types
Char
Can contain basic character set.
Int
Capable of containing at least the [-2,147,483,648 to 2,147,483,647] range.
Float
Real floating-point type, usually referred to as a single-precision floating-point type.
String
used to store a sequence of characters.
Here is an example of declaring variables with each type:
Char
(char a = 'a');
Int
(int a = 1);
Float
(float a = 1.1);
String
(string a = "variable");
Boolean type
The boolean type are use for conditions and to store the current status of statement.
A boolean can be:
In the following example, the boolean True is used in a condition:
(if (#t) {
(puts "a");
})
We can also initialize a variable with a boolean type:
(bool a = #f);
Pointer types
The Pointer in C, is a variable that stores address of another variable.
The purpose of pointer is to save memory space and achieve faster execution time.
Each variable also has its address.
The address can be retrieved by putting '&' before the variable name.
Synopsis
Here's the syntax to declare pointer:
(<typename> * <variable-name> = malloc(<pointer-size>));
Addresses
Although all pointers are addresses , we want the type of the pointer to indicate what is being pointed to.
Example
Lets consider this case:
(int a);
(int * b = &a);
In this case, we can point b at a by using the & operator.
We are assigning to b the address of the integer a;
You can also pass address to function:
(func (increment (int * i)){
(?i = (+ 1 ?i);)
})
(func (test (int a)){
(increment &a);
(a);
})
You can notice that we use a new operator ?. We will explain right now what is it
Dereferencing Pointers
(int * b = malloc(1));
This example would define a pointer to an integer, and ?p
would dereference that pointer, meaning that it would actually designates the memory location where p
is defined.
By doing that we can modify a value directly where it is stored, so we don't need to return it in the previous case.
Type casting
Converts between types using a combination of implicit and user-defined conversions.
Synopsis
(int a = <value>)
(<new-type> b = ((<new-type>)a)
Here b
takes the value of a
and converts its type from int to <new-type>.
Example
(float f = 48.7)
(char c = '1')
(int i = ((int)f))
(int i2 = ((int)c))
Here f
is a float and c
a char and their type is converted to int in i
and i2
. Here i = 48
(so it's rounded) and i2 = 49
.
Last updated