ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
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.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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:
The bottom line is, it's impossible. By the C standard, main() must return a value and return requires a semicolon. Somewhere in the code, a semicolon would need to be present.
You can do it without #defines in only one semicolon, by abusing a standard global variable. I think main without an int is a historical C standard (possibly K&R?)
++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.
Note: I ran this under Windows using Cygwin, so the \n actually prints a \r\n. Under Linux it'd probably only print \n so that 4 would probably need to be a 3.
Also, although I don't completely like using errno, most C compilers will initialize global variables to 0, though local variables are still usually undefined...
Also realize that defining main() as returning type void has undefined results according to ANSI C which means according to the standard you'd need to return a value.
I hate to be pedantic but the question never specified which standard of C to use. There are several, and looking at them I noticed this in the change-log for IEEE standard version 1003.1-2001 (http://www.opengroup.org/onlinepubs/...ons/errno.html) for errno:
Quote:
Issue 5
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:
Quote:
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...
Code:
#include <stdio.h>
int PrintNumber(int i)
{
if (printf("%i", i) < 3 && printf("\n") && PrintNumber(i+1))
{
}
}
int main()
{
if (PrintNumber(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:
Quote:
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.