C program

This is a discussion on C program within the C Programming forums, part of the General Programming Boards category; Code: need answ & expl of following question main() { printf("\nab"); printf("\bsi"); printf("\rha"); }...

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    32

    Lightbulb C program

    Code:
    need answ & expl of following question
    
    main()
    {
    printf("\nab");
    printf("\bsi");
    printf("\rha");
    }

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Google printf and read its documentation.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  3. #3
    Registered User
    Join Date
    Sep 2010
    Posts
    10
    hai

    would be the output!
    \n is newline-takes cursor to new/next line on console;
    \b is backspace-does the same work as the backspace key on keyboard;
    \r is carriage return-brings the cursor to beginning of current line on console;

    try interpreting the arguments provided to 'printf',u ll understand hw it prints 'hai'.

  4. #4
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,641
    Still teaching stupid programming tricks in Southern Asia, amirite?

  5. #5
    Registered User
    Join Date
    Sep 2010
    Posts
    10
    Has nothing to do with real time programming however if you execute this you still get an answer!

  6. #6
    Registered User
    Join Date
    Sep 2010
    Posts
    32
    Quote Originally Posted by avee137 View Post
    hai

    would be the output!
    \n is newline-takes cursor to new/next line on console;
    \b is backspace-does the same work as the backspace key on keyboard;
    \r is carriage return-brings the cursor to beginning of current line on console;

    try interpreting the arguments provided to 'printf',u ll understand hw it prints 'hai'.
    Hi,
    I run this program using eclipse, it throws the output

    absi
    ha

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,344
    Look carefully at the box character - see the numbers?

    Now go here
    Ascii Table - ASCII character codes and html, octal, hex and decimal chart conversion

    Then read your C book for what \b means.

    Do you see it now?

    Also, you would have gotten a much better answer if you'd posted both code AND output in the same post, instead of the terse "what's this code do" you started with.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 12:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 12:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 09:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21