Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 720
Manoj Doubts Avatar asked Jan 22 '09 06:01

Manoj Doubts


3 Answers

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.

like image 199
derobert Avatar answered Oct 14 '22 20:10

derobert


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.

like image 38
Johannes Schaub - litb Avatar answered Oct 14 '22 19:10

Johannes Schaub - litb


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.

like image 40
2 revs Avatar answered Oct 14 '22 20:10

2 revs