CS19001/CS19002 Programming and Data Structures Laboratory | Autumn/Spring semester |
Frequently Asked Questions
My program works in my home PC, but not here.- You must be using a non-standard compiler in your home. The most likely culprit is the Turbo C compiler. You may, for example, get the following error message on a program compiled under Turbo C.
myprog.c:1:19: conio.h: No such file or directoryThe standard header file for ANSI C is <stdio.h>, not <conio.h>. Use Microsoft's Visual C++ compiler or the Windows gcc/gpp compiler. Download gcc compilers from here.
My program works in the lab, but not in the home.- Again the reason is most likely the use of a nonstandard compiler. Use VC++ or gcc.
I have included <math.h>, but am still getting the error:
/home/a53/tmp/ccgOZE1o.o(.text+0x30): In function `main': : undefined reference to `sqrt' collect2: ld returned 1 exit status- Compile with the -lm flag:
cc myprog.c -lm
I have defined the function myfunction, but still getting the error message:
/home/a53/tmp/ccE8gEBo.o(.text+0x45): In function `main': : undefined reference to `myfuction' collect2: ld returned 1 exit status- Well, you have defined myfunction, but not myfuction. The converse mistake, i.e., defining myfuction but calling myfunction, has a similar effect, but may be a bit more difficult to locate. Check for typos.
What is segmentation fault?- Segmentation faults are caused by unauthorized access of memory. For you, the most likely cause is that you forgot the ampersand (&) in your scanf statement.
If all your scanf's are okay and you are playing with arrays or dynamic memory, you need to bother about segmentation faults. Whenever you try to access (read or write) memory locations not allotted to you, this fault occurs. It is then necessary to debug your code and locate the exact place where this illegal memory access occurs. The process is not always easy. C programmers learn to live gracefully with segmentation faults. You too should!
My book says it is not always necessary to include <stdio.h>.- That is true. But it may be too difficult for you to determine when you program requires <stdio.h>. Many funny errors (compilation or run-time) may be caused by the fact that you did not include this header file. We advocate you to make it a habit to include <stdio.h> in any C program you write. Doing that brings you absolutely no harm, but saves many unwelcome hassles.
Can main() return void?- No, this is not allowed in the ANSI C standard. See this site.
My program is not printing my diagnostic messages- I/O is buffered in your systems. Until the output buffer is full, you do not see your diagnostic messages. You may force the buffer to be output by the call:
fflush(stdout);In most of the lab machines, a new-line character also flushes the buffer. So you should put a new-line character after every line of diagnostic nessage if you want to see it immediately.
Is it necessary to code the algorithm specified in the assignment?- Yes, it is! The PDS course and lab teach you C syntax, and how you can convert a specified algorithm in the C language. Teaching you to design algorithms is not the goal of the course/lab.