Thread: Basic C programming

  1. #1
    Registered User
    Join Date
    Jun 2012
    Posts
    21

    Basic C programming

    Code:
    #include<stdio.h>
    int main ()
    {
    printf(5+"My name is Dwe");
    return 0;
    
    }
    output :- me is Dwe

    Please anyone help me . Explain because printf function print the "" field string and it is eating 5 words of string

    thank you

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Printf format strings - Cprogramming.com

    Code:
    printf(5+"My name is Dwe");
    What is "5+" supposed to be doing?

  3. #3
    Registered User
    Join Date
    Jun 2012
    Posts
    21
    That's what I am asking that What it is doing :P . Please explain anyone

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Hint: if x is an array, then x[5] is equivalent to *(x + 5), which is equivalent to *(5 + x).
    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

  5. #5
    Registered User
    Join Date
    Jun 2012
    Posts
    21
    This question from pointers . ???

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Yes, it is. The array, in this case a string literal, is converted to a pointer to its first element. So, when you add 5 to a pointer, what happens?
    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

  7. #7
    Registered User
    Join Date
    Jun 2012
    Posts
    21
    Actually I am a beginner .I just started reading C . So i don't have any idea about pointers and arrays. I saw this problem and I get confused with printf function . Thanks for reply

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Okay. Then you can ignore this until later, or just accept that adding 5 skips the first 5 characters of the string to be printed.
    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

  9. #9
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    Okay, let's say that the string literal "My name is Dwe\0" is stored at the memory address 0x100. Printf, when using %s, will print characters starting at the location provided until it hits a zero. It would do this:

    0x100 | 'M'
    0x101 | 'y'
    .....
    0x10d | 'e'
    0x10e | '\0' <-- and it stops printing.

    However, adding five would make it start printing at 0x105:

    0x105 | 'm'
    0x106 | 'e'
    0x107 | ' '
    .....

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Basic C programming help
    By jonny0503 in forum C Programming
    Replies: 3
    Last Post: 01-13-2012, 01:00 PM
  2. Basic c programming help
    By benrogers in forum C Programming
    Replies: 12
    Last Post: 01-24-2011, 05:01 PM
  3. Very basic programming help please
    By xrated in forum C++ Programming
    Replies: 8
    Last Post: 08-14-2006, 03:02 PM
  4. basic C-programming Q
    By slyonedoofy in forum C Programming
    Replies: 2
    Last Post: 10-30-2002, 09:54 PM
  5. Basic Programming
    By plexy1 in forum C Programming
    Replies: 2
    Last Post: 09-05-2001, 02:03 PM