HOME       >>       Programming

Writing My First C Program


I have mentioned in another thread on this forum, that I have started learning C. (link : http://forums.xisto.com/topic/51673-learnin-python-where-to-start/
'>Learning Python -- where to start?
) Don't get confused about the link saying "Learning PYTHON". Actually the story is that my ultimate goal is to learn C++ but I wanted to start with something simpler and I had Python in my mind for this. @yordan told me that instead of python I should go for C (instead of python) as the starting language for C++. I bought his suggestion and now I have started learning C.

This thread is about a concept I am unable to act and it is not strictly about my first C program.

About the concept I am not getting...

My first C program looked something like this:

# include <stdio.h>int main(void){printf("to c or not to c, this is the question; \n");return 0;}

A fairly simple program... but I am not able to understand what exactly does the "return 0" does here.. I moved a step further in my second experiment and created a similar program but this time I didn't add the "return 0" line to it. And when I ran the program (of course after compiling.... ==> I am a noob) it returned the same result..

so what exactly is the role of this line in the program..


On which operating system did you run it?This a very common way of using hidden factors, named error codes.return 0 means error code zero, which is is the normal exit for a program.You could add other things more complicated, for insance the case where it's not good (return 1), there is no apple in my pocket (return 2).Afterwards, you query the return codes, if the return code is 2 this means that there is no apple in your pocket.On Unix, "echo $?" tells you the last error code, so after running your program "echo $?" should give "0".


The operating system is Lubuntu on a vmware and the compiler I am using is GCC.You explained it quite healthily but most of it got over my head. First of all, I don't understand why would a C program need and ERROR code to exit. Isn't their any better way. Secondly, if C program does need an error code to exit, why is that when the same error code is not supplied, the program behaves normally. I mean it should give some error etc to notify that an important component is missing.About the more advanced thing you mentioned @yordan... I really didn't understand it so can't say anything about that......


The C program does not need error handling. The "return" (or "exit" 0 in shell scripts) is absolutely not needed.Error handling is an extra, out of this program. Just in order to help you know if the program finished normally or if it crashed."exit 0" says "I am going to exit normally".Not necessary, but it's a way of writing programs, in order to clearly tell the people who will debug it later if needed (me or you next year) how the exit was.


Yeah... Now I got the concept. Thanks for that.So the line return 0 makes sure that the program finished normally and more importantly it provides means to check whether the program exited normally or not.I must tell you that there are more naive questions like this one are going to come... so I hope you are ready....


Yeah... Now I got the concept. Thanks for that.
So the line return 0 makes sure that the program finished normally and more importantly it provides means to check whether the program exited normally or not.

Yes, and, more important, these infos are transmitted out of the program and readable afterwards without the need of writing any method of communication between you and the computer.


Xisto.com offers Free Web Hosting to its Members for their participation in this Community. We moderate all content posted here but we cannot warrant full correctness of all content. While using this site, you agree to have read and accepted our terms of use, cookie and privacy policy. Copyright 2001-2019 by Xisto Corporation. All Rights Reserved.