Is it possible to return a char datatype from main() in c? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Is it possible to return a char datatype from main() in c?

https://code.sololearn.com/c7UmZvFO4vNd/?ref=app I tried returning a char datatype from main() to my function name() but it shows a lot of errors. Is returning char allowed in c or did I go wrong in this code?

12th Jul 2021, 10:06 AM
Srinath
10 Answers
+ 3
It is recommended to use `int` for `main` function return type, details here https://en.cppreference.com/w/c/language/main_function Your main function return type is `int`, not `char` ~ (code edited). The errors came from incorrect parameter type for `name` function definition and forward declaration. Change type of parameter <t> from `char` to `char*` to get the code to run. (Edited)
12th Jul 2021, 10:22 AM
Ipang
+ 1
Ipang Oh thanks that worked! But could you please explain why char* should be used instead of just char in the parameters? And still I'm getting one warning saying main() return is not int but char, how can I rectify this?
12th Jul 2021, 11:08 AM
Srinath
+ 1
Verstappen, A string in C is `char` array, and variable <b> in `main` is a string (`char` array). When passing a string as argument for a function, `char*` is used. But if the variable is just a `char` (not `char` array), then it is safe to define argument type as `char`. Passing variable as argument to function (by using pointer) also means the changes to the argument will take direct effect, and reflects in function caller scope. So changes to argument <t> in `name` function will reflect in `main` function (caller). BTW, last time I read your code the `main` function had `int` as return type, now it is changed to `char`. Just change the return type back to `int` to get rid of the warning.
12th Jul 2021, 12:05 PM
Ipang
+ 1
Ipang thanks for this great explaination. So as passing variable as argument (using pointer) to function will have direct effect on the caller, does that mean I don't have to return the value as the changes would have already occured in the main() ?
12th Jul 2021, 12:22 PM
Srinath
+ 1
Verstappen, I meant to say that any changes made to the argument <t> inside `name` function will reflect in actual variable <b> in `main` function, because they are one thing under different name (<b> in `main`, <t> in `name`). As changes to arguments passed by pointer are directly effective, it's at your discretion whether or not to return the changed argument.
12th Jul 2021, 12:30 PM
Ipang
+ 1
Ipang ohh okay got it, that was really helpful!! Thanks -))
12th Jul 2021, 12:47 PM
Srinath
+ 1
Verstappen, Look at this little snippet. Here in changes() <number> parameter is readable and writable, but in no_changes() <number> parameter is read-only, and will trigger error cause we try to change the data referred by the pointer. #include <stdio.h> void changes( int *number ) { *number *= 2; } void no_changes( int const *number ) { *number *= 2; // Error: can't change const argument } int main() { int n = 5; changes( &n ); // works okay printf( "after changes() n = %d\n", n ); no_changes( &n ); // compile error printf( "after no_changes() n = %d\n", n ); // not displayed cause above error return 0; }
12th Jul 2021, 1:23 PM
Ipang
+ 1
Ipang so we didn't use return here as we used pointer, as the pointer we used here directly changes the value of the address which is passed. Am I right on this? As using return here is not even necessary . And this pointer changes directly to the main(), is for all datatypes right? not only for int and char
12th Jul 2021, 3:26 PM
Srinath
+ 1
Verstappen, Pointers is a topic big enough to be written in a book, and I am also still not fully understanding it but the basic use cases. So here a link to an e-book that might explain it better than I could ever did with my limited knowledge. http://www.iitk.ac.in/esc101/current/Lectures/ESc101Lec25_26pointers.pdf
12th Jul 2021, 4:48 PM
Ipang
+ 1
Ipang ohh sure will check it 👍and yeah thanks a lot for helping me out !!
13th Jul 2021, 4:12 AM
Srinath