-4

I want to print on the screen index of velocity.I am not author of the code.

Code is here http://pastebin.com/47CbB1vb

And get line is also necessary for compilation

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/*
*   get_line:
*       Reads a line into string, returns length.
*       Kernighan & Ritchie p.69
*
*/
int
get_line(fp, line, lim)
   FILE           *fp;
   char           *line;
   int             lim;
{
   int             c, i;

   i = 0;
   while (--lim > 0 && (c = getc(fp)) != EOF && c != '\n')
      line[i++] = c;
   if (c == '\n')
   {
      line[i++] = c;
   }
   line[i] = '\0';
   if (c == EOF)
      return (-1);
   else
      return (i);
}
/*
 *  ignore_line:
 *      Gets the next line (up to and including the newline
 *      character) from the file pointed to by fptr and
 *      promptly loses it.  Taken from asc2ah.c.
 *
 *          Siggi   29.06.1990
 */
int
ignore_line(dat_fp)
   FILE           *dat_fp;
{
   char            string[256];
   char           *fgets();
   if (fgets(string, 250, dat_fp) == NULL)  /* nothing there     */
      return (-1);
   return (0);          /* there was something   */

WHen I try to compile with gcc

gcc gi_line.c  vel2d.c -lm -o vel2d
vel2d.c: In function ‘main’:
vel2d.c:205:19: warning: format ‘%f’ expects argument of type ‘double’, but argument 3 has type ‘int’ [-Wformat=]
                   fprintf(stdout,"%.1f",index);
                   ^
vel2d.c:206:9: error: ‘else’ without a previous ‘if’
         else
         ^

I have included only fprintf line.Without that line I c can compile code and it works perfectly.So what should I change?

milenko
  • 31
  • 1
  • 10
  • 1
    Where is the `fprintf` in your code snippet? (It's on line 205 and the offending `else` is on line 206. Your code doesn't have that many lines.) – M Oehm Apr 01 '16 at 13:08
  • 1
    Possible duplicate of [Why do I get a compiling error that says error: ‘else’ without a previous ‘if’?](http://stackoverflow.com/questions/17587875/why-do-i-get-a-compiling-error-that-says-error-else-without-a-previous-if) – owacoder Apr 01 '16 at 13:08
  • @MOehm - It's in the pastebin link. But the problem is just two statements after an `if` with an `else` after it. Nothing a pair of curly brackets wouldn't fix. – owacoder Apr 01 '16 at 13:09
  • Oh god, it's 1969 again, and everything is horrible. – EOF Apr 01 '16 at 13:09
  • @MOehm I have changed pastebin,take a look! – milenko Apr 01 '16 at 13:13
  • 2
    @owacoder: Okay, I see it now. Typical case of "I don't need braces, it's just one statement" and "I'll just add a quick print statement for debuging here". – M Oehm Apr 01 '16 at 13:13
  • 1
    `return` is a statement, not a function. There is no need parenthetising the result and it should not be done in modern code with such a simple expression. Also the use of non-prototype function declarators is deprecated since a long time. The code is ancient and can include other outdated features which differ from standard C. – too honest for this site Apr 01 '16 at 13:25
  • 1
    Don't post external links to code only. See [ask] and provide a [mcve] – too honest for this site Apr 01 '16 at 13:29

1 Answers1

1

Here's the offending code in question:

       if (xflg)
              index = m * nxy + l*nx + k;
              fprintf(stdout,"%.1f",index);
       else
          index = m * nxy + k*ny + l;

The "if" and "else" sections of this if statement originally had one line each, so adding braces wasn't strictly necessary. But when you added the call to fprintf, that changed things.

Since the are no braces after if, the index = m * nxy + l*nx + k; line consists of the entire if part. The printf that follows is not part of the if and is a separate statement. Then when the compiler sees the else, there's no corresponding if because the prior if is complete.

This can be fixed by adding the braces:

       if (xflg) {
              index = m * nxy + l*nx + k;
              fprintf(stdout,"%.1f",index);
       } else {
          index = m * nxy + k*ny + l;
       }

You should always add braces to any if, else, for, while, or do block, even if the body is only one line. That prevents errors like this from happening.

You were lucky in this case that failing to add braces resulted in a compiler error. If you didn't have an else part in this example, the code would compile and the printf would always run. Then you'd be scratching your head to figure out why.

As for the printf warning, index is of type int, so you need to use the proper format specifier to print it which is %d.

dbush
  • 205,898
  • 23
  • 218
  • 273
  • How to write m,l,k in one line?I have tried fprintf(f,"%2d %d %2d\n",m,l,k); but it writes each in new line. – milenko Apr 01 '16 at 15:13