LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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

Reply
 
Search this Thread
Old 08-27-2004, 03:28 PM   #1
vijeesh_ep
Member
 
Registered: Jul 2004
Location: India
Distribution: Redhat-8
Posts: 35

Rep: Reputation: 15
Question 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.
 
Old 08-27-2004, 04:04 PM   #2
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 58
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
 
Old 08-27-2004, 04:21 PM   #3
vijeesh_ep
Member
 
Registered: Jul 2004
Location: India
Distribution: Redhat-8
Posts: 35

Original Poster
Rep: Reputation: 15
I forgot to add.It clearly says that we can only use standard C libreries.
 
Old 08-27-2004, 04:28 PM   #4
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 58
My example does use only standard C libraries. The only function it calls is printf() and that's in the standard C library.
 
Old 08-27-2004, 04:34 PM   #5
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 58
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.
 
Old 08-27-2004, 04:38 PM   #6
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 58
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
}
 
Old 08-27-2004, 04:47 PM   #7
rjlee
Senior Member
 
Registered: Jul 2004
Distribution: Ubuntu 7.04
Posts: 1,990

Rep: Reputation: 67
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 05:40 PM.
 
Old 08-27-2004, 04:55 PM   #8
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 58
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 05:40 PM.
 
Old 08-27-2004, 05:40 PM   #9
rjlee
Senior Member
 
Registered: Jul 2004
Distribution: Ubuntu 7.04
Posts: 1,990

Rep: Reputation: 67
++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)
 
Old 08-27-2004, 05:47 PM   #10
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 58
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 05:53 PM.
 
Old 08-27-2004, 06:09 PM   #11
deiussum
Member
 
Registered: Aug 2003
Location: Santa Clara, CA
Distribution: Slackware
Posts: 895

Rep: Reputation: 32
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...
 
Old 08-27-2004, 06:15 PM   #12
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 58
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.
 
Old 08-27-2004, 06:21 PM   #13
rjlee
Senior Member
 
Registered: Jul 2004
Distribution: Ubuntu 7.04
Posts: 1,990

Rep: Reputation: 67
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 06:22 PM.
 
Old 08-27-2004, 06:32 PM   #14
deiussum
Member
 
Registered: Aug 2003
Location: Santa Clara, CA
Distribution: Slackware
Posts: 895

Rep: Reputation: 32
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 06:34 PM.
 
Old 08-27-2004, 06:37 PM   #15
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 58
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Printing numbers from a text file dynamically mrobertson Programming 1 06-28-2005 09:19 AM
printing line numbers? fisheromen1031 Programming 1 07-27-2004 03:19 PM
Request for best use of semicolon and fi in scripts aus9 Programming 5 05-13-2004 03:14 PM
Scribus Printing How to put 4 flyers on a page when printing jazzboy Linux - Software 0 05-02-2004 07:13 AM
Adding numbers, break on non-numbers... Cruger Programming 1 03-22-2004 10:18 AM


All times are GMT -5. The time now is 07:31 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration