Linux is the most used OS in the world
But without C, we'd still be working with something like an abacus. Thanks to Dennis Ritchie (and not to any other "Jobs").
Data Types
An integer is a whole number (without decimals). It is a number between -2,147,483,648 and +2,147,483,647.Rules for integers:
- An integer must have at least one digit (0-9)
- An integer cannot contain comma or blanks
- An integer must not have a decimal point
- An integer can be either positive or negative or unsigned
A Boolean represents two possible states: TRUE or FALSE. Not all programming languages support booleans, but we can use an integer to obtain the same result at the same cost
An array stores multiple values in one single variable.
A string is a sequence of characters, like "Hello Obilab!".
Why can't I use strings for everything and why should I prefer int over float?
With an 8-bit word, we can represent
(base 10).
= 256 combinations.
So the decimal range will be [0,255].
But if we need to work in Z, instead of the N set, we must reserve 1 bit for the sign.So,
with a range [-127,+127].
We don't want to discuss two's complement representation, but it's necessary to say that the real range
will be [-128,+127].
The problem arises when we need to represent one or more decimal points.
7,123456 is completely different from 712345,6 for your calculator.When we have to work with floating-point numbers, we need to know what they cost our computer: one or more bits for every word. So, returning to our 8-bit word, it becomes:
The MSB for the sign, a maximum of 6 bits for the integer part, and at least 1 bit for the decimal part (the LSB).
The new range will decrease to
integer numbers from 0 to 63.Our computers don't work with 8-bit words, but it is a huge mistake to waste resources.
In a string of chars, we can write any ANSI char, but we cannot perform any arithmetic operations on it. So if we put the number 3 into a String (S1), and a number 4 into another string (S2), we cannot compute S1+S2 (unless you want to give me a heart attack).
Variables
Variables are "containers" for storing information. Assignment works from right to left. The right value goes inside, irreversibly overwriting the content of the left side (a few specific situations work from left to right, for example, ++i).Here is a simple C code
#include <stdio.h>
int main()
{
/* This is a comment in language ANSI C */
int sum=0; /* This is a variable declaration, integer type */
int unsigned a=3, b=4; /* This is a variable declaration, integer unsigned type */
/* assignment goes from right to left */
sum=a+b; /* the sum of a+b will be assigned to the variable "sum" */
printf ("The sum between %d and %d is %d\n\n", a,b,sum);
return 0;
}
The same program in bash
#!/bin/bash
# this is a comment in bash. The first line of a bash script defines the shell
a=3 #we do not need to declare types
b=4 #we do not need to terminate lines with " ; "
sum=$(( $a+$b )) #we do not need to declare the variable sum
#but we need to take care about spaces
echo The sum between $a and $b is $sum
Limits of integers, signed and unsigned
#include <stdio.h>
#include <limits.h>
int main()
{
printf("Max int = 2^31 [ %d ]\n", INT_MAX);
printf("Min int = [ %d ]\n", INT_MIN);
printf("Max unsigned int 2^32 = [ %u ]\n", UINT_MAX);
printf("MaxUlong unsigned int 2^64 = [ %lu ]\n", ULONG_MAX);
return 0;
}
Homogeneous vs heterogeneous array
In C, the arrays are really powerful data containers, but
- It is necessary to declare it
- It is necessary to declare the data types we want to put inside
- It is necessary to declare the dimension
In ANSI C, arrays are homogeneous data types.
#include <stdio.h>
int main()
{
int A[3]={17,9,99};
printf("%d %d %d ", A[0], A[1], A[2]); /* Index starts from 0 */
return 0;
}
Same code in bash... more or less... the word 'stefano' isn't an integer
Bash arrays are heterogeneous data types.
#!/bin/bash
A=("9" "22" "stefano") #no types declaration
echo ${A[0]} ${A[1]} ${A[2]} # mad syntax for arrays with bash

