Works with one compiler, but not with other [closed] - c

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I'm trying to code a very simple program using C language.
I'm using Visual Studio and CodeBlocks to make it compiled.
But this code:
#include <stdio.h>
#include <stdlib.h>
int main() {
float a;
printf("Give a number: \n");
scanf("%f", &a);
float b = a * 2;
printf("The result is: %f", b);
}
It works using CodeBlocks, but not with Visual Studio.
Could you please give me a hint?
Severity Code Description Project File Line Suppression State
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. Variables c:\users\PinkP\documents\visual studio 2015\projects\variables\variables.c 32

scanf("%f", &a); is considered unsafe by the Visual Studio compiler and they rather cheekily schedule the function for deprecation. (Although ISO have no such plans.) Really they want you to use scanf_s instead, as that's less likely to mess up your memory.
You can disable this warning by following the compiler message: i.e. #define _CRT_SECURE_NO_WARNINGS.
The irony here is the compiler permits your non-standard main() function without a twitch! Do consider using the more portable
int main()
which has been required since and including C99.

Visual Studio 2015 is stricter about deprecated functions than 2010 was.
Rather than just advising you that you need to do something about your use of scanf, as Visual Studio 2010 did, it now FORCES you to do something about it.
You need to switch to scanf_s or use _CRT_SECURE_NO_WARNINGS.

Related

Why scanf_s does not work anywhere other than visual studio? [duplicate]

This question already has an answer here:
Why didn't gcc (or glibc) implement _s functions?
(1 answer)
Closed 3 years ago.
When I use visual studio, it says "scanf is unsafe, try using scanf_s". But if I use scanf_s in gcc or other compilers, it doesn't work. Does scanf_s works only on visual studio? If so, why? The visual studio website says "scanf is unsafe". If it is unsafe, then why others still uses it?
As mentioned here
scanf_s is Microsoft-specific. Header is stdio.h but not in GCC.
As documented here
When reading a string with scanf, always specify a width for the %s format (for example, "%32s" instead of "%s"); otherwise, improperly formatted input can easily cause a buffer overrun.
Alternately, consider using scanf_s, _scanf_s_l, wscanf_s, _wscanf_s_l or fgets.
See more at "Why didn't gcc implement _s functions?"
pmg adds in the comments that scanf_s() is Standard C11 (optional).
That means that activating c11 with gcc might be enough.
However Shawn adds:
IIRC, Microsoft's version doesn't follow the standard.
Plus no other major C library vendor has bothered to implement Annex K, so it might as well be MS specific for all intents and purposes.
pmg confirms:
My gcc (version 6.3.0) does not recognize scanf_s() with gcc -std=c11 -pedantic ...

Visual Studio - C programming [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 4 years ago.
Improve this question
Total NOOB here.
I've noticed that under Visual Studio common C commands are a bit different,
the only example I can think of right now is that VS insists on using scanf_s instead of scanf, but I'm sure there are other quirks.
what I'm asking is, should I be learning, or even working with C on VS and why?
Thanks
MSVC does not insist but only issues warnings, to provoke you into using their own non-standard functions. The warning message from MSVC tells you what to do:
warning C4996: 'scanf': This function or variable may be unsafe. Consider using scanf_s instead. To disable deprecation, use
_CRT_SECURE_NO_WARNINGS
which means putting #define _CRT_SECURE_NO_WARNINGS before any #include files.
I put the following lines before any #include
#define _CRT_SECURE_NO_WARNINGS
#define _CRT_SECURE_NO_DEPRECATE
#define _CRT_NONSTDC_NO_DEPRECATE
#define _USE_MATH_DEFINES
#include <stdio.h> // etcetera
Anyway MSVC does not comply with C standards.

Cross Platform usage of printf_s and scanf_s functions - C (linux/win32) [duplicate]

This question already has answers here:
Disabling Warnings generated via _CRT_SECURE_NO_DEPRECATE
(10 answers)
Closed 5 years ago.
I'm currently trying to get some C code that I originally wrote for linux (gcc) to build on a win32 box.
The MSVC compiler is giving me warnings for all my printf and scanf usage suggesting I should use printf_s and scanf_s instead as a more secure alternative.
Its never nice to ignore 100's of compiler warnings, but should I in this instance?
Is there a simple workaround to fix this? Perhaps encapsulate those functions in a platform specific preprocessor directive?
You can suppress these warning by defining _CRT_SECURE_NO_DEPRECATE before the #include statements. But you should consider to use the new, secure functions.

"Declaration Not allowed here" and "Constant Expression" Error in C [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 9 years ago.
Improve this question
I used Microsoft Visual Studio to write a C code in C++ project and It's working fine there when I convert the file extension from my.cpp to my.c and tried to run in via TurboC++ 3.0 then it gives me number of errors like "Constant Expression Required" and "Declaration is not allowed here".
I tried to run my code online compiler but its not giving me these error there.
Can anyone help me with this ?
I hope it's due to C99 mode but not confirmed.
TurboC++ 3.0 Supports C99 or not ?
Note: I can't share my code directly here due to project research work , If anyone want to have a look at my code I can send you via private message , Sorty for that
The error: Declaration Not allowed here is due to the mixed type declaration of variables and the error: Constant Expression required is because of the variable length arrays.
Mixed type variables and variable length arrays are are allowed in C99 and latter. Neither MSVC nor Turbo C++ supports C99.
I tried to run my code online compiler but its not giving me these error there.
This is because almost all new (and online) C compilers support C99.

About printf and scanf [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
What is the meaning that printf and scanf commands are the part of C language, as they don't need inclusion of #include<stdio.h>.
Why aren't others part of the C language?
What is the mean that,(print f) and (scan f) commands are the part of c language
They are not "commands", but rather functions, and they aren't part of the language either.
they don't need inclusion of #include
They do. They need the headers/declarations even more than others, since they are variadic. Not providing a prototype for them will quickly lead to undefined behavior.
why don't others be the part of c language
Again, these aren't part of the language because... because they are not part of the language. They are stand-alone functions, which don't contribute to the core syntax and semantics of a C program. They aren't included in C's context-free grammar. The C standard does describe them, though - since they are part of the C standard library.
Actually, no, they are not part of the language in the way you think they are. If you call print("hello, %d", 5); it will create an implicit declaration based on the parameters you've provided and the returning type will be int.
Luckily, there is a match for this in libc which is implicitly linked to your program, and linker will be able to link your source file and the library definition of printf.
In certain IDE, it is possible that printf & scanf are used and not underlined as false while editig the source code, because of the indexer which knows these functions exists. But you will not be able to compile it. The include is not optional as the compiler itselfs doesn't know printf nor scanf.
printf() and scanf() Example programs
In layman language
printf() is a function use to display (output)
scanf() is a function used for reading any input
printf and scanf aren't part of the grammar, but they are part of the language by virtue of being in the standard library as specified by the language definition. You do need to include stdio.h to use them properly, though.

Resources