title: How to avoid "Segmentation Fault" in c program
body: <p>I have written C program for Case Register. My program is working good. I used <code>-Wall</code> for compile. It didn't show any Warning.But the <code>problem is</code>, if I want to run it with <code>bash loop</code> such as <code>.test.sh | ./caseRegister</code>, the output is like the below:</p>
  
  <pre><code>:&gt;&gt;:1498.00
  :&gt;&gt;:1499.00
  :&gt;&gt;:1500.00
  :&gt;&gt;:1501.00
  :&gt;&gt;:1502.00
  :&gt;&gt;:1503.00
  :&gt;&gt;:1504.00
  :&gt;&gt;:1505.00
  :&gt;&gt;:1506.00
  :&gt;&gt;:1507.00
  :&gt;&gt;:1508.00
  :&gt;&gt;:1509.00
  :&gt;&gt;:1510.00
  :&gt;&gt;:1511.00
  :&gt;&gt;:1512.00
  :&gt;&gt;:1513.00
  :&gt;&gt;:1514.00
  :&gt;&gt;:1515.00
  :&gt;&gt;:1516.00
  :&gt;&gt;:1517.00
  :&gt;&gt;:1518.00
  Segmentation fault
  </code></pre>
  
  <p>That test.sh is:</p>
  
  <pre><code>#/bin/sh
  
  i=1
  run=1
  for ((; ; ))
  do
  echo $i
  #echo $((i++))
  done
  </code></pre>
  
  <p>Why my code is showing "segmentation Fault" for long run? Please anyone explain me the causes and prevention techniques to rectify it. Thank u in advance.</p>
  
  <p>My Code is:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;string.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;ctype.h&gt;
  struct prod_details
  {
      int no_prod;
      double total;
      double array[1024];
  }pd;
  
  char *getinput(char *inp)
  {
      printf(":");
      gets(inp);
      if(strlen(inp) &gt; 11)
      {
          printf("Input is restricted to 10 character\n");
          getinput(inp);
      }
      return inp;
  }
  
  void print()
  {
      printf("Grant Total is : %.2f\n",pd.total);
  }
  
  int check(char *str)
  {
      int i,minusflag=0,plusflag=0,optrflag=0;
      if(strlen(str) == 0)
          return 0;
      if(str[strlen(str) -1] == '+' || str[strlen(str) -1] == '-' || str[strlen(str) -1] == '*')
      {
          printf("last operator\n");
          return 1;
      }
  
      if(str[0] == '-')
      {
          i=1;
          while(str[i] != '-' )
          {
              if(str[i] == '\0')
              {
                  minusflag=1;
                  break;
              }
              i++;
          }
          if(str[i] == '-')
          {
              minusflag=0;
              return 1;
          }
      }
      if(str[0] == '-')
  
      {
          i=1;
          while(str[i] != '+' )
  
          {
              if(str[i] == '\0')
              {
                  minusflag=1;
                  break;
              }
              i++;
          }
          if(str[i] == '+')
          {
              minusflag=0;
              return 1;
          }
      }
      if(str[0] == '-' &amp;&amp; minusflag == 1)
          return 2;
      if(str[0] == '+')
      {
          i=1;
          while(str[i] != '+')
          {
              if(str[i] == '\0')
              {
                  plusflag=1;
                  break;
              }
              i++;
          }
          if(str[i] == '+')
          {
              plusflag=0;
              return 1;
          }
      }
      if(str[0] == '+')
      {
          i=1;
          while(str[i] != '-' )
          {
              if(str[i] == '\0')
              {
                  plusflag=1;
                  break;
              }
              i++;
          }
          if(str[i] == '-')
          {
              plusflag=0;
              return 1;
          }
      }
      if(str[0] == '+' &amp;&amp; plusflag == 1)
          return 2;
      if(str[0] == '*')
          return 1;
      if((str[0] == '+' || str[0] == '-') &amp;&amp; (str[1] == '+' || str[1] == '-' || str[1] == '*' || str[1] == '/'  ))
          return 1;
  
      for(i=0;i&lt;strlen(str);i++)
      {
          if(((str[i] &gt;= '!' &amp;&amp; str[i] &lt;= '/') || (str[i] &gt;= ':' &amp;&amp; str[i] &lt;= '~')))
          {
              if(str[i] == '*' || str[i] == '+' || str[i] == '-' || str[i] == '/')
              {
                  optrflag++;
              }
              else
                  return 1;
          }
      }
      if(optrflag == 1)
          return 3;
      else if(optrflag &gt; 1)
          return 1;
      return 2;
  }
  
  int expcalc(char *str)
  {
      char copy[10];
      char op;
      char *temp;
      char numb[10],numf[10];
      printf("Start of expcal\n");
      int i;
      double result=0;
  
      for(i=0;i&lt;strlen(str);i++)
      {
          if(str[i] == '+' || str[i] == '-' || str[i] == '*' || str[i] == '/')
          {
              op = str[i];
          }
      }
      strcpy(copy,str);
      i=0;
      while(str[i] != op)
      {
          numf[i] = str[i];
          i++;
      }
      numf[i] ='\0';
      temp=strchr(copy,op);
      i=1;
      printf("\n");
      while(temp[i] != '\0')
      {
          numb[i-1] = temp[i];
          i++;
      }
      numb[i-1] = '\0';
      switch(op)
      {
          case '+':
              result=atof(numf)+atof(numb);
              break;
          case '-':
              result=atof(numf)-atof(numb);
              break;
          case '*':
              result=atof(numf)*atof(numb);
              break;
          case '/':
              result=atof(numf)/atof(numb);
              break;
          default:
              break;
      }
      printf("%.2f\n",result);
      if((pd.total+result) &lt; 0)
      {
          printf("Couldn't calculate\n");
          return 0;
      }
      pd.array[pd.no_prod]=result;
      pd.total=pd.total+result;
      printf("&gt;&gt;:%.2f\n",pd.total);
      pd.no_prod++;
      return 0;
  }
  
  int calc(char *str)
  {
      if((pd.total+atof(str)) &lt; 0)
      {
          printf("Coundn't Calculate\n");
          return 0;
      }
      pd.array[pd.no_prod]=atof(str);
      pd.total=pd.total+pd.array[pd.no_prod];
      printf("&gt;&gt;:%.2f\n",pd.total);
      pd.no_prod++;
      return 0;
  }
  int call()
  {
      int chkflg;
      char input[1024];
      getinput(input);
      chkflg=check(input);
      if(chkflg == 3)
      {
          expcalc(input);
          call();
      }
      else if(chkflg == 2)
      {
          calc(input);
          call();
      }
      else if(chkflg == 1)
      {
          printf("You have entered Wrogly!!!\n Please enter correctly\n");
          call();
      }
      else
      {
          print();
          return 1;
      }
      return 0;
  }
  
  int main()
  {
      printf("..CASE  RIGISTER..\n");
      call();
      return 0;
  }
  </code></pre>