0

I was trying to create a class in c, after seeing some tutorial.

I saw video on classes and objects on youtube , the code was in java but instructor said it is same for other languages too. I have learnt some basic in C language. When I opened Code::Blocks and typed "class" it highlighted and then I compiled it without any error. I know that C isn't a OOP language so it should not contain classes. And yes the file was a .c file.

 #include"stdio.h"
  void main(void)
  {
         int sa[5];
         printf("Size : %ld\n",sizeof(sa));
  }
  class;

There are no errors while compiling it but there are some warning

warning: data definition has no type or storage class
warning: type defaults to ‘int’ in declaration of ‘class’ [-Wimplicit-int]|
||=== Build finished: 0 error(s), 2 warning(s) (0 minute(s), 0 second(s)) ===|
  • 4
    *"I know that C isn't a OOP language so it should not contain classes."* That's right. Due to that, `int class;` is valid, because `class` isn't a reserved keyword, just like `foobar` for instance. The warning says that it is implicitly turning your `class;` into `int class;`. – Blaze Jul 08 '19 at 14:45
  • 4
    > it is implicitly turning your class; into int class; That's nasty! – mihai Jul 08 '19 at 14:47
  • 4
    Because CodeBlocks is not that good of an IDE. It highlights `class` because that's a C++ keyword. Not sure why it's being highlighted in a C code. – HolyBlackCat Jul 08 '19 at 14:48
  • @HolyBlackCat I also tried other online IDE but they also highlighted "class" as a keyword – Sandeep Shiven Jul 08 '19 at 15:02
  • @SandeepShiven It means those IDEs didn't bother with proper highlighting for C as well. – HolyBlackCat Jul 08 '19 at 15:07
  • 1
    Side note: in C, the `main()` function must be defined to return type `int`, not `void`. But as a special case, if control reaches the closing brace of that particular function then it's handled as if a `return 0;` were executed. – John Bollinger Jul 08 '19 at 15:17

3 Answers3

1

The reasons are two, firstly CodeBlocks is, for some reason, tricking you by highlighting a word that is not a keyword in C. Secondly, the ancient "implicit int" rule, dropped by the C standard in C 2011 is compiling your class; as int class;. I suggest that if you are getting started, you should put a -pedantic-errors -Wall in the compilation options so that you are protected from this and others similar behaviors.

1

I saw video on classes and objects on youtube , the code was in java but instructor said it is same for other languages too.

That was somewhat irresponsible of the instructor. Some other languages have syntax similar to Java's for defining classes -- most notably, C++. On the other hand, some other OO languages have quite different syntaxes, and some don't even use the term "class", much less a keyword with that spelling.

I have learnt some basic in C language. When I opened Code::Blocks and typed "class" it highlighted

"class" is not a C keyword, so it is counterproductive to highlight it as one in C code. If that is what Code::Blocks was doing (for a .c file) then that would constitute a flaw. But if it was highlighting that word as an identifier (a variable name) then it was behaving reasonably.

and then i compiled it without any error.

... but with some warnings that you mention only later:

warning: data definition has no type or storage class
warning: type defaults to ‘int’ in declaration of ‘class’ [-Wimplicit-int]

These are telling you that your code is being interpreted differently than you probably expected.

The two are related. They are telling you that "class" is being treated as an indentifier (of a variable), not as a language keyword. They are furthermore telling you that the type of that identifier is being implicitly assigned as int, which is a compatibility feature of GCC and some other C compilers. Since C99, such a declaration is not valid in C, and if you use a strict(er)-conformance compilation option, such as -std=c99, then the compiler will reject it.

I know that C isn't a OOP language so it should not contain classes.

Does not contain classes would be a more appropriate way to phrase it. But that doesn't mean it recognizes the word "class" as something it should specifically reject. Rather, it means that the word "class" is not recognized as anything special.

John Bollinger
  • 160,171
  • 8
  • 81
  • 157
0

It is correct. the class in your case is a declaration of the variable having the class name. Because you did not specify the type it defaults to int. As there is a warning the IDE is highlighting this declaration.

0___________
  • 60,014
  • 4
  • 34
  • 74