Still going with C and enjoying it!
I've been playing with functions today and have coded up a simple program that gets two numbers from the user and returns them both with the sum, difference and product by calling a function. (pointless I know... but using it to get used to how it works etc).
Anyway, the program seems to compile and run fine
My Code:
- Code: Select all
#include <stdio.h>
int main()
{
int number1;
int number2;
/* Prompt user to choose the 1st number */
printf("Please enter a number of your choice: ");
/* Assign the 1st number to int number1 */
scanf("%d", &number1);
/* Prompt user to choose the 2nd number */
printf("Now enter another number of your choice: ");
/*Assign the 2nd number to int number2 */
scanf("%d", &number2);
/* Display the user's two chosen numbers */
printf("You entered: %d and %d\n\n",number1,number2);
/* Get the SUM, DIFFERENCE and PRODUCT of the two numbers. */
for (int i = 0; i <= 3 ; i++)
{
calcNumbers(number1, number2, i);
}
printf("Finished.");
return 0;
}
void calcNumbers (int num1, int num2, int i)
{
if (i == 0)
printf("The SUM of the numbers (%d + %d) = %d\n", num1, num2, num1 + num2);
if (i == 1)
printf("The DIFFERENCE of the numbers (%d - %d) = %d\n", num1, num2, num1 - num2);
if (i == 2)
printf("The PRODUCT of the numbers (%d * %d) = %d\n", num1, num2, num1 * num2);
}
The compiler gives me the following:
[steve@Gimli learning]$ gcc -std=c99 03.c -o run_03-test
03.c: In function âmainâ:
03.c:30:3: warning: implicit declaration of function âcalcNumbersâ [-Wimplicit-function-declaration]
03.c: At top level:
03.c:37:6: warning: conflicting types for âcalcNumbersâ [enabled by default]
03.c:30:3: note: previous implicit declaration of âcalcNumbersâ was here
... but the program seems to run ok:
[steve@Gimli learning]$ ./run_03-test
Please enter a number of your choice: 5
Now enter another number of your choice: 5
You entered: 5 and 5
The SUM of the numbers (5 + 5) = 10
The DIFFERENCE of the numbers (5 - 5) = 0
The PRODUCT of the numbers (5 * 5) = 25
Finished.[steve@Gimli learning]$
Sorry for my still total newbie questions... whilst confident in Java... I'm finding C to be a different ballgame...