No startup error

Asked

Viewed 51 times

1

I’m having a non-boot error, and I can’t figure out why:

#include <stdio.h>
int main(){
  const int LENGTH = 10;
  const int WIDTH = 5;
  const char NEWLINE = "\n";
  int area;
  area = LENGHT * WIDTH;
  printf("Value of area : %d", area);
  printf("%c", NEWLINE);
  return 0;
}

Mistakes:

C:\Users\zegla\OneDrive\Ambiente de Trabalho\Cexs\trainning6_const.c: In function 'main':
C:\Users\zegla\OneDrive\Ambiente de Trabalho\Cexs\trainning6_const.c:6:24: warning: initialization makes integer from pointer without a cast [-Wint-conversion]
   const char NEWLINE = "\n";
                        ^
C:\Users\zegla\OneDrive\Ambiente de Trabalho\Cexs\trainning6_const.c:9:10: error: 'LENGHT' undeclared (first use in this function)
   area = LENGHT * WIDTH;
          ^
C:\Users\zegla\OneDrive\Ambiente de Trabalho\Cexs\trainning6_const.c:9:10: note: each undeclared identifier is reported only once for each function it appears in
  • Did the answer solve your question? Do you think you can accept it? See [tour] if you don’t know how you do it. This would help a lot to indicate that the solution was useful for you. You can also vote on any question or answer you find useful on the entire site (when you have 15 points).

1 answer

2

It has two errors, one is typing, the variable LENGTH is spelled wrong.

The other mistake is that you’re trying to save an address for a string in a variable that is not a string. She would need to be a pointer to bear what she desires.

#include <stdio.h>
 
int main(){
    const int LENGTH = 10;
    const int WIDTH = 5;
    const char *NEWLINE = "\n";
    int area;
    area = LENGTH * WIDTH;
    printf("Value of area : %d", area);
    printf("%c", NEWLINE);
    return 0;
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

Nos Ides online I did a little better and different in each case.

It’s not usually done that way, although all this code is unnecessary, so it’s even complicated to say what could be better, since it would be better to do everything differently. It can end up getting programming addictions doing this way. This code produces the same result with a line.

  • Shouldn’t be printf("%s", NEWLINE); instead of %c?

  • @Cat should, but there is so much error there, that one more does not make a difference, the important thing is that compiled and rotated :) Doing right would take away all this, since the code does not make sense. I know it’s for learning, but it’s better to learn from more realistic things. I improved the answer by giving alternatives.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.