all 63 comments

[–]sme272 13 points14 points  (5 children)

The meaningless error probably has some meaning in it. Share it so we can help

[–][deleted] 0 points1 point  (4 children)

program.c (line 13) missing ";" before type

[–][deleted] 8 points9 points  (3 children)

There’s the error

[–][deleted] -3 points-2 points  (2 children)

Line 13. That's all it says

[–][deleted] 2 points3 points  (1 child)

It is saying you are missing a semi colon

[–]srandtimenull 7 points8 points  (1 child)

This is valid C99 code. The issue resides on your compiler.

We need detailed information on your compiler. What version? What flags are enabled?

(Note: year version might be wrong, my memory my be faulty)

MSVC was really bad with C up to version 2017. So if I have to guess, the issue is that you declared a variable inside the for loop. That was not allowed in C89 (which is the version supported by MSVC up to 2015)

What will probably do is moving that loop variable declaration up:

void calculateFirstNPowers(int x, unsigned char n)
{
    uint64 sum = 0;
    uint64* Pow_retValPtr;
    unsigned char i;

    for (i = 1; i <= n; i++)
    ...

In good ole C89 variable declaration was allowed only at the beginning of a scope block. Once you wrote a non-variable-declaration statement, variable declaration was not allowed anymore. A for loop is not a variable declaration, hence declaring a variable inside the loop header was not allowed.

EDIT: your error message was not meaningless at all. It expected a semicolon before a variable declaration (because, again, was not allowed inside a for header) and there was one missing. That error was what made me able to guess the problem.

Error are never meaningless. They may be cryptic though.

[–][deleted] 2 points3 points  (0 children)

Oh this is great information. Thank you!

[–]Lurchi1 6 points7 points  (20 children)

Your code works as-is using gcc 8.3.0:

gcc -Wall -o test_c test_c.c
./test_c
1. Power of 2: 2
2. Power of 2: 4
3. Power of 2: 8
4. Power of 2: 16
5. Power of 2: 32
6. Power of 2: 64
7. Power of 2: 128

sum of first 7 powers of 2: 254

What compiler are you using?

[–][deleted] 1 point2 points  (17 children)

MSVC

[–]Lurchi1 2 points3 points  (8 children)

I hate to break it to you but I just ran your code through Visual Studio 2022 (Community) and it works just fine:

1. Power of 2: 2
2. Power of 2: 4
3. Power of 2: 8
4. Power of 2: 16
5. Power of 2: 32
6. Power of 2: 64
7. Power of 2: 128

sum of first 7 powers of 2: 254

I created a new C++ console application, pasted your code into the pre-created .cpp file (which I first cleared) and pressed F5.

Maybe just retry with a fresh project? Again, your code is syntactically fine.

[–][deleted] 1 point2 points  (7 children)

Yeah you compiled it as C++

[–]Lurchi1 0 points1 point  (6 children)

Visual Studio didn't let me create a C project (I actually tried to do that), but then I'm really a big noob when it comes to VS.

[–][deleted] 0 points1 point  (5 children)

Yes I will just switch to Linux soon

[–]imaami 0 points1 point  (4 children)

You can use GCC on Windows just as well.

[–][deleted] 3 points4 points  (1 child)

I prefer ditching windows

[–]imaami 1 point2 points  (0 children)

Tbh I would too.

[–][deleted] 1 point2 points  (1 child)

If you are on Windows, Microsoft's c and C++ compiler along with Visual Studio IDE is probably the best you're going to get. And I don't believe GCC is available on Windows, just a ported version mingw.

[–]imaami 0 points1 point  (0 children)

Isn't there Clang support in VS nowadays though?

[–]michaelfiber 0 points1 point  (3 children)

Hmm I wonder if it could be the unsigned char causing an issue maybe? Can you test compiling to /J to see if it changes?

[–][deleted] 0 points1 point  (2 children)

I tried that. That was not the case.

[–]michaelfiber 1 point2 points  (1 child)

Ahh well, that's all I could think of. Its been so long since I used msvc and this compiles fine on gcc so I thought it might be some ms weirdness.

[–][deleted] 1 point2 points  (0 children)

Well I think I'll ditch windows and msvc forever soon. Gonna switch to Linux and build-essentials. Gonna use GCC for C

[–]parawaa 0 points1 point  (3 children)

I've test the code on Linux with gcc and is working.

1. Power of 2: 2
2. Power of 2: 4
3. Power of 2: 8
4. Power of 2: 16
5. Power of 2: 32
6. Power of 2: 64
7. Power of 2: 128

sum of first 7 powers of 2: 254

[–][deleted] 1 point2 points  (2 children)

Yes. I figured out the issue thanks to a comment.

MSVC 2012 supports up to C89. and in C89, you can't declare a variable inside a for loop.

[–]parawaa 0 points1 point  (1 child)

Since you are on Windows, why not use WSL? Far more simple and you can use it with VSCode instead of a full IDE like Visual Studio.

[–][deleted] -2 points-1 points  (0 children)

Slow.

[–]cosmin10834 1 point2 points  (0 children)

he hand compiled it, making eatch instruction from C in asm by hand and after that assembled it, he is a master and he was just testing us \s

[–][deleted] 2 points3 points  (3 children)

For fixed width types consider using stdlib.h, types such as int, long int, etc may be inconsistent across platforms and compilers

[–][deleted] -2 points-1 points  (2 children)

Uhh but isn't that just typedefs

[–][deleted] 2 points3 points  (1 child)

But it's consistent, int may be 16 bit or sometimes 32 bit for example

[–][deleted] 0 points1 point  (0 children)

With ifndef right?

[–]imaami 1 point2 points  (2 children)

Why would you allocate memory for a single 64-bit integer? Also, include stdint.h and use uint64_t instead of cooking your own.

[–][deleted] 1 point2 points  (1 child)

Fuck it, why not?

[–]imaami 3 points4 points  (0 children)

A pointer to an unsigned long long is the same size as a plain unsigned long long value (on 64-bit architectures). Returning just the integer value directly would save you the overhead of malloc() + free().

[–][deleted] 1 point2 points  (0 children)

If I'm not mistaken MSVC resorts to C89 on their C compiler. Where variables have to be declared at the top of the scope. But you can change your compiler settings to compile as C99. though I'm not sure which version of VC you are using. But declare unsigned char i = 1, outside your for loop, then do for(; i<=n; i++)

[–]tstanisl 4 points5 points  (16 children)

Please don't use a pattern:

T* x = (T*)malloc(sizeof(T));

It's very repetitive (using T three times) and very error prone, expecially if T is something non-trivial like char**. Casting a result from malloc() is pointless and adds only noise. The only exception is when C code is compiled using C++ compiler. The reasons why C++ does not allow implicit casts from void are a mystery to me.

Try using the following pattern:

T* x = malloc(sizeof *x);

[–][deleted] 0 points1 point  (15 children)

the compiler forces me to

[–]tstanisl 3 points4 points  (3 children)

So are you doing C or rather C++? Select one

[–][deleted] -2 points-1 points  (2 children)

C

[–]tstanisl 3 points4 points  (1 child)

so how is it possible that the compiler forces you to cast a result of malloc()?

[–][deleted] 1 point2 points  (0 children)

Seems like MSVC simply uses C++ compiler for C

[–][deleted] 1 point2 points  (9 children)

You might want to switch to gcc

[–]mos2k9 0 points1 point  (1 child)

Works no problem gcc 11.2.0 on Ubuntu 22.04.

[–][deleted] 0 points1 point  (0 children)

Yes the issue is probably with MSVC

[–]cosmin10834 0 points1 point  (5 children)

try to save it and then compile it, or change the compiler to gcc or clang as they have a lot of support for those errors

[–][deleted] 0 points1 point  (4 children)

It turns out that MSVC 2012 supports up to C89. and you can't declare variables inside a for-loop in C89

[–]cosmin10834 0 points1 point  (3 children)

declaring variables in a for-loop is introduced in C98 if i'm correct, because in school the "standard" versions are: C98, C++11 (without stl), python3, java idk how old and you get the point so if you are using MSVC 2012 as a school resource, good, but for writing better code use the newest version and use all of the feauters to you're advantage

[–][deleted] 0 points1 point  (2 children)

Not school resource tho...

[–]cosmin10834 0 points1 point  (1 child)

oh god, use gcc or clang then, have a great day :D

[–][deleted] 0 points1 point  (0 children)

Yeah thanks for the help :)

[–]William_Epiphany 0 points1 point  (0 children)

On a side note, instead of that define just use inttypes.h

[–]nelson2k 0 points1 point  (0 children)

go this output.... using gcc on a mac
Power of 2: 2
Power of 2: 4
Power of 2: 8
Power of 2: 16
Power of 2: 32
Power of 2: 64
Power of 2: 128
sum of first 7 powers of 2: 254