pgm without std library functions

A

Anjana

hi,
can anyone illustrate a program to read and print a number without
using standard library functions??
waiting for reply....
 
V

Vladimir S. Oka

Anjana opined:
hi,
can anyone illustrate a program to read and print a number without
using standard library functions??
waiting for reply....

Your wait is now over! Here's your code:

/* Not possible in standard C. Sorry. */

--
"We all know Linux is great...it does infinite loops in 5 seconds."
(Linus Torvalds about the superiority of Linux on the Amsterdam
Linux Symposium)

<http://clc-wiki.net/wiki/Introduction_to_comp.lang.c>
 
V

Vladimir S. Oka

Vladimir S. Oka opined:
Anjana opined:


Your wait is now over! Here's your code:

/* Not possible in standard C. Sorry. */

Correction: it actually is, but you have to tell us your exact
environment (both software and hardware). Unfortunately, that would
make the question off-topic in c.l.c, so you're much better off asking
where your environment is topical.

<OT>
If you're running DOS, and Borland C, have a look at <conio.h>. It's
perfectly non-standard.
</OT>
 
E

Eric Sosman

Vladimir S. Oka wrote On 04/07/06 13:31,:
Anjana opined:




Your wait is now over! Here's your code:

/* Not possible in standard C. Sorry. */

Don't give up so easily! Here, for example, is
a program that reads and prints the number forty-two
without using any standard library functions:

#error 42 bottles of beer on the wall

(All right, we may need some hand-waving and hair-
splitting and airy persiflage about exactly what a
"program" is, but processing the source code must
involve both reading and writing the 42. ;-)
 
P

pete

Anjana said:
hi,
can anyone illustrate a program to read and print a number without
using standard library functions??
waiting for reply....

/* BEGIN new.c */

#include <stdio.h>

int main(void)
{

#ifdef putchar
putchar('9');
putchar('\n');
#endif

return 0;
}

/* END new.c */
 
S

Skarmander

Eric said:
Vladimir S. Oka wrote On 04/07/06 13:31,:

Don't give up so easily! Here, for example, is
a program that reads and prints the number forty-two
without using any standard library functions:

#error 42 bottles of beer on the wall

(All right, we may need some hand-waving and hair-
splitting and airy persiflage about exactly what a
"program" is, but processing the source code must
involve both reading and writing the 42. ;-)
Well, now, not so fast. He specifically asked for a program that *printed*
the number 42. Obviously, a compiler is only required to "produce a
diagnostic message that includes the specified sequence of preprocessing
tokens", and a diagnostic message is a "message belonging to an
implementation-defined subset of the implementation's message output". Now
whether the "message output" is "produced" by a process we'd call "printing"
is anyone's guess... It seems perfectly acceptable for an environment to
release appropriately painted pigeons that somehow encode the diagnostic. I
guess this is a QoI issue...

S.
 
V

Vladimir S. Oka

Skarmander opined:
Well, now, not so fast. He specifically asked for a program that
*printed* the number 42. Obviously, a compiler is only required to
"produce a diagnostic message that includes the specified sequence of
preprocessing tokens", and a diagnostic message is a "message
belonging to an implementation-defined subset of the implementation's
message output". Now whether the "message output" is "produced" by a
process we'd call "printing" is anyone's guess... It seems perfectly
acceptable for an environment to release appropriately painted
pigeons that somehow encode the diagnostic. I guess this is a QoI
issue...

Good point!

OP, please provide a definition of "print"...
 
I

Ivanna Pee

Anjana said:
hi,
can anyone illustrate a program to read and print a number without
using standard library functions??
waiting for reply....

I got it all figgered out. Read a byte from some random memory
location, then Write directly to video RAM setting pixels that resemble
the number read.

Get to work.
 
P

pete

Richard said:
putchar(), even if implemented as a macro, is a Standard Library
function.

The requirement was not to *use* any standard
library functions, and this program, doesn't.

If there is no putchar macro, then there is no output.
The putchar function is not used.
 
