code

This is a discussion on code within the C Programming forums, part of the General Programming Boards category; Code: #include<stdio.h> #include<conio.h> void main() { char *ptr="string"; clrscr(); printf("%c",*ptr++); printf("%c",*(++ptr)); printf("%c".(*ptr)++); printf("%c",++*ptr); getch(); } output: srrt please explain how ...

  1. #1
    Registered User
    Join Date
    Nov 2011
    Location
    chennai
    Posts
    1

    Post code

    Code:
    #include<stdio.h>
    #include<conio.h>
    void main()
    {
    char *ptr="string";
    clrscr();
    printf("%c",*ptr++);
    printf("%c",*(++ptr));
    printf("%c".(*ptr)++);
    printf("%c",++*ptr);
    getch();
    }
    
    
    output:
    srrt
    
    please explain how am i getting output as srrt
    urgent please response soon.
    explain the logic how am getting this output

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,776
    Concentrate on these two lines:
    Code:
    printf("%c",*ptr++);
    printf("%c",*(++ptr));
    In particular, recall how post-increment and pre-increment works.

    Ignore these two lines as they result in undefined behaviour since they modify what ptr points to, which is the contents of a string literal:
    Code:
    printf("%c".(*ptr)++);
    printf("%c",++*ptr);
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 03-10-2010, 10:28 AM
  2. Replies: 14
    Last Post: 04-01-2008, 02:23 AM
  3. producing c/c++ code from flowcharts,pseudo code , algorithims
    By rohit83.ken in forum C++ Programming
    Replies: 3
    Last Post: 02-20-2008, 06:09 AM
  4. Having trouble translating psudeo-code to real-code.
    By Lithorien in forum C++ Programming
    Replies: 13
    Last Post: 10-05-2004, 07:51 PM
  5. Replies: 0
    Last Post: 02-21-2002, 05:05 PM

Tags for this Thread


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