.C files in VC++ 2008 Express Edition

  • C/C++
  • Thread starter Firestrider
  • Start date
  • Tags
    files
In summary: You can open C/C++ source files just like any other files with Visual Studio. Just select "File->Open->Project...->Open..." and navigate to where you want the file to be opened.
  • #1
Firestrider
104
0
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.
 
Technology news on Phys.org
  • #2
Firestrider said:
Is there anyway to open a <name>.c file and compile/run it in VC++ 2008 EE without any extra steps?
you can use the compiler on the command line with "cl blah.c"

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.
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
 
  • #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:
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?
 
  • #4
Anyone got info on this? Thanks.
 
  • #5
Would you care to show us the source?
 
  • #6
It appears to me to be saying you don't have a main() function.

Is there anything in your .c file?
 
  • #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?
 
  • #8
Firestrider said:
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?
Well, you have to run cl every time... if this is the way you're doing it...

When compile errors are found is it printed in the command prompt?

Yes.

What is the debugger for and how can I use it?

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.
 
  • #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.
 
  • #10
Firestrider said:
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.

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.
 
  • #11
shoehorn said:
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.

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.
 
  • #12
Firestrider said:
Thing is I need just .c files not .cpp files for my class and there is no option for that type of file.

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.

Firestrider said:
I would also like to open .c source code and have a one button to compile and run from the IDE.

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.
 
  • #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:
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:
#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);
}
 
  • #14
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:
{
  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)
 

1. What is a .C file?

A .C file is a source code file written in the C programming language. It contains the instructions and functions that make up a program.

2. How do I create a .C file in VC++ 2008 Express Edition?

To create a .C file in VC++ 2008 Express Edition, go to File > New > File and select "C File" from the list of options. Give the file a name and click "Add".

3. Can I use .C files in other programming languages?

Yes, .C files can be used in other programming languages, such as C++ or Objective-C, as long as the language supports the C syntax.

4. How do I compile and run a .C file in VC++ 2008 Express Edition?

To compile a .C file, go to Build > Build Solution. To run the program, go to Debug > Start Without Debugging or press F5.

5. Can I edit a .C file in a different text editor?

Yes, you can edit a .C file in any text editor as long as it supports the C syntax. However, it is recommended to use an IDE like VC++ 2008 Express Edition for easier development and debugging.

Similar threads

  • Programming and Computer Science
Replies
6
Views
8K
  • Programming and Computer Science
Replies
4
Views
3K
  • Programming and Computer Science
2
Replies
65
Views
2K
  • Programming and Computer Science
Replies
1
Views
3K
  • Programming and Computer Science
Replies
4
Views
5K
  • Programming and Computer Science
Replies
12
Views
2K
  • Programming and Computer Science
Replies
8
Views
2K
  • Programming and Computer Science
Replies
4
Views
2K
  • Programming and Computer Science
Replies
4
Views
7K
  • Programming and Computer Science
Replies
4
Views
8K
Back
Top