locked
c program in visual studio 2010

    Question

  • hello everyone,

    thank you very much that you are going to read my problem,

    i am a begineer

    using visual studio 2010 ultimate

    it gives me option to make a file in cpp extension which is c++.

      i want to make a file with .c extension and execute it ,, but after try a lot not able to do this

    please help

    your friend :sparsh

    Wednesday, June 06, 2012 6:24 PM

Answers

  • Hi sparsh,

    Welcome to MSDN forums.

    Brian is right. Based on your description, you want to make a *.c extension and execute it, and here I give you two examples below:

    1. New an empty project:

    • Add a new item of c++ file(.cpp) to your source files, then change its extension to “.c”.
    • Input c style code in your *.c file, like below:
    #include <stdio.h>
    void main()
    {
    	printf("Hello world!\n");
    }

    2. Change complier option of an complied project:

    • Change your *.cpp file to *.c file.
    • Go to the properties pages, then ->configuration properties-> C/C++->Advanced, choose “Compile As C Code(/TC)” from the Compile As.

    I hope it will help you.

    Regards,

    Elegentin Xie


    If your question has been solved, please mark the answers and close the post. If not, please feel free to post your questions. With regards.


    Tuesday, June 12, 2012 7:22 AM
    Moderator

All replies

  • It's pretty simply. You can use the GUI to create a *.cpp file, and then simply change the extension to *.c. Now recompile. The C compiler is automatically used instead of the C++ compiler.

    If you say you are "not able to do this", you need to be more specific as to exactly what you mean by this, if you want a more specific answer.

    Wednesday, June 06, 2012 9:02 PM
  • Hi sparsh,

    Welcome to MSDN forums.

    Brian is right. Based on your description, you want to make a *.c extension and execute it, and here I give you two examples below:

    1. New an empty project:

    • Add a new item of c++ file(.cpp) to your source files, then change its extension to “.c”.
    • Input c style code in your *.c file, like below:
    #include <stdio.h>
    void main()
    {
    	printf("Hello world!\n");
    }

    2. Change complier option of an complied project:

    • Change your *.cpp file to *.c file.
    • Go to the properties pages, then ->configuration properties-> C/C++->Advanced, choose “Compile As C Code(/TC)” from the Compile As.

    I hope it will help you.

    Regards,

    Elegentin Xie


    If your question has been solved, please mark the answers and close the post. If not, please feel free to post your questions. With regards.


    Tuesday, June 12, 2012 7:22 AM
    Moderator
  • You can also create a text file and give it the .c extension.  Afterwards, you would write code as usual inside the text file.  This option may be a choice for any file type extension in an empty project. There is little to no difference between you and the wizard doing it. The empty project will require you to be in full control of including files, more work.

    If you are using a project template/wizard it may be better just to rename the file.  If you want to add a C code file, just remember to add a text file with a .c file extension in the filename.

    Good luck.


    David

    Thursday, June 14, 2012 2:14 AM


    • Edited by sparsh610 Thursday, June 14, 2012 3:24 PM i do the same as per your answer but still not able to see the output
    Thursday, June 14, 2012 3:17 PM
  • Your program invokes undefined behavior.  Variable f in rec() has an indeterminate value.  Attempting to use this value is not allowed.  Did you mean
        f = x*2;

    Your call to printf() in rec() doesn not contain a \n in the format argument nor do you ever call fflush for stdout.  If stdout is buffered, the system is never told to send the data in the buffer to its intended destination (your monitor?).

    If you step through the program in debug mode it's not a problem but rec() calls itself recursively forever.  If you ever fix it so it terminates, remember that it must return an int back to main().

    Thursday, June 14, 2012 5:33 PM
  • >still not able to see the output

    I presume you actually *ran* the program after building it?

    The output from a console program is - naturally enough - in
    a console window. That window will open - be created - when
    the program starts, and will close - be destroyed - when the
    program ends. You must use some method to keep the window
    open long enough for you to examine it. There are various
    ways to do that, Try running it using Ctrl-F5 (Run without
    debugging). Or one of the methods described in this thread:

    http://social.msdn.microsoft.com/Forums/en-US/Vsexpressvc/thread/9cf1b231-df76-4424-82a1-c42f1ad66fea

    - Wayne

    Thursday, June 14, 2012 8:34 PM
  • Hi sparsh,

    Welcome back.

    Based on your screenshot, maybe you want to print word in an infinite loop. However, you have not assigned the variable f, so change the code like below, maybe you can get what you want:

    int rec(int x)
    {
           f=x*2;
           printf("%d",f);
           rec(5);
    }

    Please feel free to post your problems, if you have any more.

    Regards,

    Elegentin Xie


    If your question has been solved, please mark the answers and close the post. If not, please feel free to post your questions. With regards.


    Friday, June 15, 2012 7:16 AM
    Moderator
  • thanks for ur reply

    ctrl+f5 works

    but still it ask for assignment of f variable.

    but upto my knowledge best if we don't assigned any value ,it become of auto class and value takes a "garbage value".

    i want from this program to print a garbage value each time it executes the loop :)

    please help

    ur friend :sparsh

    Saturday, June 16, 2012 7:24 AM
  • The warnings: 1. recursive code : means that you are calling the same function you are defining in the function definition. The program may not exit/end. Allowed but gives warning.

    2. returns value: means you are using a funtion that returns a value, but you dont assign the returned value. Allowed but gives warning.

    3. uninitialized variable: means you are using a variable that has not been initialized and may have bad/corroupt data. Allowed but gives warning.

    Change the f to x like the function signature shows. Change the last statement in rec function from rec(5) to return x;. Live with the warning and just run the program, or disable warning in the compiling options. The program buildt and also should run.

    Good luck.


    David


    • Edited by 7david7 Saturday, June 16, 2012 7:59 AM edit 5 to x.
    Saturday, June 16, 2012 7:49 AM
  • thanks for ur time

    but i leave int f; becoz i want to generate the garbage value;

    so that each time loops runs; a new value get printed;

    but still it gives me warning;

    switch from turbo c to MS studio;

    in turbo c it still shows garbage value ; but not in ms studio;

    on compilation i.e ctrl+f7 command promt not come in to play :(

    please help

    ur friend : sparsh :)


    • Edited by sparsh610 Saturday, June 16, 2012 12:19 PM
    Saturday, June 16, 2012 12:17 PM
  • >but i leave int f; becoz i want to generate the garbage value;

    Your choice.

    >so that each time loops runs; a new value get printed;

    Not necessarily. In Debug builds, VC++ will initialize
    any uninitialized variables to a special value which
    the Debugger uses to help track overwrites, etc. So
    for a Debug version of your program variable f will
    always have the same value. Note that in Release builds
    no special initialization of otherwise uninitialized
    variables is done, so variable f *may* have a random
    value. However, note that in Release builds code
    optimization is enabled by default. (It's off in
    Debug builds.) That may affect the output.

    >but still it gives me warning;

    Do you mean the *compiler* warning? If so, of course
    it does. How can the compiler read your mind and know
    that you're doing it on purpose? Just ignore it if
    that's what you really want to do. Or disable that
    particular warning message - which I *strongly*
    advise against, especially for a beginner. Beginners
    should have as many warnings enabled as possible and
    should address and fix each of them.

    If you mean you get a warning at *run* time, then
    that's probably because you've done a Debug build.
    If you do a Release build you won't get a run time
    warning each time the variable is used.

    >switch from turbo c to MS studio;
    >in turbo c it still shows garbage value ;
    >but not in ms studio;

    Tough. As Barry told you:
    "Your program invokes undefined behavior."
    That means that anything may and likely will happen.
    There are no guarantees with undefined behavior -
    that's why it's called "undefined".

    - Wayne
    Saturday, June 16, 2012 12:51 PM
  • Rather than leave f with no value which invokes undefined behavior when you try to reference that value, why not assign a random value to f and achieve the same result legally.

    Call srand in main() and call rand in rec().

    Sunday, June 17, 2012 1:39 AM
  • can someone also help me with mine? 

    i really dont understand why this wont work -.- i made this on .cpp extention..

    but the problem is, i don't understand how to make this on .c extention, it had many errors please help

    -------------------------------------------------------------------------------------

    #include <iostream.h>
    #include <stdlib.h>
    #include <conio.h>

    char matrix[3][3];
    void cou(void);
    int main()
    {

    int m,n;
    char ch='y';
    while(ch=='Y'||ch=='y'){
    for (m=0;m<3;m++)for (n=0;n<3;n++)matrix[m][n]= '\0';
    int i,j,sum=0;
    while ( sum < 10){
    if (sum == 0) cou();
    cout<<"Player 1 is 'X': choose the row and column"<<endl;
    cout<<"Row : ";
    cin>>i;
    cout<<"Column : ";
    cin>>j;
    for (;i>3 || i<1 || j>3 || j<1 ||('X'==matrix[i-1][j-1]||'O'==matrix[i-1][j-1]);) {cout<<"Sorry boy, but you gotta choose another place.\n";cout<<"row : ";cin>>i;cout<<"column : ";cin>>j;}
    matrix[i-1][j-1]='X';
    sum++;
    cou();


    if (matrix[0][0]=='X' && matrix[0][0]==matrix[1][1] && matrix[1][1]==matrix[2][2]) {cout<<"Player 1 wins"<<endl;break;}
    if (matrix[2][0]=='X' && matrix[2][0]==matrix[1][1] && matrix[1][1]==matrix[0][2]) {cout<<"Player 1 wins"<<endl;break;}
    if (matrix[0][0]=='X' && matrix[0][0]==matrix[1][0] && matrix[1][0]==matrix[2][0]) {cout<<"Player 1 wins"<<endl;break;}
    if (matrix[0][1]=='X' && matrix[0][1]==matrix[1][1] && matrix[1][1]==matrix[2][1]) {cout<<"Player 1 wins"<<endl;break;}
    if (matrix[0][2]=='X' && matrix[0][2]==matrix[1][2] && matrix[1][2]==matrix[2][2]) {cout<<"Player 1 wins"<<endl;break;}
    if (matrix[0][0]=='X' && matrix[0][0]==matrix[0][1] && matrix[0][1]==matrix[0][2]) {cout<<"Player 1 wins"<<endl;break;}
    if (matrix[1][0]=='X' && matrix[1][0]==matrix[1][1] && matrix[1][1]==matrix[1][2]) {cout<<"Player 1 wins"<<endl;break;}
    if (matrix[2][0]=='X' && matrix[2][0]==matrix[2][1] && matrix[2][1]==matrix[2][2]) {cout<<"Player 1 wins"<<endl;break;}

    if (sum == 9){cout<<"The game is over and no one wins, hahaha, you both stink!!!"<<endl; break;} //sum=9 because there are only 9 boxes in the game


    cout<<"Player 2 is 'O': choose the row and column"<<endl;
    cout<<"Row : ";
    cin>>i;
    cout<<"Column : ";
    cin>>j;
    for (;i>3 || i<1 || j>3 || j<1 ||('X'==matrix[i-1][j-1]||'O'==matrix[i-1][j-1]);) {cout<<"Sorry boy, but you gotta choose another place.\n";cout<<"row : ";cin>>i;cout<<"column : ";cin>>j;}
    matrix[i-1][j-1]='O';
    sum++;

    cou();

    if (matrix[0][0]=='O' && matrix[0][0]==matrix[1][1] && matrix[1][1]==matrix[2][2]) {cout<<"Player 2 wins"<<endl;break;}
    if (matrix[2][0]=='O' && matrix[2][0]==matrix[1][1] && matrix[1][1]==matrix[0][2]) {cout<<"Player 2 wins"<<endl;break;}
    if (matrix[0][0]=='O' && matrix[0][0]==matrix[1][0] && matrix[1][0]==matrix[2][0]) {cout<<"Player 2 wins"<<endl;break;}
    if (matrix[0][1]=='O' && matrix[0][1]==matrix[1][1] && matrix[1][1]==matrix[2][1]) {cout<<"Player 2 wins"<<endl;break;}
    if (matrix[0][2]=='O' && matrix[0][2]==matrix[1][2] && matrix[1][2]==matrix[2][2]) {cout<<"Player 2 wins"<<endl;break;}
    if (matrix[0][0]=='O' && matrix[0][0]==matrix[0][1] && matrix[0][1]==matrix[0][2]) {cout<<"Player 2 wins"<<endl;break;}
    if (matrix[1][0]=='O' && matrix[1][0]==matrix[1][1] && matrix[1][1]==matrix[1][2]) {cout<<"Player 2 wins"<<endl;break;}
    if (matrix[2][0]=='O' && matrix[2][0]==matrix[2][1] && matrix[2][1]==matrix[2][2]) {cout<<"Player 2 wins"<<endl;break;}

    }
    cout<<"\nWould you like to play again??? (Y - N)\n";
    cin>>ch;
    }
          system("PAUSE");
          return 0;
    }




    void cou(void)
    {

    cout<<"\n\t\t                1   2   3\n"<<endl;
    cout<<"\t\t             1  "<<matrix[0][0]<<" | "<<matrix[0][1]<<" | "<<matrix[0][2]<<endl;
    cout<<"\t\t               ---|---|---\n";
    cout<<"\t\t             2  "<<matrix[1][0]<<" | "<<matrix[1][1]<<" | "<<matrix[1][2]<<endl;
    cout<<"\t\t               ---|---|---\n";
    cout<<"\t\t             3  "<<matrix[2][0]<<" | "<<matrix[2][1]<<" | "<<matrix[2][2]<<"\n\n\n";
    }

    Sunday, September 30, 2012 2:16 PM
  • can someone also help me with mine? 

    i really dont understand why this wont work -.- i made this on .cpp extention..

    but the problem is, i don't understand how to make this on .c extention, it had many errors please help

    -------------------------------------------------------------------------------------

    #include <iostream.h>

    cout<<"Player 1 is 'X': choose the row and column"<<endl;
    cout<<"Row : ";
    cin>>i;
    cout<<"Column : ";
    cin>>j;

    Of course it had many errors. Your program is trying to
    use libraries and features that are only available in the
    C++ language, not the C language. You have to change all
    of the C++-specific code to something that will work in
    C. For example: iostream, cout, cin need to be changed
    to stdio.h, printf, gets(), scanf(), etc.

    You should delete your post from this thread and start a
    new thread of your own. It's usually not a good idea to
    attach your own problems to the end of an existing older
    thread, especially one which has already been marked as
    "Answered".

    - Wayne

    • Proposed as answer by i1friend Monday, October 01, 2012 6:04 AM
    Sunday, September 30, 2012 4:46 PM