How will you print any character, string or value of a variable without library functions in C?
Asked Answered
M

4

9

If for example I should not use standard library functions like printf(), putchar() then how can I print a character to the screen?

Is there an easy way of doing it. I dont know much about system calls and if I have to use them then how do I use them? So can any one advice an easy way of printing without using library functions?

Mortise answered 22/1, 2009 at 6:59 Comment(5)
Why can't you use the standard library functions? They're "standard" for a reason, no?Souza
Why would you want to do that?Wilden
well I wanted try out that to use in some sort of parser If u know any way please advice......Mortise
Wow excellent work Hao you helped me a lot the first small program u proposed me is working well but u mistakenly written src instead of scr but ok i changed it now its working. so i am assuming that this B8000000L is the address from where our output character goes to screen is that correct ?Mortise
@Hao, If you have given this one as an answer instead of comment I would have accepted it as an answer.ok upto how many times we can increment that scr (B8000000L).I think there will be some limit right. please let me know about any resource to know more about this addresses.Any way gr8 job.ThanksMortise
M
4

Well thank u all for ur answers.I found one simple answer by a comment from Mr. Hao below the question. his answer is simple program like this

Turbo C(DOS program):

char far* src = (char far*) 0xB8000000L; 
*src = 'M'; 
src += 2; 
*src = 'D'; 

or try this: http://en.wikipedia.org/wiki/Brainfuck :) – //Hao (an hour ago)

I tried it on Turbo C and its working. I wanted a simple solution like this and I wanted to accept it as correct answer but he(Hao) gave it as a comment so I pasted it here for other users to know about this on behalf of him and accepted it. Once again thank u Mr.Hao.

Mortise answered 22/1, 2009 at 7:0 Comment(2)
@Manoj Doubts: How does this program work? Specifically, what is 0xB8000000L and how does one arrive at it?Monopoly
@Lazer: the original IBM PC had that address hard-coded for the display buffer. Apparently Windows supports it for backwards compatibility.Rotterdam
A
17

In standard C, you can't. The only I/O defined in C is through the C standard library functions.

On a given platform, there may be ways to do it:

  • Make kernel calls directly. You will probably need to write some inline assembly to do this. You could make litb's write call directly, without using your C library. Grab the source of your C library to see how it's done.
  • Write directly to the frame buffer. Multi-user OS's often disallow this (at least without making any library/kernel calls).

Unless you're writing your own C library, I'm not sure why you'd want to do this.

Aphonic answered 22/1, 2009 at 7:15 Comment(1)
yeah thank u i wanted to do some thing similar to that thank u very much if u have any more resources for that please send me thank uMortise
P
8

In linux, you can use the write system-call:

write(1, "hello\n", 6); // write hello\n to stdout

If you can't get enough of it, you can go one step lower, invoking the syscall generically:

syscall(__NR_write, 1, "hello\n", 6);

It's worth knowing about strace, which you can use to see which syscalls are used by any particular program while it runs. But note that for "some simple parser", it's hardly needed to use raw system calls. Better use the functions of the c library.

By the way, lookout for WriteFile and GetStdHandle functions if you want to do the above in Windows without using the c standard library. Won't be as l33t as the linux solution though.

P answered 22/1, 2009 at 7:5 Comment(3)
Hate to to you, that's a library function. Most likely in your C library.Aphonic
ok then in windows if anybody knows how to do it please help. well for ur note this is not a homework question. I was trying to do it for some simple parserMortise
objdump -T /lib/libc.so.6 | grep syscall informs me that syscall is still a library function :-DAphonic
M
4

Well thank u all for ur answers.I found one simple answer by a comment from Mr. Hao below the question. his answer is simple program like this

Turbo C(DOS program):

char far* src = (char far*) 0xB8000000L; 
*src = 'M'; 
src += 2; 
*src = 'D'; 

or try this: http://en.wikipedia.org/wiki/Brainfuck :) – //Hao (an hour ago)

I tried it on Turbo C and its working. I wanted a simple solution like this and I wanted to accept it as correct answer but he(Hao) gave it as a comment so I pasted it here for other users to know about this on behalf of him and accepted it. Once again thank u Mr.Hao.

Mortise answered 22/1, 2009 at 7:0 Comment(2)
@Manoj Doubts: How does this program work? Specifically, what is 0xB8000000L and how does one arrive at it?Monopoly
@Lazer: the original IBM PC had that address hard-coded for the display buffer. Apparently Windows supports it for backwards compatibility.Rotterdam
L
2

