Compiles good but fails in execution

This is a discussion on Compiles good but fails in execution within the C Programming forums, part of the General Programming Boards category; HI, I am sort of newbie so I cant figure out whats going wrong in this code. Objective : To ...

  1. #1
    Registered User
    Join Date
    Jan 2010
    Posts
    4

    Smile Compiles good but fails in execution

    HI,

    I am sort of newbie so I cant figure out whats going wrong in this code.

    Objective: To print a square box made out of 'S' i.e. S's are printed as outline of the square.

    the Square has arm length of 5 and the code has been compiled using Devcpp IDE 4.9.9.2 .

    It says no error while compiling but on running this it says .exe has stopped working.Same fate it met on turbo C also. There it says abnormal termination of application.


    Here is the code:

    Code:
    #include<stdio.h>
    
    main()
    
    {
          
          int i, j, x;
          
          x = 'S';
          
          for (i=0; i<5; i++) {
                                        
                                         for(j=0; j<5; j++)  {
       
                                                                        if (i == 0 || i == 4)
                                                                              { 
                                                                                  printf("%s", x); 
                                                                              }
    
                                                                           if (i == 1|| i == 2|| i == 3)
                                                                              { 
                                                                                  if ( j == 0 || j == 4)
                                                                                     { 
                                                                                         printf("%s", x);
                                                                                     } else { 
                                                                                                  printf("\0"); 
                                                                                                }
                                                                               
                                                                              }
    
                                                                           if ( j == 4 && i !=4 )
                                                                              { 
                                                                                  printf("\n"); 
                                                                              }
    
                                                                   }
    
                                       }
    
          return(0);
    
          }

    Also tell the proper position to use clrscr() and getch() in this code.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
       printf("%s", x);
    That's your problem.

    %s is for strings.
    %d is for integers.
    %c is for characters.

    You have x as an integer, but assign it a character (perfectly legal by the way), but you try to print it as a string.

    Try either %c or something like putchar instead.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Jan 2010
    Posts
    4
    Quote Originally Posted by quzah View Post
    Code:
       printf("%s", x);
    That's your problem.

    %s is for strings.
    %d is for integers.
    %c is for characters.

    You have x as an integer, but assign it a character (perfectly legal by the way), but you try to print it as a string.

    Try either %c or something like putchar instead.


    Quzah.
    Thanks Quzah!

    Your help was invaluable but man be easy with that sucking and **........ like things.PLz remove them coz they dont make ur signature or whatever you call that pretty

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. What good are typelists?
    By Elysia in forum C++ Programming
    Replies: 6
    Last Post: 07-18-2008, 06:34 PM
  2. Do you have to be good at math to be good at C/C++?
    By orion- in forum A Brief History of Cprogramming.com
    Replies: 14
    Last Post: 01-07-2007, 05:15 AM
  3. In a game Engine...
    By Shamino in forum Game Programming
    Replies: 28
    Last Post: 02-19-2006, 11:30 AM
  4. Good books for learning WIN32 API
    By Junior89 in forum Windows Programming
    Replies: 6
    Last Post: 01-05-2006, 05:38 PM
  5. Good resources for maths and electronics
    By nickname_changed in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 12-22-2004, 04:23 PM

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