# Thread: Function to produce sum with int and char

1. ## Function to produce sum with int and char

My teacher for my class is asking the following question:

Write a simple C program that defines a function (other than main) that takes two parameters, an int and a char, and that returns their sum as an int. Be sure to declare the variables, and assign them values using assignment statements. In the main function, your sample program should call your function, passing int and char variables and assigning the return value to another int variable.

How do you add an int and a char together to yield an int sum? I'm confused by this. Help appreciated.

Code:
```#include <stdio.h>
#include <stdlib.h>

int x;
char y;
int z;

int sumfunc(int x, int y){
z= x + y;
return z;
}

int main(void){

scanf("%i", &x);
printf("Please type a single digit number.\n");
scanf("%c", &y);
printf("The sum of the two numbers is %i\n", sumfunc(x,y));

system("pause");
return 0;
}```
Obviously this doesn't work because using char doesn't make any sense.

2. There's a couple of ways. chars are really just small ints and you can treat it as such. The other way would be to convert the character to its int equivalent by subtracting '0' from it.
Code:
```int sumfunc(int x, char y)
{
z = x + y;
return z;
}```
Of course, using %c in the scanf() confuses things and will not give you the expected output.

Your other option is to subtract '0' from y after reading it in and storing that result in something like and int.
Code:
```scanf("%c", &y);
int yInt = y - '0';```
Then you could pass yInt to your function. This works because the digits are laid our sequentially in the ASCII table. '0' is 48 decimal in the ASCII table. '1' is 49, '2' is 50, etc. Subtracting any single digit by '0' (48) will give you the actual number (e.g. '2' - '0' is 50 - 48 is 2).

3. Ok, first thing, move the declaration of z into the top of the sum function. you should also move the declarations of x and y into your main function. There's no reason for them to be global.

Yes you can sum an int and a char... A char is really just an 8 bit integer.

You can add any two numbers where the product will fit into the lvalue's (left of the equal sign) type.

The problem is that using scanf("%c", &y); y will be equivalent to the ascii value of the character you type, so if you type 0 you're gonna get 48... not 0 from scanf. If you use scanf("%d",y); it will be the true value... you should be able to enter any number from -128 to +127.

When in doubt, make a simple test proggy like this...
Code:
```int x = 0;
int y = 50;
char z = 100;

x =  y + z;

printf( "x = %d \n", x);```

4. char is not required to be 8 bits. bits in char >= 8 as per standard.

5. Originally Posted by CommonTater
...
Yes you can sum an int and a char... A char is really just an 8 bit integer.
...
Make that one byte. A char is guaranteed to be 1 byte, though a byte may not necessarily be 8 bits.

6. Thanks, guys. Got it working with the following code and also understand!

Code:
```#include <stdio.h>
#include <stdlib.h>

int x;
char y;
int z;

int sumfunc(int x, char y){
z= x + y;
return z;
}

int main(void){

scanf("%i", &x);
scanf("%d", &y);
printf("The sum of the two numbers is %i\n", sumfunc(x,y));

system("pause");
return 0;
}```

7. Oh, when I move the declarations out of global, the sum function doesn't work!

8. Change
z= x + y;
to
return x + y;
Plain and simple.

Or make z local:
int z = x + y;
return z;

Plain and simple. Why do you need globals?

9. Originally Posted by quintenmater
Oh, when I move the declarations out of global, the sum function doesn't work!
Try it like this...

Code:
```#include <stdio.h>
#include <stdlib.h>
int sumfunc(int x, char y){
int z;
z= x + y;
return z;
}

int main(void){
int x;
char y;

scanf("%i", &x);
scanf("%d", &y);
printf("The sum of the two numbers is %i\n", sumfunc(x,y));

system("pause");
return 0;
}```

10. the atoi function in the stdio header file can return an int from a char then you can just add it up. You could try something like this:

Code:
```int sumfunc(int x, char y){
z = x + atoi(y) /* returns the int value of y) */
return z;
}```
you can read up on it here: atoi - C++ Reference

11. Originally Posted by Elysia
Change
z= x + y;
to
return x + y;
Plain and simple.

Or make z local:
int z = x + y;
return z;

Plain and simple. Why do you need globals?
z can't be local, it is dangerous to return a local varible.

12. > z can't be local, it is dangerous to return a local varible.

The rule is "POINTER TO LOCAL" which is dangerous. Returning the value stored in a local is perfectly safe, because the return makes a copy.

13. the atoi function in the stdio header file can return an int from a char then you can just add it up. You could try something like this:
What ?? Think before posting. It seems like you didn't even read the link you posted.

14. Originally Posted by patrink
the atoi function in the stdio header file can return an int from a char then you can just add it up. You could try something like this:

Code:
```int sumfunc(int x, char y){
z = x + atoi(y) /* returns the int value of y) */
return z;
}```
you can read up on it here: atoi - C++ Reference
atoi wants a string, not a char. Tsk.

15. Originally Posted by Salem
> z can't be local, it is dangerous to return a local varible.