>Is this the correct way to use scanf.
Very close:
Code:
#include <stdio.h>
#include <stdlib.h>
int main ( void )
{
int a = 0, b = 0;
printf ( "Enter two numbers: " );
/*
** Always check return values.
*/
if ( scanf ( "%d %d", &a, &b ) == 2 ) {
printf ( "A\tB\tSum\tDiff\tProd\n" );
printf ( "%d\t%d\t%d\t%d\t%d\n",
a, b, a + b, abs ( a - b ), abs ( a * b ) );
}
else
perror ( "Invalid input" );
/*
** system() is not very portable because it's
** argument is platform dependent. This works
** just as well.
*/
printf ( "Press return to continue" );
(void)getchar();
/*
** When you include stdlib.h, it never hurts to
** use the EXIT_SUCCESS macro to improve readability.
*/
return EXIT_SUCCESS;
}
>I want to learn to write correct, effiecient and portable code.
You will want to invest in several good books on C as well as the ISO C standard. Refer to them for everything until you are confident in your knowledge. Another excellent idea is to get a Lint program for C which will warn you about all kinds of subtle problems concerning syntax, semantics, portability, etc... My favorite Lint is LCLint/Split which can be found here. It takes some time to get used to so many warnings, but you'll find your code improves dramatically with regular Linting.
-Prelude