Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6944789
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T13:21:10+00:00 2026-05-27T13:21:10+00:00

The code: #include stdafx.h #include stdio.h #include math.h #include stdlib.h #include time.h int main()

  • 0

The code:

#include "stdafx.h"
#include "stdio.h"
#include "math.h"
#include "stdlib.h"
#include "time.h"
int main()
{
    time_t start, end;
    time (&start);
    int i;
    double dif;
    /*int serie[100000];*/
    /* int *serie = malloc( sizeof(int) );

    for (i = 0; i <= 100000; i++) {
        *serie(i)=rand();
        printf("%d \n", serie[i]);
    }
    */
    time (&end);
    dif = difftime (end, start);

    printf ("Time of execution is: %f\n", dif );

    getchar();

    return 0;

}

The Introduction: (no need to read it)

I knew a bit of C++ from about 3 years ago. I have decided
to learn C to create a “fast” subset sum algorithm. This was a
simple program to learn C. The problem is to create random numbers and
time it. This code actually works when I compiled as default or
C++ with Visual Studio, but I decided to do it in C
and I wanted to create a dynamic array.

It seems that there is no new in C. We have to use malloc,
but to compile malloc I think it has to be compiled in C. In C++,
it gives me this error:

cannot convert from 'void *' to 'int *'

Anyway, it happens that I decided to write in C, so it seems logical to
compile in C. I have chosen the time function to measure the program,
I like it more than clock because I do not know how many processors
are working.

The question:

The code as it is above with the comments compiles perfectly in C++, but in C it doesn’t compile.
Specifically, this line:

time (&start);

gives me this error:

syntax error : missing ';' before 'type'

I just want to compute the start time and the end time and then
subtract them with difftime as I have successfully done in C++.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T13:21:11+00:00Added an answer on May 27, 2026 at 1:21 pm

    I bet that your C compiler is defaulting to the rule that variable declarations must be at the beginning of a block.

    You have your call to time(&start) before int i. This is OK in C++ but not in C. To be specific, all variables must be declared before any program code in all versions of C until C99. In the 1999 C Standard, the rule changed to be like C++ and you can mix code and variable declarations.

    If you have a C99 compiler or a compiler option that does allow C99 rules, still don’t do it, because it isn’t reliably portable. And even in C99 there are not many good reasons to mix variables and code anyway. The only one I can think of is a C99 variable length array that requires code to calculate the desired length.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

#include stdafx.h #include stdio.h #include math.h #include stdlib.h void test (int a,int *b, int
#include stdafx.h #include stdio.h #include math.h int main() { float c; printf(Type c); scanf(%f,
Test the following code: #include <stdio.h> #include <stdlib.h> main() { const char *yytext=0; const
i have the following code: #include <stdio.h> int main(void) { float a[4] __attribute__((aligned(0x1000))) =
The code #include <QtCore> #include <QtXml/QDomElement> int main(int argc, char *argv[]) { QCoreApplication app(argc,
I have this code #include <iostream> using namespace std; int main(int argc,char **argv) {
I have the following code: #include <string.h> int main(void) { char *buffer = NULL,
#include stdafx.h #include <windows.h> #include <winsock.h> #include <stdio.h> int APIENTRY WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR IpCmdLine,int
I have written the following code: #include stdafx.h #include <iostream> using namespace std; double
I tried without any result. My code looks like this: #include stdafx.h #include <iostream>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.