1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

UNKNOWN! runtime error

Discussion in 'C' started by lionaneesh, Mar 23, 2010.

  1. lionaneesh

    lionaneesh New Member

    Code:
    #include<stdio.h>
    
    void reverse(char str[]);
    
    int main()
    {
        char str[100];
        int i;
    
        printf("Enter to reverse : ");
        scanf("%s",str);
        reverse(str);
        return(0);
    }
    /* Function */
    
    void reverse(char str[])
    {
    
        int i;
        for(i=99;i>=0;i--)
        {
            printf("%c",str[i]);
        }
        putchar('\n');
    }
    
    
    
    :embarasse:embarasse:embarasse:embarasse:embarasse:embarasse:embarasse:embarasse:embarasse:embarasse:embarasse:embarasse\


    Why there is a runtime error in the above programme..........

    help!!!!!!
    URJENT!!!!!!!!
     
  2. virxen

    virxen New Member

    Code:
    #include<stdio.h>
    [COLOR=Red]#include <string.h>[/COLOR]
    
    void reverse(char str[]);
    
    int main()
    {
        char str[100];
        int i;
    
        printf("Enter to reverse : ");
        scanf("%s",str);
        reverse(str);
        return(0);
    }
    /* Function */
    
    void reverse(char str[])
    {
    
        int i;
        [COLOR=Red]int z=strlen(str);[/COLOR]
        for(i=[COLOR=Red]z-1[/COLOR];i>=0;i--)
        {
            printf("%c",str[i]);
        }
        putchar('\n');
    }
    
    
    the error is that you reserve 100 positions in memory for the string entered
    but it uses actually only those the user enter.

    so if i enter the word car-->'c' 'a' 'r' '\0' so it uses 4 positions instead of 100
    so instead of starting from the end of 100positions you must start from the end of 4positions down to 0
     
    shabbir likes this.
  3. xpi0t0s

    xpi0t0s Mentor

    That doesn't explain why there would be a runtime error. It would explain why garbage is displayed and if I modify the program to add
    Code:
    	for (i=0; i<100; i++)
    		str[i]='~';
    
    to main just after the int i declaration, the output is:
    Enter to reverse : hello
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ olleh

    but even without this modification I don't get a runtime error (in Visual Studio 2008 building the program in Debug mode).

    When it prompted you to enter a string, did you enter more than 100 characters? That might cause a runtime error - this is the infamous "buffer overflow" bug.

    If not then I can only suggest this is a compiler bug. Maybe the RTL has a problem with %c for non-printable characters. You can test this hypothesis with a simple program:
    Code:
    printf("Starting test\n");
    fflush(stdout);
    for (i=0; i<256; i++)
    {
      printf("%d %c ",i,i);
      fflush(stdout);
    }
    
    The fflush ensures that the last character before the runtime error is displayed; normally output is buffered which can cause confusion when a program crashes - it looks like it crashed before it actually went tits up.
     

Share This Page