Programming This 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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
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.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
|
08-27-2004, 02:28 PM
|
#1
|
|
Member
Registered: Jul 2004
Location: India
Distribution: Redhat-8
Posts: 35
Rep:
|
printing numbers without using semicolon
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.
|
|
|
|
08-27-2004, 03:04 PM
|
#2
|
|
Senior Member
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246
Rep:
|
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:
Code:
somefile.h
-------------
#define LOOP { int i; for(i = 1;i <= 100;++i) printf("%d\n", i); } return 0;
Code:
somefile.c
-------------
#include <stdio.h>
#include "somefile.h"
int main(void)
{
LOOP
}
and viola...the .c file doesn't have any semicolons 
|
|
|
|
08-27-2004, 03:21 PM
|
#3
|
|
Member
Registered: Jul 2004
Location: India
Distribution: Redhat-8
Posts: 35
Original Poster
Rep:
|
I forgot to add.It clearly says that we can only use standard C libreries.
|
|
|
|
08-27-2004, 03:28 PM
|
#4
|
|
Senior Member
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246
Rep:
|
My example does use only standard C libraries. The only function it calls is printf() and that's in the standard C library.
|
|
|
|
08-27-2004, 03:34 PM
|
#5
|
|
Senior Member
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246
Rep:
|
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.
|
|
|
|
08-27-2004, 03:38 PM
|
#6
|
|
Senior Member
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246
Rep:
|
Here's an alternative code, but notice that you still need a semicolon in the #define:
Code:
#include <stdio.h>
#define S ;
int main(void)
{
int i S
for(i = 1 S i <= 100 S ++i)
printf("%d\n", i) S
return 0 S
}
|
|
|
|
08-27-2004, 03:47 PM
|
#7
|
|
Senior Member
Registered: Jul 2004
Distribution: Ubuntu 7.04
Posts: 1,990
Rep:
|
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?)
Code:
#include <stdio.h>
#include <errno.h>
void main() {
while (++errno <= 100)
printf("%d\n", errno);
}
If you find the right non-standard compiler, it might even let you get away with missing off the last semicolon.
Last edited by rjlee; 08-27-2004 at 04:40 PM.
|
|
|
|
08-27-2004, 03:55 PM
|
#8
|
|
Senior Member
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246
Rep:
|
errno isn't guranteed to start at 1 and very very probably won't. Also, you're only printing up to 10 instead of 100.
And any compiler that doesn't need a semicolon at the end of that should be burned and buried.
Last edited by itsme86; 08-27-2004 at 04:40 PM.
|
|
|
|
08-27-2004, 04:40 PM
|
#9
|
|
Senior Member
Registered: Jul 2004
Distribution: Ubuntu 7.04
Posts: 1,990
Rep:
|
++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
Meant to say 100, no 10! (Edited to correct)
|
|
|
|
08-27-2004, 04:47 PM
|
#10
|
|
Senior Member
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246
Rep:
|
Quote:
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.
Last edited by itsme86; 08-27-2004 at 04:53 PM.
|
|
|
|
08-27-2004, 05:09 PM
|
#11
|
|
Member
Registered: Aug 2003
Location: Santa Clara, CA
Distribution: Slackware
Posts: 895
Rep:
|
Using the errno idea I came up with this... I don't like using errno either, so I'll probably still think about an alternative:
Code:
#include <stdio.h>
#include <errno.h>
void main()
{
while(printf("%i\n", ++errno) < 4)
{
}
}
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...
|
|
|
|
08-27-2004, 05:15 PM
|
#12
|
|
Senior Member
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246
Rep:
|
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.
|
|
|
|
08-27-2004, 05:21 PM
|
#13
|
|
Senior Member
Registered: Jul 2004
Distribution: Ubuntu 7.04
Posts: 1,990
Rep:
|
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?
Last edited by rjlee; 08-27-2004 at 05:22 PM.
|
|
|
|
08-27-2004, 05:32 PM
|
#14
|
|
Member
Registered: Aug 2003
Location: Santa Clara, CA
Distribution: Slackware
Posts: 895
Rep:
|
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.
Last edited by deiussum; 08-27-2004 at 05:34 PM.
|
|
|
|
08-27-2004, 05:37 PM
|
#15
|
|
Senior Member
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246
Rep:
|
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.
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 06:41 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|