7 Replies - 437 Views - Last Post: 10 February 2019 - 05:15 PM

#1 aemaeth   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 52
  • Joined: 10-February 17

Does MS Visual Studio suck for writing C code?

Posted 10 February 2019 - 02:14 AM

I have this code that works perfectly in this compiler my teacher provided called "Programmers Notepad", but I wanted to write my code in VisualStudio but everytime I do, the same exact code (which compiles perfectly on PN) throws so many errors. Can anyone tell me why this happens? I made a new project and renamed the .cpp file to .c but its so darn annoying. Does VS just suck for writing C code?

Posted Image

#include <stdio.h>   // scanf(), fscanf(), fprintf(), fopen()
#include <stdlib.h>  // atoi(), getenv()

#define  LINELEN  64

int main(int argc, char *argv[])
{
   int cols = 3; // default value for columns is 3.
   int prec = 13; // default value for precision is 13 decimal places.
   int colWidth = 18; // 13 for decimal, 5 for integer = 18 
   int count =  0; // keep track of length of input stream, i.e. "10 55 13 666 77" has length 5.
   double input; // the input stream typed by input by user
   char oneLine[LINELEN];
   
   // Override the default value with a value from the configuration file.
   // open the configuration file
   FILE * fp;
   if ( (fp = fopen("filter.cfg", "r")) != NULL )
   {
      // read the first line of the config file
      // where oneLine is BUFFER
      // where LINELEN is sizeof BUFFER
      // where fp is stream
      
      if( fgets(oneLine, LINELEN, fp) != NULL) // first IF gets line 1 of .cfg file
      {
         // get an operand from the config file
         prec = atoi(oneLine);
         // 5+prec adds 5 spaces to precision so the column spacing is not disturbed when a prec parameter is passed
         colWidth = 5+prec; 
      }
      if( fgets(oneLine, LINELEN, fp) != NULL) // second IF gets line 2 of .cfg file
      {
      cols = atoi(oneLine);
      }
      
   }
   
   // Override the default value with an environment variable value.
   char *opPrec = getenv( "Precision" );
   char *opCols = getenv( "Columns" );
   if (opPrec != NULL)
   {  // get an operand from the environment
      prec = atoi(opPrec);
      // 5+prec adds 5 spaces to precision so the column spacing is not disturbed when a prec parameter is passed
      colWidth = 5+prec;
   }
   if(opCols != NULL) 
   {  // get an operand from the environment
      cols = atoi(opCols);
   }
   
   // Get a command line argument (if it exists) to set precision. If it's not there, do nothing and just use default precision (13)
   if (argc > 1)
   {  // get an operand from the command line
      prec = atoi(argv[1]);
      // 5+prec adds 5 spaces to precision so the column spacing is not disturbed when a prec parameter is passed
      colWidth = 5+prec;
   }
   if (argc > 2) 
     {
      cols = atoi(argv[2]);
     }
   
   // User gets prompted to enter input
   printf("Enter input stream numbers OR pipe source.exe into filter.exe: \n");
      
   // While loop spits output and formats it
   while (scanf("%lf", &input) != EOF)
     {
   
   // this keeps 2 spaces ONLY after the 1st column
   if(count >= 1)
     {
      printf("  ");
     } 
   
   // Asterisk(*) in format string keeps precision dynamic and column width. Default value for precision is 13 and column width is 18.
      printf("%*.*f", colWidth,prec, input);
      count++;

   // When counter 'count' == cols, \n is output to format columns per the col parameter passed to function
   if (count == cols) 
     {
      printf("\n");
      count = 0;
     }
   }
      return 0;
}



Is This A Good Question/Topic? 0
  • +

Replies To: Does MS Visual Studio suck for writing C code?

#2 Salem_c   User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 2328
  • View blog
  • Posts: 4,437
  • Joined: 30-May 10

Re: Does MS Visual Studio suck for writing C code?

Posted 10 February 2019 - 02:31 AM

https://stackoverflo...-default-in-vis

You can apply this
- within the current file
- within the current project
- within every project you've ever created
- within every project you're going to create
Was This Post Helpful? 0
  • +
  • -

#3 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 6825
  • View blog
  • Posts: 23,203
  • Joined: 05-May 12

Re: Does MS Visual Studio suck for writing C code?

Posted 10 February 2019 - 06:55 AM

That is Microsoft's attempt at trying to get people to write more secure code since the attempts at "educating" developers about unsafe practices of not validating buffer lengths was not working. Alas, it tends to cause more annoyance than make people stop and ask: why am I getting this error message?
Was This Post Helpful? 0
  • +
  • -

#4 german-one   User is offline

  • New D.I.C Head
  • member icon

Reputation: 14
  • View blog
  • Posts: 40
  • Joined: 05-August 18

Re: Does MS Visual Studio suck for writing C code?

Posted 10 February 2019 - 07:57 AM

