Thread: Tricky C question asked in an interiew - display name?

  1. #1
    Registered User
    Join Date
    Jul 2012
    Posts
    10

    Tricky C question asked in an interiew - display name?

    How to display name on screen with a C program without using printf,puts,putch,cprintf,sprintf ?

    Any suggestion/solution ?

    - Jenny

  2. #2
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    You can use fwrite, to print to the screen if you use stdout as file descriptor.

  3. #3
    Registered User
    Join Date
    Jul 2012
    Posts
    10
    oops, forgot to mention earlier, cannot use fwrite as well.

  4. #4
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Can you use putc?

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > oops, forgot to mention earlier, cannot use fwrite as well.
    Anything else you'd care to mention?

    If we can't use the standard functions, then by definition the answer must use a non-standard approach, and therefore knowledge of the compiler/OS/machine is VITAL to answering the question.

    Like for example that the OS is DOS, and you can go poking around in video memory directly.

    Or that the display device is a 20x2 LCD display on a vending machine.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Consider how you would go about implementing printf() if you were a compiler vendor. The set of standard I/O functions cannot be implemented purely in terms of other standard functions: eventually, within the bowels of printf() - or one of the standard functions it calls, if any - there will be instructions or function calls that get funky with your host operating system or hardware.

    Generally, if you can't use standard functions, it is necessary to find non-standard functions. These non-standard functions may be a library supplied with the operating system (for example, the win32 API) or it might be some library written in another language. Or both.

    Also consider *cough* inline assembler.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  7. #7
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Can you use vfprintf to the stdout stream?

  8. #8
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    Can you use write()/send() to STDOUT_FILENO?

  9. #9
    Registered User
    Join Date
    Mar 2012
    Location
    the c - side
    Posts
    373
    What about a sneaky

    Code:
    fprintf(stderr,"Colonel Bogey");

  10. #10
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Quote Originally Posted by gemera View Post
    What about a sneaky

    Code:
    fprintf(stderr,"Colonel Bogey");
    If your interviewer doesn't think that's a bad answer then there is a problem with the interview.

  11. #11
    Registered User
    Join Date
    Mar 2010
    Posts
    583
    That's a good interview question -- probably asked to see what sort of things you come up with and how you think, rather than looking for a specific answer. Possibly also to see whether you'd try to answer it without asking for more context and clarification.

    You could "display name on screen" in all sorts of ways. For example, find a C GUI library and pop up a message box with it in. Get a graphics library and show it in technicolour.

    If the question is narrowed to printing the name to stdout in the way printf would... I think Salem and grumpy have nailed it with their posts - use a non-standard function. Most C libraries will implement file I/O in terms of some low level I/O functions that you could call directly. In turn these will likely be implemented by calls to OS specific functions, which you could also call directly.

    If the question is narrowed further to not being allowed to use any libraries then the answer is even more dependent on the system and context. Under an OS you'd probably have to write some assembly to make a system call (e.g. Hello, world!). I can't think why you'd do that other than for the sake of learning.
    If you had direct access to the display hardware (e.g. no OS, or writing a device driver) then you'd do whatever the documentation for the hardware said you should, having first done all the stuff necessary to enable and set up the device. E.g. for a memory mapped UART you might write a character at a time to a particular address, for an LCD you'd convert characters into pixel representations then shove it all in a buffer somewhere.

    I'd have thought that that'd be a bad answer if you were on an OS, but:

    Quote Originally Posted by Salem
    Like for example that the OS is DOS, and you can go poking around in video memory directly.
    Ew!

    Vying for "worst suggestion so far": if you're on an OS that has the 'echo' command (echo (command) - Wikipedia, the free encyclopedia) you could:
    Code:
    system("echo name");

  12. #12
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    This says nothing about it having to be in a console window, nor does it mention a specific platform. I would therefore choose and list assumptions that suit me.
    A Win32 app with calls to DrawText inside the WM_PAINT handler would do it.
    If that's not acceptable, then they need to fix the question.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  13. #13
    Registered User
    Join Date
    Dec 2011
    Posts
    69
    Can you use inline assembly? Or a simple messagebox.

  14. #14
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    How to display name on screen with a C program without using the standard library IO functions?
    O_o

    Who gives a ........?

    That is a really bad interview question.

    Every answer to this question will resolve to either a request for more information or a statement about using a different library facility.

    If an interviewee can't read documentation/requirements the ability to resolve a "trick question" is meaningless.

    Soma

  15. #15
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    That is the point of such questions, Soma. Would you want a candidate who would not bother to question or clarify these requirements?

    I can tell you, having interviewed a lot of people, that a significant proportion of candidates would not.

    Interviews are not only about what candidates know. They are also about how they approach a new problem that is outside their experience, how they handle deficient or ambiguous requirements. What the candidate does NOT do in response to such questions is just as important as what they do.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A tricky(maybe simple) question
    By wayne08 in forum C++ Programming
    Replies: 9
    Last Post: 04-20-2010, 09:35 AM
  2. Tricky C Question
    By ganesh bala in forum C Programming
    Replies: 11
    Last Post: 01-28-2009, 12:58 PM
  3. prolog question; Can they be asked?
    By Unregistered in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 02-28-2002, 12:09 AM
  4. Replies: 1
    Last Post: 11-01-2001, 05:59 PM
  5. A question asked a million times, but where do I start?
    By Unregistered in forum Game Programming
    Replies: 10
    Last Post: 09-09-2001, 09:15 AM