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){
printf("Please type an integer.\n");
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.