1 / 12

CS1010: Programming Methodology comp.nus.sg/~cs1010/

CS1010: Programming Methodology http://www.comp.nus.edu.sg/~cs1010/. Aaron Tan. Week 3: Miscellany (1/2). You are urged to explore on your own, as it is impossible for us to cover every detail in class.

theola
Download Presentation

CS1010: Programming Methodology comp.nus.sg/~cs1010/

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS1010: Programming Methodologyhttp://www.comp.nus.edu.sg/~cs1010/ Aaron Tan

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

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

  4. 1. return statement in main( ) • Q: Why do we write return 0; in our main() function? • Answer: • Our main() function has this header • intmain(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). This is called the status code of the program. • In UNIX, when a program terminates with a status codeof 0, it means that it is a successful run. • You may optionally check the status code to determine the appropriate follow-up action. In UNIX, this is done by typing echo $?immediately after you have run your program. – You do not need to worry about this. CS1010 (AY2013/4 Semester 1) Week3 Miscellany - 4

  5. 2. Default return type (1/3) • A ‘type-less’ function has default return type of int 1 #include <stdio.h> 2 3 int main(void) { 4 printf("%d\n", f(100, 7)); 5return 0; 6 } 7 8 f(int a, int b) { 9return a/b; 10 } • Program can be compiled, 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 - 5

  6. 2. Default return type (2/3) • Another example Without function prototype, compiler assumes function f to be an int function when it encounters this. 1 #include <stdio.h> 2 3 int main(void) { 4 f(100, 7)); 5return 0; 6 } 7 8 void f(int a, int b) { 9printf("%d\n", a/b); 10 } However, f is defined as a void function here, so it conflicts with above. • Program can be compiled, 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 - 6

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

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

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

  10. 5. CodeCrunch: Program failing all test cases • Q: I tested my program and it works well, but when I submit it to CodeCrunch, it fails all the test cases! Why? • This is a very commonly encountered problem once students start to submit their programs to CodeCrunch. • A very likely reason is that you have forgotten to initialise some variable properly. Remember that an uninitialised numeric variable may not contain zero. • 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. Don’t do this! CS1010 (AY2013/4 Semester 1) Week3 Miscellany - 10

  11. 6. Filenames to avoid in naming your executable files • Q: I compiled my C program into an executable file called test, but I can’t run it. 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, prefix it with ./, i.e., type this instead:./test • make is another commonly used name that should be avoided. • Avoid naming your executable files with names that are UNIX commands. (If your program can’t run, check that it doesn’t have the same name as a UNIX command.) CS1010 (AY2013/4 Semester 1) Week3 Miscellany - 11

  12. End of File

More Related