P

Pedro Graca

Anjana said:
can anyone illustrate a program to read and print a number without
using standard library functions??

For suitable definitions of "read" and "print", this highly unportable,
untested program might fit the bill:


#define MK_FP(seg, off) (((seg) << 4) + off)
#define NUMLOCK 0x20
#define NUMLOCK_BIT 5
#define CAPSLOCK 0x40
#define CAPSLOCK_BIT 6
#define SCROLLLOCK 0x10
#define SCROLLLOCK_BIT 4
#define LOCKS_SEG 0x0040
#define LOCKS_OFF 0x0017
#define SCREEN_SEG 0xB800
#define SCREEN_OFF 0x0000

int main(void) {
int x;
{ /* read number */
unsigned char locks = *((unsigned char*)MK_FP(LOCKS_SEG, LOCKS_OFF));
x = (locks & SCROLLLOCK) >> SCROLLLOCK_BIT;
x += (locks & CAPSLOCK) >> CAPSLOCK_BIT;
x += (locks & NUMLOCK) >> NUMLOCK_BIT;
}
{ /* print number */
unsigned char * screen = (unsigned char *)MK_FP(SCREEN_SEG, SCREEN_OFF);
*screen = x + '0';
*(screen + 1) = 31; /* white on blue */
}
return 0;
}
 
R

Richard Bos

Vladimir S. Oka said:
Vladimir S. Oka opined:


Correction: it actually is, but you have to tell us your exact
environment (both software and hardware).

Correction on the correction: it _may_ be. Depending on the system.

Richard
 
W

Walter Roberson

Correction on the correction: it _may_ be. Depending on the system.

I don't know that I would consider relying upon implementation-defined
behaviour to fall within "standard C". For example, although C leaves
it as implementation defined as to what happens if you convert an
integer to a pointer and write to that address, would we really
consider a program that did that to be "standard C" ?
 
K

Keith Thompson

I don't know that I would consider relying upon implementation-defined
behaviour to fall within "standard C". For example, although C leaves
it as implementation defined as to what happens if you convert an
integer to a pointer and write to that address, would we really
consider a program that did that to be "standard C" ?

Yes, it would be standard C, just not portable C.

The concept of implementation-defined behavior is defined by the
standard, after all.
 
R

Robert Gamble

Anjana said:
hi,
can anyone illustrate a program to read and print a number without
using standard library functions??
waiting for reply....

Depending on your definition of read, this might suffice:

int main (void) {
int i = 0;
assert(i);
}

which will result the value 0 being "read" and at least two numbers
being "printed". assert is a macro and doesn't directly call any
standard library functions.

If your definition of read and print is more strict, the following will
work. It is of course not standard but has the benefit of being
portable to many real-world systems.

#include <unistd.h>
int main (void) {
char num;
read(STDIN_FILENO, &num, 1);
write(STDOUT_FILENO, &num, 1);
return 0;
}

Run the program and enter a one-digit number.

Robert Gamble
 
A

Anjana

I havent had a deep study in C....i have never came across the topics
like #include<unistd.h>...
Can anyone post a suitable site where i can have a deep study in C...or
can anyone suggest a book for it?
And thanks for the answers ....
 
S

S.Tobias

Robert Gamble said:
assert is a macro and doesn't directly call any
standard library functions.

How do you know that, and what's the difference directly or not?
 
C

CBFalconer

Anjana said:
I havent had a deep study in C....i have never came across the
topics like #include<unistd.h>...
Can anyone post a suitable site where i can have a deep study in
C...or can anyone suggest a book for it?

If you reply to an article, quote adequate context. If you are
trying to start a new thread, don't reply to an existing article.
For some clues as to how to do this on the foul and invidious
google interface to usenet, set my sig. below.

You can probably get most of what you want from the C-FAQ.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
Also see <http://www.safalra.com/special/googlegroupsreply/>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,906
Latest member
SkinfixSkintag

Latest Threads

Top