Thread: reason for output in C

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    4

    reason for output in C

    Hello guyz

    here is a sample code in C with output but i coudnt undrstnd the reason for output values...... please explain it if any1 knw....


    main()
    {
    printf("%d %d %d %d");
    }

    o/p-- 0 344 0 0

  2. #2
    Banned ಠ_ಠ's Avatar
    Join Date
    Mar 2009
    Posts
    687
    printf doesn't actually know how many parameters you give it, it examines the string and assumes you provided that many variables, because you did not, it prints out garbage leftover in memory from another program
    ╔╗╔══╦╗
    ║║║╔╗║║
    ║╚╣╚╝║╚╗
    ╚═╩══╩═╝

  3. #3
    Registered User
    Join Date
    Aug 2010
    Location
    Rochester, NY
    Posts
    196
    Quote Originally Posted by ಠ_ಠ View Post
    printf doesn't actually know how many parameters you give it, it examines the string and assumes you provided that many variables, because you did not, it prints out garbage leftover in memory from another program
    Yeah.

    You would want to use it like:
    Code:
    printf("%d %d %d %d", 10, 20, 30, 40);
    Printing:  10 20 30 40
    or
    
    printf("Number %d has a name of %s and an age of %d.", 375, "Sam", 17);
    Printing:  Number 375 has a name of Sam and an age of 17.
    Note: Most strings are either prefixed or sufixed with a new line characters ("\n") - so you would have something like:
    Code:
    printf("Hello world\n");
    If you don't, it'll all print on the same line, soft wrapping at the end of the terminal.

  4. #4
    Registered User
    Join Date
    Oct 2010
    Posts
    4
    synadacate

    i understood wt u said.... n i knw it..... bt here the prob is little bit different.....

    Originally Posted by ಠ_ಠ
    printf doesn't actually know how many parameters you give it, it examines the string and assumes you provided that many variables, because you did not, it prints out garbage leftover in memory from another program

    these are not the garbage values.... coz on every computer the values are same.

  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
    > these are not the garbage values.... coz on every computer the values are same.
    YouŽre confusing repeatability with being correct.

    The local memory layout of the stack frame for any given run of your program generated by a single compiler will likely be constant. The fact that it seems the same doesnŽt make it any less uninitialised as far as youŽre concerned.

    Try it with a different compiler, youŽll get different answers (ALL of them wrong).
    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
    Oct 2010
    Posts
    4
    salem

    thnx..... ya iknw... on diff. compilers the result wl b diff.

    r u sure dat da same compiler cn give same rslt on evry computer..... n da rslt is nt machine dependent?

  7. #7
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by deepak1906 View Post
    synadacate
    these are not the garbage values.... coz on every computer the values are same.
    There are garbage; most likely from your own program.
    If you used a different Compiler they might change or a different OS.

    I would have guess all zeros as the most likely values to happen for an compiler not displaying garbage values. One of yours was not zero.

    Tim S.

  8. #8
    Registered User
    Join Date
    Oct 2010
    Posts
    4
    stahta01


    yeah.... m jst thinking.....y the second value is not 0?

  9. #9
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    You know what else is garbage? The crappy SMS-speak you're spewing through your keyboard. SPEAK PROPER ENGLISH!

  10. #10
    Third Eye Babkockdood's Avatar
    Join Date
    Apr 2010
    Posts
    352
    You haven't specified any values to print, so printf just made up some.

  11. #11
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    Quote Originally Posted by rags_to_riches View Post
    You know what else is garbage? The crappy SMS-speak you're spewing through your keyboard. SPEAK PROPER ENGLISH!
    I agree. I could not have said it better myself.

  12. #12
    Banned ಠ_ಠ's Avatar
    Join Date
    Mar 2009
    Posts
    687
    Quote Originally Posted by rags_to_riches View Post
    You know what else is garbage? The crappy SMS-speak you're spewing through your keyboard. TYPE PROPER ENGLISH!
    fixed
    ╔╗╔══╦╗
    ║║║╔╗║║
    ║╚╣╚╝║╚╗
    ╚═╩══╩═╝

  13. #13
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    reason for output in C
    This HAS been tried. Inspired by the monkey with his mouth covered in a mythological tale.

    But it failed, badly - I think it was the marketing, that killed it. Who's going to buy a "C-Dumb" compiler?

    You see the problem? Unless you're the guy with his eye's covered.

    <ROFL>
    Last edited by Adak; 10-07-2010 at 05:58 AM.

  14. #14
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well after the latest keyboard dribble, I lost interest.
    How To Ask Questions The Smart Way
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Give me a reason!
    By julia in forum C Programming
    Replies: 4
    Last Post: 04-28-2002, 01:46 AM
  2. Float to Int
    By pdstatha in forum C Programming
    Replies: 3
    Last Post: 03-30-2002, 08:30 AM
  3. For some reason, it prints funny characters ???
    By Nutshell in forum C Programming
    Replies: 8
    Last Post: 01-14-2002, 04:27 PM
  4. How can you reason with this madman?
    By EvenFlow in forum A Brief History of Cprogramming.com
    Replies: 25
    Last Post: 10-11-2001, 06:51 PM