Thread: A silly question (probably)

  1. #1
    Registered User
    Join Date
    Sep 2011
    Location
    Dongargarh, India
    Posts
    16

    Question A silly question (probably)

    can someone explain me the difference between the 2 statements...

    1. printf("%c",count);
    2. printf("%s",&count);

    count is an integer & both the statements do the same thing : print the character associated with the value of count.

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    The first one prints a single character of value count... the other produces a memory access violation.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    I'm seeing a lot of "what is the difference" posts at the moment.
    What is this - learn C through random "facts"?

    Endianness - Wikipedia, the free encyclopedia
    Now consider the various internal formats that an integer can be represented in, and the likely consequences of simply pointing at it and calling it a string.
    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.

  4. #4
    Registered User
    Join Date
    Sep 2011
    Location
    Dongargarh, India
    Posts
    16
    it doesn't produce memory address violation or any other error...

    I'm new to programming & I stumbled upon this & i couldn't find any explanations in my book or from my teacher so i posted here.
    Sorry to disappoint you Salem with a random question.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Tanu
    it doesn't produce memory address violation or any other error...
    You are treating the bytes of the integer as if it were a null terminated string, but there is no guarantee that there is a null byte in the bytes of the integer, hence you may end up accessing memory that does not belong to you.

    Also, the argument should be a char*, not a pointer to some other integer type. Although this might not result in an compile error, your compiler may warn you of it if you compile at a sufficiently high warning level.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Sep 2011
    Location
    Dongargarh, India
    Posts
    16
    let me give the details...

    I'm using the "Turbo C++" compiler.
    & i cam upon this when we were making a program that prints the alphabets A-Z & a-z...

    here's the code :
    Code:
    #include<stdio.h>
    #include<conio.h>
    void main()
    {
     int count=64;
    
     while(count<122)
     {
      count++;
      if(count>90 && count<97)
      continue;
      printf(" %c\t", count); or {printf(" %s\t". &count);
     }
    }
    both the statements work fine.
    what i don't understand is how are they different?

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    I would suggest you stumble towards some other learning resource, because you're not going to learn a lot from the one you're using at the moment.

    It is a mildly interesting effect, not deep and meaningful 'C' knowledge you can use elsewhere. If you presented this code in a work environment, you would get sacked (yes, it's that bad).

    Did you read the link on endianess I posted?
    If your integer (say 65, corresponding to 'A' in the ASCII character set) is encoded in two bytes as 0x41 0x00, then you "win" and you see what you see when you try to print it as a string.
    But on some other machine, it would be encoded as 0x00 0x41 and you "lose", nothing is printed.


    > I'm using the "Turbo C++" compiler.
    Yeah, we kinda knew that when we all saw your location.

    I would suggest you get familiar with some modern compilers. unless you want to be part of the 97% in my sig-link.
    Free Developer Tools - Visual Studio 2010 Express | Microsoft Visual Studio
    smorgasbordet - Pelles C
    Code::Blocks
    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.

  8. #8
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Try something like this:
    Code:
    int count = -1;
    
    printf("%s", &count);
    ( I wonder what will happen when a seg-fault happens inside an emulated 16-bit program ^_^ )

    EDIT: The test would be more successful if you make "count" global...
    Devoted my life to programming...

  9. #9
    Registered User
    Join Date
    Sep 2011
    Location
    Dongargarh, India
    Posts
    16

    Unhappy

    Quote Originally Posted by Salem View Post
    I would suggest you stumble towards some other learning resource, because you're not going to learn a lot from the one you're using at the moment.

    It is a mildly interesting effect, not deep and meaningful 'C' knowledge you can use elsewhere. If you presented this code in a work environment, you would get sacked (yes, it's that bad).

    Did you read the link on endianess I posted?
    If your integer (say 65, corresponding to 'A' in the ASCII character set) is encoded in two bytes as 0x41 0x00, then you "win" and you see what you see when you try to print it as a string.
    But on some other machine, it would be encoded as 0x00 0x41 and you "lose", nothing is printed.


    > I'm using the "Turbo C++" compiler.
    Yeah, we kinda knew that when we all saw your location.

    I would suggest you get familiar with some modern compilers. unless you want to be part of the 97% in my sig-link.
    Free Developer Tools - Visual Studio 2010 Express | Microsoft Visual Studio
    smorgasbordet - Pelles C
    Code::Blocks
    I'm not looking to learn allot of programming. I don't want deep & meaningful knowledge of C. I'm a student of Biology & in future I'm going to be studying Genetics or Medicine. But I have to clear this one paper in order to pass school. I read the wiki about "endianess" that you suggested & frankly i didn't understand anything.
    I agree that its a bad code & won't present it in a job interview.
    What code would you right to print the English alphabet in upper case followed by lower case?

    Quote Originally Posted by GReaper View Post
    Try something like this:
    Code:
    int count = -1;
    
    printf("%s", &count);
    ( I wonder what will happen when a seg-fault happens inside an emulated 16-bit program ^_^ )

    EDIT: The test would be more successful if you make "count" global...
    Its my first week doing c-programming, & I don't know what a seg-fault is.

    All you guys are doing is talking about some technical stuff that is far beyond the scope of my syllabus but no-one has actually addressed the presented problem.

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Tanu
    What code would you right to print the English alphabet in upper case followed by lower case?
    Well...
    Code:
    #include <stdio.h>
    
    int main(void)
    {
        puts("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz");
        return 0;
    }
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well I gave you a copy/paste answer, but apparently it didn't work.

    > What code would you right to print the English alphabet in upper case followed by lower case?
    How about you post your answer to this, rather than worrying about broken code and ideas you found randomly scattered on the web.
    If this is ALL you have to do, then just do that much, ask here if you get stuck, and stop wasting everyone's time (including yours) on trivia which you have no interest in.

    > All you guys are doing is talking about some technical stuff that is far beyond the scope of my syllabus
    Yes, so cut to the chase and post your actual attempt at your actual homework.
    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.

  12. #12
    Registered User
    Join Date
    Sep 2011
    Location
    Dongargarh, India
    Posts
    16
    the code i posted is the actual code i wrote in class...
    the objective of the class was to practice the use of "While loop" & to print the English alphabet.
    i noticed that i could do it by using both the statements:
    1. printf("%c",count);
    2. printf("%s",&count);
    so that got me curious.
    Maybe I'm in the wrong place & this forum is not for newcomers like me.

    @laserlight : you really think that is a better code to present in a work environment?

  13. #13
    Registered User
    Join Date
    Sep 2011
    Location
    Dongargarh, India
    Posts
    16
    from what i understand
    the second statement should not work as &count would represent the address of the variable count, & the ASCII character associated with it shouldn't be the same as the one associated with the value of count.
    but either statement does the job, why???

  14. #14
    Registered User
    Join Date
    Sep 2011
    Location
    Stockholm, Sweden
    Posts
    131
    Quote Originally Posted by Tanu View Post
    the code i posted is the actual code i wrote in class...
    the objective of the class was to practice the use of "While loop" & to print the English alphabet.
    i noticed that i could do it by using both the statements:
    1. printf("%c",count);
    2. printf("%s",&count);
    so that got me curious.
    Maybe I'm in the wrong place & this forum is not for newcomers like me.
    You have gotten very good explanations how those two statements differ in this thread. You complain about the answers being too technical, but that is simply because your question cannot really be explained in a simple way without having a deeper knowledge about how C and memory access/management works.

  15. #15
    Registered User
    Join Date
    Sep 2011
    Location
    Stockholm, Sweden
    Posts
    131
    Quote Originally Posted by Tanu View Post
    from what i understand
    the second statement should not work as &count would represent the address of the variable count, & the ASCII character associated with it shouldn't be the same as the one associated with the value of count.
    but either statement does the job, why???
    I suggest that you re-read answer #7 by Salem.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Silly Question
    By AdamOhio76 in forum C Programming
    Replies: 2
    Last Post: 05-08-2011, 01:53 PM
  2. Silly question but..
    By swgh in forum C++ Programming
    Replies: 3
    Last Post: 05-05-2008, 12:39 PM
  3. A Silly Question!
    By MK4554 in forum C++ Programming
    Replies: 2
    Last Post: 01-13-2006, 02:49 PM
  4. Really really silly question
    By caduardo21 in forum C Programming
    Replies: 6
    Last Post: 06-06-2005, 09:33 AM
  5. silly question??
    By swgh in forum Game Programming
    Replies: 4
    Last Post: 04-27-2005, 08:50 PM