2

I'm doing one of the beginner C-programming exercises, building a simple calculator that shows the menu, takes in the user's choice of operation, takes in 2 operands and shows the result. Here's what I have so far:

#include<stdio.h> int main(){ int add (int a,int b) {return a+b;} int substract (int a,int b) {return a-b;} int multiply (int a,int b) {return a*b;} double divide (int a,int b) {return a/b;} int modulo (int a,int b) {return a%b;} int num1, num2, choice; printf ("======MENU=======\n"); printf ("1. Add\n"); printf ("2. Substract\n"); printf ("3. Multiply\n"); printf ("4. Divide\n"); printf ("5. Modulo\n"); printf ("Please, select your operation and press enter:\n"); scanf ("%d", &choice); switch (choice){ case 1: printf ("Please, enter the two operands separated by space and press enter:\n"); scanf ("%d %d", &num1, &num2); add (num1, num2); printf ("%d\n", add); break; case 2: printf ("Please, enter the two operands separated by space and press enter:\n"); scanf ("%d %d", &num1, &num2); substract (num1, num2); printf ("%d\n", substract); break; case 3: printf ("Please, enter the two operands separated by space and press enter:\n"); scanf ("%d %d", &num1, &num2); multiply (num1, num2); printf ("%d\n", multiply); break; case 4: printf ("Please, enter the two operands separated by space and press enter:\n"); scanf ("%d %d", &num1, &num2); divide (num1, num2); printf ("%lf\n", divide); break; case 5: printf ("Please, enter the two operands separated by space and press enter:\n"); scanf ("%d %d", &num1, &num2); modulo (num1, num2); printf ("%d\n", modulo); default: printf ("Invalid entry\n"); break; }; scanf ("%d"); return 0; } 

My issues with this code are:

  1. The program doesn't return the correct values. The addition option shows numbers over 4000000. Other operations do the same.
  2. The program should scan for an integer to be entered once the operation is complete and then end. However, it crashes once the integer has been entered.
3
  • 2
    Nested functions are non-standard, non-portable and generally a bad idea. Commented Nov 25, 2015 at 8:13
  • Note that your divide function, despite returning a double, will truncate the division. Use 1.0 * a / b or similar instead. That also fixes the zero b case. Your specific problem: you're attempting to print the addresses of functions; using an incorrect format specifier to do so. Your program behaviour is undefined. Commented Nov 25, 2015 at 8:14
  • @Bathsheba Important point, I'll add that to my answer. :) Commented Nov 25, 2015 at 8:18

2 Answers 2

3

TL;DR -- printf ("%d\n", add); does not print the returned value of the previous add() function call, it tries to print the function address itself. Now, in case of using wrong argument to a format specifier, undefined behavior is the end product.


To elaborate, the main problem is the approach. As you have written

 add (num1, num2); printf ("%d\n", add); 

this will not add num1 and num2 and print the result of the addition magically. You need to either

  1. collect the return value of the function in a variable and print that variable later.

    int res = -1; res = add (num1, num2); printf ("%d\n", res); 
  2. Otherwise, you have to call the add() function itself as the argument to the format specifier in the format string in printf(), like

    printf ("%d\n", add (num1, num2);); 

Same goes for the other function calls in all the cases, too.

Also, as mentioned by Mr. Bathseba in the previous comment, though you're using the return type as double in your division function, the actual division involves the operands of type int, which makes the division to be performed as integer division, then the result will get promoted to double, which probably you don't want. You have to enforce the floating point division.

And finally, regarding usage of nested function, it's a bad idea as this is not standard C. Please see this answer for more info.

Sign up to request clarification or add additional context in comments.

2 Comments

Upvoted, but you should also mention that they are using nested functions, which are a compiler extension.
@Bathsheba Right. Let me get a link to that. :)
1

You are printing function addresses:

modulo (num1, num2); printf ("%d\n", modulo); 

You must replace by:

int var = modulo(num1, num2); printf ("%d\n", var); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.