You have to understand that the order of functions in your source file makes absolutely NO difference to what happens when you run the code.
The program always starts at main(), and from that point you can follow how the execution of the program would proceed by reading each line of main(). That's why your first six printf's happen first, in the order you wrote them in. At some point, getOperands() is called, and when that returns, main carries on from where it left off.
Also, the order of functions in your source makes almost no practical difference to the compiler. In fact, putting main last is a somewhat lazy approach while you have the benefit of being able to write a small program in a single source file. Larger projects have 10's, 100's or 1000's of source files, but they all begin somewhere with a single main() function.
For example, these two programs would compile the same.
Code:
#include <stdio.h>
#include <stdlib.h>
void getOperands(float *a, float *b)
{
printf("Immetti il primo operando: \n");
scanf("%f", a);
printf("Inserisci il secondo operando: \n");
scanf("%f", b);
}
int main()
{
int op;
float a, b, c;
int err;
printf("Semplice Calcolatrice\n");
printf("Inserisci 1 per la somma\n");
printf("Inserisci 2 per la sottrazione\n");
printf("Inserisci 3 per la moltiplicazione\n");
printf("Inserisci 4 per la divisione\n");
printf("La tua scelta: \n");
scanf("%d", &op);
/*La condizione di errore viene inizializzata*/
err = 0;
/*Esegui l'operazione richiesta*/
switch( op )
{
case 1:
getOperands( &a, &b);
c = a + b;
break;
case 2:
getOperands( &a, &b);
c = a - b;
break;
case 3:
getOperands( &a, &b);
c = a * b;
break;
case 4:
getOperands( &a, &b);
if( b == 0 )
{
printf("Impossibile dividere per zero!\n");
err = 1;
}
else
{
c = a / b;
}
break;
default:
printf("Operazione errata\n");
err = 1;
}
if( err == 0 )
printf("Il risultato vale: %f\n", c);
exit(0);
}
vs.
Code:
#include <stdio.h>
#include <stdlib.h>
// This is a function prototype.
// It's purpose is to inform the compiler that the function
// getOperands() will exist at some point in the future,
// either later in this file, or perhaps some other file.
// Regardless of where it is, the compiler has enough
// information to allow you to make calls to this function
// within main(), or indeed any other function following in
// this source file.
void getOperands(float *a, float *b);
int main()
{
int op;
float a, b, c;
int err;
printf("Semplice Calcolatrice\n");
printf("Inserisci 1 per la somma\n");
printf("Inserisci 2 per la sottrazione\n");
printf("Inserisci 3 per la moltiplicazione\n");
printf("Inserisci 4 per la divisione\n");
printf("La tua scelta: \n");
scanf("%d", &op);
/*La condizione di errore viene inizializzata*/
err = 0;
/*Esegui l'operazione richiesta*/
switch( op )
{
case 1:
getOperands( &a, &b);
c = a + b;
break;
case 2:
getOperands( &a, &b);
c = a - b;
break;
case 3:
getOperands( &a, &b);
c = a * b;
break;
case 4:
getOperands( &a, &b);
if( b == 0 )
{
printf("Impossibile dividere per zero!\n");
err = 1;
}
else
{
c = a / b;
}
break;
default:
printf("Operazione errata\n");
err = 1;
}
if( err == 0 )
printf("Il risultato vale: %f\n", c);
exit(0);
}
void getOperands(float *a, float *b)
{
printf("Immetti il primo operando: \n");
scanf("%f", a);
printf("Inserisci il secondo operando: \n");
scanf("%f", b);
}