Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

.C files in VC++ 2008 Express Edition

  1. Feb 6, 2009 #1
    Is there anyway to open a <name>.c file and compile/run it in VC++ 2008 EE without any extra steps?

    After messing around with it, it seems I have to first make a Win32 console application, rename all my files from .cpp to .c, and then change the file to compile as c.

    I know I could do this easily with DevC++ but I just want to get familar with VC++ and right now its getting me frustrated.
     
  2. jcsd
  3. Feb 6, 2009 #2

    mgb_phys

    User Avatar
    Science Advisor
    Homework Helper

    you can use the compiler on the command line with "cl blah.c"

    That's the normal way, there is an option to create an empty project without the files.
    You can then just create and empty .c files
     
  4. Feb 7, 2009 #3
    I tried creating a new .cpp source file, renamed it as a .c file, and opened the Visual Studio 2008 Command Prompt and get this error:

    Code (Text):


    C:\Program Files (x86)\Microsoft Visual Studio 9.0\vc\bin>"C:\Program Files (x86
    )\Microsoft Visual Studio 9.0\Common7\Tools\vsvars32.bat"
    Setting environment for using Microsoft Visual Studio 2008 x86 tools.

    C:\Program Files (x86)\Microsoft Visual Studio 9.0\vc\bin>cd C:\Users\Sean\Deskt
    op

    C:\Users\Sean\Desktop>cl helloworld.c
    Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 15.00.21022.08 for 80x86
    Copyright (C) Microsoft Corporation.  All rights reserved.

    helloworld.c
    Microsoft (R) Incremental Linker Version 9.00.21022.08
    Copyright (C) Microsoft Corporation.  All rights reserved.

    /out:helloworld.exe
    helloworld.obj
    LINK : fatal error LNK1561: entry point must be defined

    C:\Users\Sean\Desktop>

     
    What am I doing wrong?
     
  5. Feb 10, 2009 #4
    Anyone got info on this? Thanks.
     
  6. Feb 10, 2009 #5

    mgb_phys

    User Avatar
    Science Advisor
    Homework Helper

    Would you care to show us the source?
     
  7. Feb 10, 2009 #6
    It appears to me to be saying you don't have a main() function.

    Is there anything in your .c file?
     
  8. Feb 10, 2009 #7
    Hmm, odd. It works now.

    Since I'm working with directly opened .c files I have to open up the Visual studio command prompt change directory and use cl to compile every time?

    When compile errors are found is it printed in the command prompt? What is the debugger for and how can I use it?
     
  9. Feb 10, 2009 #8
    Well, you have to run cl every time... if this is the way you're doing it...

    Yes.

    The debugger runs the program in a special way that gives the ability to stop the program, look at the state of variables, step through one line at a time, etc. But the debugger is a graphical program that exists as part of Visual Studio, and you have to perform the compile in a special way (with "debug symbols") in order to be able to use it. If you want to use features like this you really do not want to be going around Visual Studio's back like this. (For all I know a command line version of the debugger exists, but you probably do not want to use it...) If you're going to be using Visual Studio features you should probably just give in and create a solution/project or whatnot.
     
  10. Feb 10, 2009 #9
    Thanks. I think I'm going to go back to DevC++... there is just too many steps to take just to get code to run.
     
  11. Feb 10, 2009 #10
    Eh? You seem to be making a terrible hash of what is actually a perfectly straightforward procedure.

    • Open VC++.
    • File->New Project and select an empty Win32 Console Application.
    • Add your source code as desired.
    • Press F7 to build.

    End of story.
     
  12. Feb 11, 2009 #11
    Thing is I need just .c files not .cpp files for my class and there is no option for that type of file.

    I would also like to open .c source code and have a one button to compile and run from the IDE.
     
  13. Feb 11, 2009 #12
    Yes there is! When you're adding a source file to an empty project, just select "Add C++ file" but rename it with a C extension.

    Regardless, there's no difference whatsoever between files with a .c extension and files with a .cpp extension; the difference in file extension for C/C++ source is purely a matter of convention and is enforced as a hard and fast rule by very, very few compilers. In particular, Visual Studio treats them as one and the same type of thing.

    Visual Studio works with solutions and projects; as such, it can't be made to compile a source file in the way you suggest without first creating a solution and adding the source to it.

    Please, read the Visual Studio documentation and all this will become clear. It's really perfectly simple once you get used to it.
     
  14. Feb 15, 2009 #13
    Ok for DevC++ I get no compiler errors, yet in Visual Studio I get a couple, does anyone know why this is? Granted my code probably isn't ideal and there are probably programming mistakes but if DevC++ doesn't catch them and I see no problem with the program why does Microsoft's compiler complain.

    Here is errors I get with microsoft's c compiler:

    Code (Text):


    C:\Program Files (x86)\Microsoft Visual Studio 9.0\vc\bin>"c:\Program Files (x86
    )\Microsoft Visual Studio 9.0\Common7\Tools\vsvars32.bat"
    Setting environment for using Microsoft Visual Studio 2008 x86 tools.

    C:\Program Files (x86)\Microsoft Visual Studio 9.0\vc\bin>cd C:\Users\Sean\Docum
    ents

    C:\Users\Sean\Documents>cl jumble.c
    Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 15.00.30729.01 for 80x86
    Copyright (C) Microsoft Corporation.  All rights reserved.

    jumble.c
    jumble.c(38) : error C2143: syntax error : missing ';' before 'type'
    jumble.c(45) : error C2065: 'cur_jumb_word' : undeclared identifier
    jumble.c(45) : error C2109: subscript requires array or pointer type
    jumble.c(46) : error C2065: 'cur_jumb_word' : undeclared identifier
    jumble.c(46) : error C2109: subscript requires array or pointer type
    jumble.c(47) : error C2065: 'cur_jumb_word' : undeclared identifier
    jumble.c(47) : error C2109: subscript requires array or pointer type
    jumble.c(47) : warning C4047: 'function' : 'char *' differs in levels of indirec
    tion from 'char **'
    jumble.c(47) : warning C4024: 'RecursivePermute' : different types for formal an
    d actual parameter 1
    jumble.c(47) : warning C4047: 'function' : 'char **' differs in levels of indire
    ction from 'int'
    jumble.c(47) : warning C4024: 'RecursivePermute' : different types for formal an
    d actual parameter 2
    jumble.c(47) : error C2198: 'RecursivePermute' : too few arguments for call

    C:\Users\Sean\Documents>
     
    Here is the source code:

    Code (Text):

    #include <stdio.h>
    #include <stdlib.h>

    #define MAX_STRING_LENGTH 256
    #define NOT_FOUND -1

    void RecursivePermute(char str[], char** pList, int length, int k);
    void ExchangeCharacters(char str[], int i, int j);
    int BinarySearch(char** pList, int n, char TARGET[]);
    char** readList(char* fileName, int* wordsCount);
    void printList(char** pList, int length);
    void freeList(char** pList, int length);

    int main(void)
    {
       char dict_file_name[50];
       char** dict_pointer_list;
       int dict_words;
       FILE* jumb_file;
       int jumb_words;
       int index;
       
       puts("Enter the file name of your dictionary.");
       gets(dict_file_name);
       
       dict_pointer_list = readList(dict_file_name, &dict_words);
       
       jumb_file = fopen("jumble.txt", "r");
       fscanf(jumb_file, "%d", &jumb_words);
       
       char cur_jumb_word[30][jumb_words];
       
       // Loop through each word, finding each permutation for that word, and a
       // binary search for each permutation. Print out to the screen for each
       // permutation that is a valid word.
       for (index = 0; index < jumb_words; index++)
       {
          fscanf(jumb_file, "%s", cur_jumb_word[index]);
          printf("\nJumble %d: A permutation of %s that is a valid word is ", index + 1, cur_jumb_word[index]);
          RecursivePermute(cur_jumb_word[index], dict_pointer_list, dict_words, 0);
       }
       
       // Close the jumble.txt file, and free the memory allocated to dictionary list
       // because we are done with them.
       fclose(jumb_file);
       freeList(dict_pointer_list, dict_words);
       
       puts("\n");
       system("PAUSE");
       return 0;
    }

    // Pre-condition: str is a valid C String, pList is a valid pointer
    //                to an array of pointers, length is amount of words
    //                in pList, and k is non-negative and less than or equal
    //                to the length of str.
    // Post-condition: All of the permutations of str with the first k
    //                 characters fixed in their original positions are
    //                 found. Namely, if n is the length of str, then
    //                 (n-k)! permutations are found. For each permutation
    //                 a binary search is performed on the dictionary.
    void RecursivePermute(char str[], char** pList, int length, int k)
    {
       int index, j;
       
       if (k == strlen(str))
       {
          index = BinarySearch(pList, length, str);
          if (index == NOT_FOUND);
          else
             printf("%s. ", pList[index]);
       }
         
       // Loop through each possible starting letter for index k,
       // the first index for which we have a choice.
       for (j=k; j<strlen(str); j++)
       {
          // Place the character stored in index j in location k.
          ExchangeCharacters(str, k, j);
                 
          // Find all of the permutations with that character
          // just chosen above fixed.
          RecursivePermute(str, pList, length, k+1);
                 
          // Put the original character that used to be there back
          // in its place.
          ExchangeCharacters(str, j, k);
       }
    }

    // Pre-condition: str is a valid C String and i and j are valid indexes
    //                to that string.
    // Post-condition: The characters at index i and j will be swapped in
    //                 str.
    void ExchangeCharacters(char str[], int i, int j)
    {
       char temp = str[i];
       str[i] = str[j];
       str[j] = temp;
    }

    // Pre-condition: pList is a pointer to an array of pointers sorted in ascending
    //                order of size greater than or equal to n.
    // Post-condition: If TARGET is stored in pList in an index in between 0 and n-1,
    //                 then an index in which TARGET is stored is returned.
    //                 Otherwise, -1 is returned.
    int BinarySearch(char** pList, int n, char TARGET[])
    {
       // Set the left and rightmost boundaries in which to search.  
       int L = 0, Mid, R = n-1;
       
       // Keep on looking so long as there is a search space.
       while (L <= R)
       {
          // Find the element in the middle of the search space.
          Mid = (L+R)/2;
               
          // We got a direct hit.
          if (strcmp(TARGET, pList[Mid]) == 0)
          {
             return Mid;
          }
           
          // We know that if TARGET is in pList, then it will be located
          // to the right of pList[Mid].
          else if (strcmp(TARGET, pList[Mid]) > 0)
          {
             L = Mid + 1;
          }
           
          // We know that if TARGET is in pList, then it will be located
          // to the left of pList[Mid].
          else
          {
             R = Mid - 1;
          }
       }
       
        return NOT_FOUND;
    }

    // Pre-conditions: fileName is the name of a valid file with the proper
    //                 format, and wordsCount is a pointer to an integer that will
    //                 store the number of words read in.
    // Post-condition: A pointer to an array of pointers storing all the words in
    //                 filename will be returned.
    char** readList(char* fileName, int* wordsCount)
    {
       char** pList = NULL;
       char word[MAX_STRING_LENGTH];
       int i, wordLength;
     
       FILE* pFile = fopen(fileName, "r");   // open file.
     
       if (pFile != NULL)
       {
          // read the 1st line, get to know how many words in the dictionary.
          fscanf(pFile, "%d", wordsCount);

          /* Here we allocate space a pointer for each word in the list.
          Note that the space for the words themselves is NOT allocated here. */
          pList = (char **)malloc(*wordsCount * sizeof(char *));

          for (i = 0; i < *wordsCount; i++)
          {
             // read in this word.  
             fscanf(pFile, "%s", word);    
         
             // Allocate one extra spot for the null character.
             wordLength = strlen(word) + 1;
         
             // Allocate space for this individual word.
             pList[i] = (char *)malloc(wordLength);  
         
             // copy the word to the memory block.
             strcpy(pList[i], word);    
          }
       
          fclose(pFile);    // close file.
       }
     
       return pList;
    }

    // Pre-condition: pList is a pointer to an array of pointers, each of which
    //                already stores a string. length is the number of strings
    //                stored in the structure.
    // Post-condition: All of the strings stored in pList will be printed.
    void printList(char** pList, int length)
    {    
       int i;
       
       printf("%d\n", length);
       // Just loop through each pointer and print the appropriate string.
       for (i=0; i<length; i++)
          puts(pList[i]);
    }
         
    // Pre-condition: pList is a pointer to an array of pointers, each of which
    //                already stores a string. length is the number of strings
    //                stored in the structure.
    // Post-condition: The memory for the structure pointed to by pList will be
    //                 freed.
    void freeList(char** pList, int length)
    {
       int i;
         
       // Free the memory for each individual word.
       for (i=0; i<length; i++)
          free(pList[i]);
             
       // Free the pointers to each word.
       free(pList);
    }
     
     
  15. Feb 16, 2009 #14

    Hurkyl

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    Visual studio doesn't (appear to) support the C99 feature of allowing you to mix declarations and code.

    i.e. the following is illegal in C89:

    Code (Text):

    {
      int foo;
      foo = 1;
      int bar;
    }
     
    because it contains a declaration that occurs after executable code. The C99 standard eliminated this restriction, but as far as I can tell, the visual studio C compiler does not support this feature.

    (But, apparently, other compilers that you can use with visual studio do support this feature)
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: .C files in VC++ 2008 Express Edition
  1. Visual basic .net 2008 (Replies: 0)

  2. Fortran 2008 (Replies: 1)

Loading...