The C Standard Library (see https://en.wikipedia.org/wiki/C_standard_library ) has a set of input and output routines that are used to read from and write to various input and output resources. The C Standard Library is ordinarily built upon the system services and system calls provided by the target operating system though in the case of embedded microcontrollers that do not have an operating system a different approach is used.

In addition to the C Standard Library there are other libraries often used such as the POSIX library (see https://en.wikipedia.org/wiki/C_POSIX_library ).

I use the term resources because the target for an input request or an output request may be a large variety of different things.

  • a file on a disk

  • a network connection

  • a console device

  • a serial communications port

  • a sensor

  • a printer device

  • a window on a screen

The model adopted by the C Standard Library is similar to that of the Unix operating system in that all of the various kinds of resources are treated as a device from which you read a stream of characters or bytes and you write a stream of characters or bytes.

In some cases this is abstraction allows you to ignore the details of physical connectivity of the resource. So you can open a file on a SATA hard disk, or a USB thumb drive, or a network server. You can print to a printer whether the printer is on a serial communications port, or a USB port, or a network printer.

For some types of resources such as a console device or a printer which has rows and columns and a cursor that can be moved, you would ordinarily provide the cursor positioning information within the characters you write to the device. So you would output both the text you want to print on the console as well as escape codes that would position the cursor where you want to write or some other action such as highlighting text.

One such console device was the VT100 terminal from DEC (see https://en.wikipedia.org/wiki/VT100 ) which used special escape codes in the output to do things like position the cursor on the screen. This was so handy that console windows on Unix, Linux, and Windows use similar escape codes to do similar actions on console or command windows (see How to make win32 console recognize ANSI/VT100 escape sequences?).

The C Standard Library provides a standard interface to a wide variety of devices and it is up to the people who wrote the library to provide the underlying functionality that the library needs to actually do reading and writing.

However the Standard Library does have the limitation of streams of characters as part of the model that it uses or presents to the programmer. That is why there are other libraries that are not part of the C Standard Library for graphical user interfaces and games such as from Microsoft, Qt, and various other groups such as OpenGL (see https://en.wikipedia.org/wiki/OpenGL , https://en.wikipedia.org/wiki/DirectX , https://en.wikipedia.org/wiki/Graphical_user_interface , and What is Linux’s native GUI API? ). See as well How are GUI's really made? and How does an operating system draw windows on the screen?

If you want to bypass the C Standard Library and use the operating system I/O system calls or services directly, you can do so. However unlike the C Standard Library which is standard across compilers that meet the C Standard requirements, the operating system I/O services will vary from operating system to operating system. And some operating systems may have several ways to do the same kind of I/O. For instance Windows has a number of different ways to perform file I/O (see https://en.wikipedia.org/wiki/Windows_API ).

In some cases you can bypass the operating system and talk directly to the device or resource. This is often done with embedded applications and in many cases with embedded applications on some microcontrollers, there is no real operating system available and you must interact directly with the device.

Embedded applications on microcontrollers often use specific memory addresses to interact with sensors in order to prepare the sensor for sensing as well as to read data such as temperature from the sensor. Sensors have a number of different connectivity methods which requires the programmer to know something about the electrical setup and the protocol to be used to talk with the sensor such as I2C (see https://en.wikipedia.org/wiki/I²C ), 1-wire (see https://en.wikipedia.org/wiki/1-Wire ) and others.

Under the old MSDOS operating system you could call BIOS routines to directly interact with devices including the screen. Or if you knew the address of the screen buffers and the proper format for the memory area, you could modify what was displayed on the screen by writing directly into memory areas where the screen frame buffers were located.

Modern multi-threading and multi-processing operating systems have sophisticated functionality within their system calls to handle the kinds of synchronization and coordination problems that can arise with multiple applications all trying to use the same devices and resources. Since the C Standard Library is built on top of these operating system services, when you use the C Standard Library you get that sophisticated functionality such as file locking. And using operating system services through the Standard Library you get the expertise and knowledge of experts who have done the work that you can just use.

Modern operating systems provide a rich variety of services such as file I/O, network I/O, printing services, etc. The C Standard Library along with other libraries from other groups such as POSIX provide a standard, portable way to access those services.

And when the C Standard Library and the POSIX library are ported to microcontrollers, you have a way of accessing this same functionality without having the huge learning curve of having to write your own library.

Laundes answered 29/6, 2020 at 2:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.