<< Prev Next >>
Basic data types
The goal for every program is processing some data and giving a result with it. For processing this data, high-level languages like C define types of data. Is the data a number? If so, which type of number? Can it be negative? Can it be decimal? Or if it's not a number, is it a sequence of characters? This typification helps limiting bugs on the code, which was one of the many problems programmers had with assembly.
One important thing to note is that in C everything is a number. Even a character is a number (its ASCII code is stored). This is because for computers everything are numbers too. Keep this awake in your mind forever when dealing with computers.
On a programmer's point-of-view, C distinguishes basically between 2 types of data: numbers and characters (and strings of characters). Let's begin with the numbers.
Integers
Integers are non-decimal numbers. Basic type for this type of data is int, which defines a "32-bit signed integer". "32-bits signed" means one bit is used a sign flag (0 = positive, 1 = negative) and the rest of the 31 bits are used for the value, which means this data type can hold a value from −2,147,483,648 to 2,147,483,647. Any number outside this range cannot be represented with the int data type (you're not likely to be handling such big numbers, so don't worry).
Several modifiers can be applied on the int data type. unsigned is one of the most common. unsigned int indicates that the data cannot be negative, thus boosting the positive range from 0 to 4,294,967,295. Even if we don't need such a range, it's always wise to define an unsigned int when needed.
Let's see an example:
Code: Select all
// printf
#include <stdio.h>
// Entry point
int main()
{
// We declare an integer and store there the value -35
int number1 = -35;
// We declare a positive integer and store there the value 15
unsigned int number2 = 15;
// We use printf to show the numbers
// Note the use of %d for int and %u for unsigned int
printf("The numbers are %d and %u\n", number1, number2);
// Back to shell
return 0;
}I think most confusing line might be
Code: Select all
printf("The numbers are %d and %u\n", number1, number2);For more information about the use of printf, please refer to the documentation (man 3 printf).
Decimal numbers
Decimal numbers are referred to as floating-point numbers on computer science and float in C. This is a 32-bit single-precision floating point number, denoted as float in C. As with the other data types, having a fixed bits length means it can only store a range of numbers. In this case, not all decimal numbers can be stored exactly. For example, 0.1 cannot be stored exactly on a C float data type, so it's approximated.
Let's see an example:
Code: Select all
#include <stdio.h>
int main()
{
// We declare a decimal number and assign him the value 3.5
float number1 = 3.5;
// Note the use of %f for float
printf("Your floating point number is %f\n", number1);
return 0;
}One single character is represented by the char data type. It's one byte long, which means it can store values from -128 up to 127. Negative values are rareley used with char, so it's usual to see it declared as unsigned char. The value stored on this data type is the ASCII code of the character.
Let's see an example:
Code: Select all
#include <stdio.h>
int main()
{
// We declare a character and assign him the value 'a'
// Characters are denoted with a single quote (')
char character1 = 'a';
// Note the use of %c for character
// We also output the character as an integer to check the value stored
printf("Your character point number is '%c' and its ASCII code is %d\n", character1, character1);
return 0;
}A character string is a list of characters. It's represented as char* or char[] in C (which are actually the same thing with some differences). C uses what's called null-terminated character strings, which means the end of the character string is marked with a NULL character, the character with ASCII code 0 (do not confuse with character '0'). For example, the string "Hello" would be stored on memory as 72 101 108 108 111 0, thus being 6 bytes despite the fact that the string is only 5 characters long. This should be always in your mind while working with strings.
Let's see an example:
Code: Select all
#include <stdio.h>
int main()
{
// We declare a character string and assign him the value "Hello world!\n"
// Character strings are delimited with a double quote (")
char* string = "Hello world!\n";
// Note the use of %s for character string
printf("%s", string);
return 0;
}See you on the next part, until then carpe diem!
<< Prev Next >>







