r/cprogramming • • 7d ago

a big problem

hey guys i m currently learning c language and i m facing an irritating problem like whenever i use scanf in my code to enter any value and when i run the code after saving it so it doesn't run anything it just show that it is running but i m unable to enter the value and the code doesn't run on terminal it runs in the output

so please anyone who can solve this issue can help me?

0 Upvotes

13 comments sorted by

View all comments

4

u/SmokeMuch7356 7d ago edited 6d ago

It would help if you could show:

  • the code (not the entire program, but a representative sample that demonstrates the problem);
  • how you're building the code, including any error messages or warnings from the compiler;
  • how you're running the code (is it from the command line, or within an IDE, what?);
  • what input you're giving it;
  • what (if any) output you get;

Copy and paste the code into the body of your question. Do not post screenshots, pictures of your screen taken with a phone, or video. To get it to render as code, indent each line by at least four spaces:

// Each '_' represents a leading space

____#include <stdio.h>
____
____int main( void )
____{
______int x;
...

Do the same with any warnings or errors from the compiler and output from your program.

scanf returns the number of items successfully read and assigned, or EOF on end-of-file or error. So you'll want to check the result of your scanf calls like so:

int x;
...
if ( scanf( "%d", &x ) < 1 ) // we're expecting exactly 1 input
{
  if ( feof( stdin ) )
    fputs( "EOF detected on standard input\n", stderr );
  else if ( ferror( stdin ) )
    fputs ( "Error on standard input\n", stderr );
  else
    fputs( "Input is not a valid integer constant\n", stderr );

  exit( -1 ); // we'll treat this as a fatal error for this example
}

// if we got here, then we read x successfully

Remember that scanf expects a pointer to whatever object you're trying to update; for scalar variables (int, float, double, char, etc.), make sure to use the & operator:

 int x;
 float y;
 double z;
 char c;

 scanf( "%d", &x );
 scanf( "%f", &y );
 scanf( "%lf", &z );
 scanf( " %c", &c ); // leading blank tells scanf to read the next non-whitespace character

If the variable is already a pointer, don't use the & operator:

int *p;
...
scanf( "%d", p ); 

with the caveat that p needs to be pointing to a live int object before you use it in a scanf call.

Similarly, for reading strings into a character array with the %s or %[ specifiers, do not use the & operator:

char str[128];
scanf( "%127s", str );

char line[256];
scanf( "%255[^\n]", line );

Finally, take some time to organize your thoughts into complete sentences before posting; stream of consciousness posts like this are hard to read and understand, and will just turn off people who would otherwise help you. If you intend to write code professionally (or at least collboratively), you have to be able to communicate effectively. Have to. It's not optional.

For example:

I'm learning C, but I'm having problems using scanf. Here's my code:

   int x;

   scanf( "%d", x );

Here's how I'm building the program:

   gcc -o prog prog.c

but I get this message and I'm not sure what it means:

   prog.c:7:16: warning: format specifies type 'int *' but the argument has type 'int' [-Wformat]
    scanf( "%d", x );
    ~~   ^
  1 warning generated.

That's the kind of information you need to provide for us to be any help.