Problem z scanf , Visual Studio

0

Początkujący, prosty program

#include <stdio.h>

#define MWG 60

int main(void)
{
int minuty, gg;

printf("Podaj liczbe minut: \n");
scanf("%d", &minuty);
gg=0;
while ((minuty/MWG)>0)
{
gg+=1;
minuty-=MWG;
}
printf("%d:%d\n", gg, minuty);

return 0;
}
W Visualu wywala bład:

Błąd 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. ConsoleApplication2 C:\Users\48881\source\repos\ConsoleApplication2\ConsoleApplication2\ConsoleApplication2.cpp 14

Ostrzeżenie C6031 Ignorowana wartość zwrotna: „scanf”. ConsoleApplication2 C:\Users\48881\source\repos\ConsoleApplication2\ConsoleApplication2\ConsoleApplication2.cpp 14
Prosze o pomoc ;/ Czy to błąd Visuala czy błąd kodu?

1

Tak jak powiedział już @MarekR22 - ostrzeżenia te można zignorować.

Jednak jeśli nie chcesz zaśmiecać sobie outputu z kompilacji, możesz te ostrzeżenia wyłączyć poprzez dodanie _CRT_SECURE_NO_WARNINGS.
Możesz to zrobić:

  • w plikach, w których wykorzystujesz scanf lub printf, poprzez #define _CRT_SECURE_NO_WARNINGS albo
  • we właściwościach projektu, wśród rzeczy dodanych w polu Preprocessor Definitions: Prawym na projekt -> Properties -> C/C++ -> Preprocessor
0

Moim zdaniem, lepiej wyłączyć ten rodzaj ostrzeżenia, jak pisze @Bartosz36 . Ignorowanie ostrzeżeń to fatalny nawyk.

0

To miałem na myśli zignoruj konkretnie to ostrzeżenie. W ustawieniach kompilatora można wyłączyć pojedyncze ostrzeżenia.

Problem polega na tym, że to ostrzeżenie zachęca cię do używania scanf_s, co nie jest ci potrzebne:
scanf, fscanf, sscanf, scanf_s, fscanf_s, sscanf_s - cppreference.com

4-6) Same as (1-3), except that %c, %s, and %[ conversion specifiers each expect two arguments (the usual pointer and a value of type rsize_t indicating the size of the receiving array, which may be 1 when reading with a %c into a single char) and except that the following errors are detected at runtime and call the currently installed constraint handler function:

  • any of the arguments of pointer type is a null pointer
  • format, stream, or buffer is a null pointer
  • the number of characters that would be written by %c, %s, or %[, plus the terminating null character, would exceed the second (rsize_t) argument provided for each of those conversion specifiers
  • optionally, any other detectable error, such as unknown conversion specifier

As with all bounds-checked functions, scanf_s , fscanf_s, and sscanf_s are only guaranteed to be available if __STDC_LIB_EXT1__ is defined by the implementation and if the user defines __STDC_WANT_LIB_EXT1__ to the integer constant 1 before including stdio.h.

bo wymaga zawsze podania rozmiaru bufora docelowego, (co będzie wnerwiające), a twój przypadek nie podpada pod %c, %s lub %[.

1 użytkowników online, w tym zalogowanych: 0, gości: 1