-10

I’m having a hard time with declaring my variables within C. The compiler display a message “expected constructor destructor or type conversion before token.” What am I doing wrong?

#include <stdio.h>

int count =0;
int abc;
ABC; 
a = 19, b = 27, c = 3;

a = 4 + 5 * 3;
b = (4 +5) * 3;
c = 25 -(2 * (10 + (8 / 2)));

main {
   printf("Enter a value please\n");
   scanf("%d, , \n");
   return 0;
}
laraine
  • 21
  • 1
    which variable scanf casts the value ? – Abr001am Dec 16 '15 at 14:48
  • 1
    [The Definitive C Book Guide and List](http://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list) – Ivan Aksamentov - Drop Dec 16 '15 at 15:16
  • 2
    If the compiler talks about constructor and destructor, you are compiling for C++ and not C. – Bo Persson Dec 16 '15 at 15:20
  • Are you trolling? If not, here's a hint: start off with the *minimum* possible code required to compile without errors. Then, add one line at a time, compiling between each line. – Gillespie Dec 16 '15 at 15:45
  • 2
    @RPGillespie there is a lot of really low-level trolling going on ATM. Something to do with teh winter bash hats:( – Martin James Dec 16 '15 at 15:51
  • 1
    I ended up up-voting this otherwise dreadful question which would typically give most readers, "Where do I begin?" kind of responses... but for only one reason: check out these answers below. They deserve some attention. –  Dec 16 '15 at 16:02
  • I think almost everything there is wrong... – rlam12 Dec 16 '15 at 16:15
  • @MartinJames: yeah, I think I just got geek-sniped. At least I'm getting rep out of it, though... – John Bode Dec 16 '15 at 18:21

2 Answers2

6

Here's a rewrite, showing how to fix the various issues:

#include <stdio.h>

int count = 0;     // declarations and intitializations are allowed at file
                   // scope (that is, outside of any function body).  The
                   // storage for count will be set aside at program start
                   // and held until the program terminates.  
                   //
                   // count is visible to all functions defined within this
                   // file, and will be visible to other files compiled and
                   // linked into the same program.  

int main( void ) { // Implicit typing of functions is no longer allowed as of C99,
                   // and it was never good practice to begin with. 
                   // IOW, the compiler will not assume a return type of
                   // int if a type specifier is missing.  main always
                   // returns int, and either takes 0 or 2 arguments.

  int a, b, c;     // int abc; declares a *single* variable named "abc", 
                   // not three variables named "a", "b", and "c".  
                   // Storage for these variables will be set aside at
                   // function entry and held until the function exits.
                   //
                   // None of a, b, or c are visible outside of the
                   // function body.            

                   // ABC has not been defined anywhere; it's not clear
                   // what the intent was behind ABC;

  a = 19;          // While not illegal, a = 19, b = 27, c = 3; is not the best style
  b = 27;          // in the world; it's better to make these three separate statements 
  c = 3;           // Note that assignment statements (like any other executable 
                   // statement) are not allowed outside of a function body.  


  a = 4 + 5 * 3;                   // fine
  b = (4 +5) * 3;                  // fine
  c = 25 -(2 * (10 + (8 / 2)));    // fine

  printf("Enter a value please\n"); // fine

  scanf("%d", &a); // Each conversion specifier in a scanf call needs a corresponding 
                   // argument to write the input value to, and the argument needs
                   // to be of the correct type.  %d expects the corresponding 
                   // argument to have type "int *" (pointer to int).  The expression
                   // "&a" gives the address of the variable "a", and the type 
                   // of the expression is "int *".  So, this statement will read an 
                   // integer value from standard input and store it to a.

  return 0;
}
John Bode
  • 119,563
  • 19
  • 122
  • 198
  • Up-voted since this is the most descriptive code-only example I've ever seen and I think it's a very direct and useful way of illustrating what's wrong (might be ideal as a standard way of answering Qs). –  Dec 16 '15 at 15:54
  • This is a great answer. – P45 Imminent Dec 16 '15 at 17:13
5
  1. You cannot write assignments like a = 19, b = 27, c = 3; outside functions.

  2. int count = 0; is the initialisation of a global variable, so that is allowed.

  3. ABC; is also meaningless unless ABC has been #defined to something numerical, and even then it would be a no-op.

  4. main is also malformed. You need to write it as int main() and make sure you return a value.

  5. Lastly, your scanf argument list is not correct. Do consult the documentation.

It would be a good idea to study an introduction to C. Kernighan & Ritchie is an excellent book.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483