Trying to use Visual Studio 2015 to do "C" programming

Bubbleawsome

Diamond Member
Apr 14, 2013
4,834
1,204
146
In my PC programming class we're learning straight up C. Whether it's a good start or not doesn't really matter to me at the moment, but I can't get anything C to run in visual studio. I know it's built for a language that wasn't obsolete 20 years ago, but I need to make it work. The program we usually use for writing, compiling, and then running C is DevC++, but the last update to that program was nearly 20 years ago and I can't get it to work on windows 10.

An example of code that runs in DevC++ but not visual studio is this;
Code:
//Calculates the area of a rectangle
#include <stdio.h>
#include <stdlib.h>

int main(void)
{
	unsigned int length, width, area, perimeter;
	printf("Enter length and width of the rectangle\n"); //prompt
	scanf("%d%d", &length, &width);
	area = length * width;
	perimeter = length + length + width + width;
	printf("Area is %d\n", area);
	printf("Perimeter is %d\n\n", perimeter);
	system("pause");
	return 0;
}

All I get from visual studio is this;
Code:
1>------ Rebuild All started: Project: AAAAAA, Configuration: Debug Win32 ------
1>  Source.c
1>  *namewashere.c
1>c:\users\*names*\documents\visual studio 2015\projects\aaaaaa\aaaaaa\doublederp.c(9): error C4996: 'scanf': This function or variable may be unsafe. Consider using scanf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
1>  c:\program files (x86)\windows kits\10\include\10.0.10240.0\ucrt\stdio.h(1270): note: see declaration of 'scanf'
1>  Generating Code...
========== Rebuild All: 0 succeeded, 1 failed, 0 skipped ==========

and then when the program fails;
Y4gCBmL.png


any and all help would be appreciated.
 

LevelSea

Senior member
Jan 29, 2013
942
53
91
The error message is telling you exactly what's wrong.
Code:
doublederp.c(9): error C4996: 'scanf': This function or variable may be unsafe. Consider using scanf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
scanf is unsafe. In order to use unsafe function, you need to use _CRT_SECURE_NO_WARNINGS. Got into your project properties (right click on the solution, Properties), Configuration Properties, C/C++, Preprocessor, and add _CRT_SECURE_NO_WARNINGS to your Preprocessor definitions.

Or use scanf_s instead of scanf.

Here's a little background on why scanf is unsafe.
http://c-faq.com/stdio/scanfprobs.html
 
Last edited:

Merad

Platinum Member
May 31, 2010
2,586
19
81
C is not an obsolete language. I'm not sure what gave you that impression. However, Visual Studio's support for C is not the best, because MSFT has long pushed people to use C++ over C. Given your professor's use of Dev-C++ though (rather terrifying, TBH) VS probably supports everything you'll need to use.
 

purbeast0

No Lifer
Sep 13, 2001
53,449
6,296
126
the solution to your problem is literally in the error message that visual studio spit out to you and that you posted in your OP.
 

Ken g6

Programming Moderator, Elite Member
Moderator
Dec 11, 1999
16,560
4,473
75
the solution to your problem is literally in the error message that visual studio spit out to you and that you posted in your OP.

Well, half the solution is there. The error message doesn't explain how to "use _CRT_SECURE_NO_WARNINGS" - namely, #define it or have the compiler define it. It also doesn't explain how to use scanf_s.

Last time I used gcc for this kind of thing, it issued a warning, not an error, for "gets", and suggested "fgets". If you're looking for a cross-platform solution, that's what I'd suggest: fgets from stdin to read a string into a buffer, and sscanf (string scanf) to parse it if necessary. But also be aware that fgets adds a newline to the end of whatever it reads, so make sure your buffer size and any other code dealing with the buffer's contents account for that.