As Salem_c said, your current failures can be easily avoided. And as Skydiver said they want to force you to use Microsoft-specific functions for security reasons (which still isn't fun if you try to learn C according to the standard). But ...

View Postaemaeth, on 10 February 2019 - 02:14 AM, said:

Does VS just suck for writing C code?
Yes. All default settings are for C++ rather than C. It's as you mentioned, if a file has extension .c then its content will be compiled as C code. To force VC++ to compile as C you have to specify option /TC (see https://docs.microso...pe?view=vs-2017). But this still doesn't help if you need support for C stuff that is part of the recent C standard but not part of the recent C++ standard. E.g. C++ threads and atomic types are supported (<thread>, <atomic>) while you won't find <threads.h> and <stdatomic.h> for C.
Was This Post Helpful? 0
  • +
  • -

#5 Salem_c   User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 2328
  • View blog
  • Posts: 4,437
  • Joined: 30-May 10

Re: Does MS Visual Studio suck for writing C code?

Posted 10 February 2019 - 08:44 AM

First of all, the functions are NOT deprecated. Only ISO/IEC JTC 1/SC 22 can deprecate parts of the standard C library (which they've done so for gets()).

Second, it's an example of M$ vendor lock-in. You use those functions, and your code is no longer portable.

Third, does the M$ compiler actually check scanf_s parameters for sanity and completeness (in the same way that GCC/Clang do)?
For example, are there any diagnostics if you do something like this?
float f;
char buff[10];
// f is the wrong type for %d
// buff is not 100 bytes long
result = scanf_s( "%d %s", &f, buff, 100 );



If there are no diagnostics at all, then the _s functions are no more safe than the standard ones.
If you don't know what you're doing, you're screwed anyway.
In fact, the _s might just lure you into a false sense of security.
Was This Post Helpful? 2
  • +
  • -

#6 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 6825
  • View blog
  • Posts: 23,203
  • Joined: 05-May 12

Re: Does MS Visual Studio suck for writing C code?

Posted 10 February 2019 - 02:26 PM

Sort of. It does a runtime check to ensure enough space is available. See docs.
Was This Post Helpful? 0
  • +
  • -

#7 jimblumberg   User is online

  • member icon

Reputation: 5703
  • View blog
  • Posts: 17,481
  • Joined: 25-December 09

Re: Does MS Visual Studio suck for writing C code?

Posted 10 February 2019 - 04:46 PM

But looking at Microsoft's documentation for scanf_s() and the C11 draft standard (ISO/IEC 9899:201x Committee Draft — December 2, 2010 N1548) there are differences that make Microsoft's implementation non-compliant to the C standard.

From Microsoft documentation linked above:

Quote

Note

The size parameter is of type unsigned, not size_t. Use a static cast to convert a size_t value to unsigned for 64-bit build configurations.


Where as the C standard states that the size parameter is a rsize_t which is the same as a size_t.

Quote

K.3.5.3.2 The fscanf_s function

4 The fscanf_s function is equivalent to fscanf except that the c, s, and [ conversion
specifiers apply to a pair of arguments (unless assignment suppression is indicated by a
*). The first of these arguments is the same as for fscanf. That argument is
immediately followed in the argument list by the second argument, which has type
rsize_t and gives the number of elements in the array pointed to by the first argument
of the pair. If the first argument points to a scalar object, it is considered to be an array of
one element.380)
5 A matching failure occurs if the number of elements in a receiving object is insufficient to
hold the converted input (including any trailing null character).

380) If the format is known at translation time, an implementation may issue a diagnostic for any argument
used to store the result from a c, s, or [ conversion specifier if that argument is not followed by an
argument of a type compatible with rsize_t. A limited amount of checking may be done if even if
the format is not known at translation time. For example, an implementation may issue a diagnostic
for each argument after format that has of type pointer to one of char, signed char,
unsigned char, or void that is not followed by an argument of a type compatible with
rsize_t. The diagnostic could warn that unless the pointer is being used with a conversion specifier
using the hh length modifier, a length argument must follow the pointer argument. Another useful
diagnostic could flag any non-pointer argument following format that did not have a type
compatible with rsize_t.


Quote

K.3.7 String handling <string.h>
1. The header <string.h> defines two types.
2. The types are
errno_t
which is type int; and
rsize_t
which is the type size_t.



By the way since Microsoft C doesn't actually support anything other that C90, it's not really a big surprise that there may be differences with the C11 standard.

Jim
Was This Post Helpful? 1
  • +
  • -

#8 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 6825
  • View blog
  • Posts: 23,203
  • Joined: 05-May 12

Re: Does MS Visual Studio suck for writing C code?

Posted 10 February 2019 - 05:15 PM

Good point! The background story is kind of hazy for me, so don't quote me. My understanding is that MS created it's own implementation while the standard was still in the initial stages of being drafted in the hopes of becoming the de facto standard by simply being first to market. Throw in a delayed Visual Studio release due to a major re-org, expending a lot of political capital trying to get internal traction for the Windows, the Office orgs, as well as external 3rd Party driver writers, to take several weeks to go change all existing code to compile cleanly against the "safe" functions along with the needed code reviews, and the committee dragging it's feet, then we get the result where MS is at now. By the time a standard was finalized, DevDiv had already used up all of its goodwill to force the other divisions and external 3rd Party driver writers to do yet another pass changing and reviewing code.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1