1

why is this error coming implicit declaration of function 'scanf_s'. scanf is not working in visual studio even after writing scanf_s instead of scanf?.

#include<stdio.h>
int main()
{
    printf("hello\n");
    int a;
    scanf_s("%d", &a);
    printf("\n%d",a);
    return 0;
}

enter image description here

Weather Vane
  • 33,872
  • 7
  • 36
  • 56
Rahul Rex
  • 11
  • 2
  • Typo anyway - `scanf_s("%d, &a");` should be `scanf_s("%d", &a);`. Aside: the newline usage is haphazard - add `\n` *after* "hello" and move the other one too, like `"%d\n"`. – Weather Vane Feb 04 '22 at 18:08
  • Are you building this for a normal Microsoft Windows console environment? – Ian Abbott Feb 04 '22 at 18:12
  • The implication is that `scanf()` is unknown too. – Weather Vane Feb 04 '22 at 18:12
  • @WeatherVane sorry i wrote the wrong code but it still showing the same problem – Rahul Rex Feb 04 '22 at 19:14
  • 1
    Presumably you need to set the "include" paths somewhere so that the compiler can find the header files (and the linker can find the library files). Does the program compile when you use `scanf()`? – Weather Vane Feb 04 '22 at 19:18
  • @WeatherVane when using scanf() its just showing [Running] cd "c:\Users\ASUS\Desktop\C lang\" && gcc first.c -o first && "c:\Users\ASUS\Desktop\C lang\"first but it is not stopping or exiting, just showing running. i have seen people getting not secure error ,but i didn't get it. only showing the RUNNING part – Rahul Rex Feb 04 '22 at 19:29
  • In that case the compiler found the standard function `scanf()` but does not support `scanf_s()`. – Weather Vane Feb 04 '22 at 19:31
  • @WeatherVane what to do now? – Rahul Rex Feb 04 '22 at 19:32
  • 1
    The reason your code is still running is because it is waiting for you to input a value. – Weather Vane Feb 04 '22 at 19:32
  • @WeatherVane without a prompt?. i wrote the prompt for the question – Rahul Rex Feb 04 '22 at 19:34
  • Try adding `fflush(stdout)` after the prompt. Are you still usign the version without a newline output? – Weather Vane Feb 04 '22 at 19:34
  • @WeatherVane i am very new to programing. needed a compiler to study c but its been nightmare – Rahul Rex Feb 04 '22 at 19:37
  • @using fflush(stdout); just prints the prompt is there any solution; – Rahul Rex Feb 04 '22 at 19:42
  • @IanAbbott i need this code to submit assignment – Rahul Rex Feb 04 '22 at 19:49
  • 1
    That was what you asked for. Now enter the numeric value for the `scanf` to accept. Please see [The Definitive C Book Guide and List](https://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list) – Weather Vane Feb 04 '22 at 19:49
  • @WeatherVane you don't understand, i cant enter value – Rahul Rex Feb 04 '22 at 20:02
  • 1
    Add another `fflush(stdout);` and `getchar();` before `return;` so that the console remains open so you can read the output. Aside: don't do this `printf("\n%d",a);` it's almost never what you need. Instead do this `printf("%d\n",a);` – Weather Vane Feb 04 '22 at 20:26
  • 1
    Just use `scanf()`. `scanf_s()` is an **optional** function that's Microsoft-only for real-world purposes, and it's no safer than the standard `scanf()` ([Microsoft **originated** the term "FUD"](https://en.wikipedia.org/wiki/Fear,_uncertainty,_and_doubt), after all...), `scanf()` hasn't been "deprecated" by anyone but Microsoft. See http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1967.htm – Andrew Henle Feb 05 '22 at 14:46

0 Answers0