I'm trying to write a function in C that will take all the digits of a positive integer and add them together. Here's my code:
int num_test(score){
int total = 0;
while(score){
total = (score );
score /= 10;
}
return total;
}
However, I keep receiving signal: segmentation fault (core dumped)
every time I try to pass anything to it in my main function. I'm relatively new to C, so I'm unsure on how to fix this error.
CodePudding user response:
Your function 'signature' needs to know the datatype it receives
int num_test(score){
int total = 0;
while(score){
total = (score );
score /= 10;
}
return total;
}
int num_test( int score ) {
int total = 0;
while(score) {
total = (score );
score /= 10;
}
return total;
}
Or, there could be other problems in unpublished code.
CodePudding user response:
it's the problem with not specifying a data type for the parameter called score
so instead of :
int num_test(score)
write :
int num_test(int score)
or what ever you like, it may be unsigned int score
or whatever you like, but you have to specify a data-type for the function paramter