ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
There is less than 12 hours left to vote in the 2015 LinuxQuestions.org Members Choice Awards. Click here to go to the polls. Vote now and make sure your voice is heard!
Introduction to Linux - A Hands on Guide
This guide was created as an overview of the Linux Operating System, geared toward new users as an exploration tour and getting started guide, with exercises at the end of each chapter.
For more advanced trainees it can be a desktop reference, and a collection of the base knowledge needed to proceed with system and network administration. This book contains many real life examples derived from the author's experience as a Linux system and network administrator, trainer and consultant. They hope these examples will help you to get a better understanding of the Linux system and that you feel encouraged to try out things on your own.
Click Here to receive this Complete Guide absolutely free.
I attended a job interview and there was a question to print numbers from 1to 100 with out using a semicoln( in the program.(in C) .How can we do that in C? If u know any similer questions plz mention them also.
Well, since the printing functions need stdio.h #include'd and stdio.h undoubtedly contains semicolons then it stands to reason that the interviewer doesn't count semicolons contained in header files, so you could do this:
++errno is the preincrementation operator, as different from errno++ which postincrements. That is to say it evaluates to the number after incrementation (so the program starts printing at 1 if errno starts at 0). I presumed that at the start of the program, no I/O errors would have occurred and errno would be 0.
I have tested it, and it definitely starts printing at 1 on GNU 3.3.3
The integer errno is set by system calls (and some library
functions) to indicate what went wrong. Its value is sig-
nificant only when the call returned an error (usually
-1), and a library function that does succeed is allowed
to change errno.
errno is defined by the ISO C standard to be a modifiable
lvalue of type int, and must not be explicitly declared;
errno may be a macro.
It's only starting at 1 for you because the memory area assigned to errno just happens to be 0. errno isn't guaranteed to start off at 0.
Also, according to the man page, printf() is free to change the value of errno if it succeeds which would most probably put you in an infinite loop.
The following sentence is deleted from the DESCRIPTION: "The value of errno is 0 at program start-up, but is never set to 0 by any XSI function". The DESCRIPTION also no longer states that conforming implementations may support the declaration:
extern int errno;
I therefore should choose IEEE 1003.1-2001, Open Group Specifications Issue 4 as my C standard. But I'm going to go back to before that, and choose old-fashioned POSIX C, which actually defined errno as an external variable, initialised to zero.
Also, this couldn't get into an infinite loop if the hardware is working. A standard function may never set the value of errno to 0; it is unmodified in the case of success. From the same page:
No function in this volume of IEEE Std 1003.1-2001 shall set errno to 0.
But you're right in that an application isn't supposed to look at errno's value except after a function call. Also, I'm not handling the case of a hardware fault — but neither have any other suggestions so far.
Btw, nice one on the while loop.
Edit: I thought defining main as return type void just made the program's exit value undefined?
Ok... here's a solution w/o looking at errno... interestingly, gcc doesn't warn about having a function with a return value of int that doesn't actually return anything. It does warn that main should return int, but it's just a warning...
int PrintNumber(int i)
if (printf("%i", i) < 3 && printf("\n") && PrintNumber(i+1))
Edit: Changed to int main, and the warning goes away. Assuming that if nothing is returned when a return type of int is specified, it returns 0, though I don't know what the spec says... in any case, my code doesn't rely on the return codes.
It's true that errno is never set to zero by any library function, but that doesn't mean it can't always be set to 1 by a library function that succeeds...or -1 even. Remember, this is also in the man page:
and a library function that does succeed is allowed to change errno.
So if every time through the loop printf() set errno to something like 1, you would be in an infinite loop.