Linux is the most used OS in the world

But without C, we'd still be working with something like an abacus. Thank you to Dennis Ritchie (and not to any other "Jobs").

LinuxBanner

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 float (floating point number) is a number with a decimal point or a number in exponential form.
A Boolean represents two possible states: TRUE or FALSE. Not all Programing Language supports boolean, but we can use, as well, an integer, in order to obtain the same result with the same costs
An array stores multiple values in one single variable.
A string is a sequence of characters, like "Hello Obilab!".

Why I cannot use strings for anything and why I would have to prefer int to float?

With a word of 8 bit, we can represent LatexGif (base 10).
LatexGif = 256 combinations. So the decimal range will be [0,255]. But if we need to work in Z, instead of N set, we need to reserve 1 bit to the sign.
So, LatexGif with a range [-127,+127]. We do not want to talk about complement representation, but it's necessary to say that the real range will be [-128,+127]. The problem borns once we need to represent one ore more decimal point. 7,123456 is deeply different from 712345,6 for your calculator.
When we have to works with floating points number, we need to know what it costs for our calculator, one or more bit for every word. So, coming back to our 8bit word it will become:
The MSB bit for sign, maximum 6 bits for integer parts and at least 1 bit for decimal part, the LSB
The new range will decrease to LatexGif integer numbers from 0 to 65.
Our computers doesn't work with word of 8 bit, but It is a great mistake to waste resource.
Into a string of chars, we can write any ANSI char, but we cannot do any arithmetic operation on it. So if we put the number 3 into a String (S1), and a number 4 into another string (S2), we cannot do S1+S2 (unless you wants to make me die with a heart attack).

Variables

Variables are "containers" for storing information. Assignment works from right to left. The right value go inside, overwriting irremediably, the content of the left side (few particular situations works from left to right, for example, ++i).

That's 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, with bash


#!/bin/bash # this is a comment in bash. The first line of bash define the shell a=3 #we do not need to declare types b=4 #we do not need to terminate line with " ; " sum=$(( $a+$b )) #we do not need to declare the variable sum #but we need to take care about spaces echo The sum betweeen $a and $b is $sum

Limits of integer, signed and unsiged


#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 container, 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 type.


#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 type.


#!/bin/bash A=("9" "22" "stefano") #no types declaration echo ${A[0]} ${A[1]} ${A[2]} # mad syntax for arrays with bash