Can you give examples of UBs in the 5 questions you mentioned? I dont
see any sir!. The site is quite well done.
Also, where do you work BTW?
I'm encouraged that you are reading the responses to your post and
responding, so this isn't just spam. Note however that newsgroups aren't a
"chat" medium, don't keep posting (essentially) the same thing repeatedly.
You can expect to wait for hours and perhaps even a day or 2 in some
circumstances for proper response.
You should learn to quote the proper context of what you are replying to,
as I have done here.
Also this is a newsgroup about discussing C, people are under no
obligation to describe their backgrounds.
Some errors I spotted on a quick scan through the questions:
Reversing linked list
Method 1
It is legitimate for a linked list to contain no elements, i.e.
the head pointer is itself null. The code fails in this case.
Method 2
The code returns 1 from main(). The only portable return values from
main() are 0, EXIT_SUCCESS and EXIT_FAILURE. Many implementations would
treat a 1 return as a failure indication. EXIT_SUCCESS and EXIT_FAILURE
are defined in <stdlib.h>
temp = (mynode *)malloc(sizeof(mynode));
Don't cast unless you really need to. Casting the return value of malloc()
is a classic error when it is used to shut the compiler up. The real error
is not having a proper declaration of malloc() is cope at the call. The
cast (usually) makes this compile silently but the error is still there
and the code is broken. The correct fix is to #include <stdlib.h> and
remove the superfluous cast. You can also use a better form for the
argument to sizeof:
temp = malloc(sizeof *temp);
Simpler, clearer and best of all correct.
The code fails to test the return value of malloc() for failure.
Always use proper prototype form in function declarations and definitions
e.g.
void print_list();
should be
void print_list(void);
This puts extra type checking requirements on the compiler.
C doesn't define any function callled getch().
sprintf(str, "[%d]->",(temp->value));
printf(str);
Why use sprintf() here at all, just use printf(). By doing it this way
you've created a dependency on a buffer size and you've introduced a
possible bug. Never use non-format data as a printf() format string, if it
contained % characters they will be interpreted as conversion specifiers.
That may not happen with the code you have but is BAD pactice and somebody
maintaining the code might easily add a % character. This is a
maintenance nightmare. Always output string data using %s e.g.
printf("%s", str);
There's undoubtedly more but this is enough for one post. I didn't pick a
bad question specifically, this is the first one I looked at. But it is a
very good example of how NOT to answer an interview question. A good
answer should demonstrate at every possible turn that you know the tricks
of the trade, the pitfalls and how to avoid them. Code that jumps in
making a whole host of basic mistakes is not a good sign.
Lawrence