CS1010: Programming Methodology http://www.comp.nus.edu.

Aaron Tan

 Seniors are also welcome to post in the forums.   Often during exploration. instead of emailing us.Week 3: Miscellany (1/2)  You are urged to explore on your own. please use the forums to help yourself and your future juniors!  CS1010 (AY2013/4 Semester 1) Week3 Miscellany . as it is impossible for us to cover every detail in class. and in the past they did post useful materials for you. We encourage you to use the IVLE forums to post your queries.2 . So. you may encounter problems or have questions you need clarification on. so that anybody can help answer your queries and the answer can be read by all.

there is no clear distinction. Note that sometimes the discussion here may include materials that have not been covered yet. as it will strengthen your knowledge on the whole. even if it is outside the syllabus. or are outside the scope of CS1010.  Some of the topics here are collected from discussion in past years’ IVLE forum.3 . but are taken out of the lecture slides to keep the lecture focused.   Do not be too concerned about whether a topic is in or outside the syllabus. CS1010 (AY2013/4 Semester 1) Week3 Miscellany . Sometimes.Week 3: Miscellany (2/2)  Some of the topics here are related to this week’s lecture topics. It is always good to know that little extra.

– You do not need to worry about this. it means that it is a successful run. You may optionally check the status code to determine the appropriate follow-up action. return statement in main( )   Q: Why do we write return 0. in our main() function? Answer:    Our main() function has this header int main(void) Hence it must return an integer (to the operating system) The value 0 is chosen to be returned to the operating system (which is UNIX in our case).1. In UNIX.4   CS1010 (AY2013/4 Semester 1) . when a program terminates with a status code of 0. this is done by typing echo $? immediately after you have run your program. This is called the status code of the program. Week3 Miscellany . In UNIX.

}  Program can be compiled. f(100. but with warnings: warning: implicit declaration of function 'f'  line 4 (due to absence of function prototype) warning: return type defaults to 'int'  line 8 CS1010 (AY2013/4 Semester 1) Week3 Miscellany . 7)). } f(int a.5 . Default return type (1/3)  A ‘type-less’ function has default return type of int 1 2 3 4 5 6 7 8 9 10 #include <stdio. return 0.2.h> int main(void) { printf("%d\n". int b) { return a/b.

 Program can be compiled. } However. so it conflicts with above. Default return type (2/3)  Another example 1 2 3 4 5 6 7 8 9 10 #include <stdio. f is defined as a void function here. a/b).h> int main(void) { f(100.6 . return 0.2. void f(int a. but with warnings: warning: implicit declaration of function 'f'  line 4 (due to absence of function prototype) warning: conflicting types for 'f' CS1010 (AY2013/4 Semester 1) Week3 Miscellany . int b) { printf("%d\n". compiler assumes function f to be an int function when it encounters this. } Without function prototype. 7)).

int main(void) { printf("%d\n". int). f(100. Default return type (3/3)  Tips:   Provide function prototypes for all functions Explicitly specify the function return types for all functions 1 2 3 4 5 6 7 8 9 10 11 12 #include <stdio. 7)).h> int f(int. } CS1010 (AY2013/4 Semester 1) Week3 Miscellany . return 0.7 . } int f(int a.2. int b) { return a/b.

out Week3 Miscellany . you can type: a. you may also use output redirection > to redirect output to a text file instead of to the screen: a.out < set1.8 CS1010 (AY2013/4 Semester 1) .3.in > myset1.in into your directory. but CodeCrunch executes your program by redirecting the input data from a text file.  Assuming that you have copied the input text file set1. one at a time.out < set1.in  Likewise.  You can do this in UNIX using input redirection <. it can test you program by reading input data from different text files. CodeCrunch: How to make use of the input and output files?  Your program works on interactive inputs.  This way.

out with the correct output file set1. for example.out provided by CodeCrunch: diff myset1. This is handy in cases where the differences between your output and the model output are not visible to the eyes.out  If the two files are identical.out set1. trailing spaces in an output line.9 .4. CodeCrunch: using ‘diff’ to compare  You may then use the diff command in UNIX to compare your output file myset1. no output will be produced by the diff command.  CS1010 (AY2013/4 Semester 1) Week3 Miscellany .

Remember that an uninitialised numeric variable may not contain zero. it fails all the test cases! Why? This is a very commonly encountered problem once students start to submit their programs to CodeCrunch. but when I submit it to CodeCrunch.10 CS1010 (AY2013/4 Semester 1) . Correct your program and resubmit to CodeCrunch!   Some students just ignored CodeCrunch feedback and did nothing to correct their program when it fails all test cases. CodeCrunch: Program failing all test cases  Q: I tested my program and it works well. Don’t do this! Week3 Miscellany .5.   A very likely reason is that you have forgotten to initialise some variable properly.

Avoid naming your executable files with names that are UNIX commands./.) Week3 Miscellany .. (If your program can’t run.11   CS1010 (AY2013/4 Semester 1) . Filenames to avoid in naming your executable files    Q: I compiled my C program into an executable file called test.6. but I can’t run it./test make is another commonly used name that should be avoided. check that it doesn’t have the same name as a UNIX command. prefix it with . type this instead: . Why? The reason is that test is a UNIX built-in command. When you type test you are running this UNIX command instead of your executable program! Tips  To run the program. i.e.

End of File .

Sign up to vote on this title
UsefulNot useful