when i am running the C prog after compile, it is not showing the result.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nandish ptl
    New Member
    • Dec 2010
    • 3

    when i am running the C prog after compile, it is not showing the result.

    #include <stdio.h>

    void main()
    {
    int a;
    printf("Enter value of a = ");
    scanf("%d\n",&a );
    a = a + 10;
    printf("The final value of a is %d",a);
    }

    when i am running this simple program it stops showing result. It executes only upto 'scanf'... After entering the 'a' value it is not showing the final result value.(i.e. 56, if a is entered 46) and i have to stop it forcefully from 'windows task manager'.

    why it does happen and what is the solution?

    Thank you.
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    It probably does show your result. However, since you have no code to pause the program after the printf, your result is displayed for only a split-second.

    Comment

    • donbock
      Recognized Expert Top Contributor
      • Mar 2008
      • 2427

      #3
      It is common for stdout to be a buffered output. This means that characters written to stdout are held in an internal buffer until either a newline is written to stdout or fflush is called.

      Terminate your format string with a newline.

      Comment

      • nandish ptl
        New Member
        • Dec 2010
        • 3

        #4
        how can i pause the program to display result..?

        i tried with different simple codes but the problem remains same. For exmple
        i wrote the following code...

        #include <stdio.h>

        void main()
        {
        int i,j;
        for(i=1;i<=5;i+ +)
        {
        for(j=1;j<=i;j+ +)
        {
        printf("%d",i);
        }
        printf("\n");
        }
        }

        when i am running the code black screen appear for a moment and angain main (blue) screen appears..

        what is the solution?

        Thank you

        Comment

        • nandish ptl
          New Member
          • Dec 2010
          • 3

          #5
          i terminated the format string with new line.. but it didnt work.

          Comment

          • AdrianH
            Recognized Expert Top Contributor
            • Feb 2007
            • 1251

            #6
            the simplest way is:

            Code:
            printf("Press enter to continue...");
            fflush(stdout);
            scanf("%*s");

            Comment

            Working...