Basic C programming

This is a discussion on Basic C programming within the C Programming forums, part of the General Programming Boards category; Code: #include<stdio.h> int main () { printf(5+"My name is Dwe"); return 0; } output :- me is Dwe Please anyone ...

  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
    2,572
    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
    21,938
    Hint: if x is an array, then x[5] is equivalent to *(x + 5), which is equivalent to *(5 + x).
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    21,938
    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?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    21,938
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 12:00 PM
  2. Basic c programming help
    By benrogers in forum C Programming
    Replies: 12
    Last Post: 01-24-2011, 04: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, 08:54 PM
  5. Basic Programming
    By plexy1 in forum C Programming
    Replies: 2
    Last Post: 09-05-2001, 02:03 PM

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