Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 4 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
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.
Improve this question
I'm starting to write a C code after a gap of almost 10 year. I was teaching my sister and unfortunately got stuck at a very basic scanf() function for a simple interest program on Ubuntu.
My program is not giving any compilation error but it's not showing anything on screen
Here is the code:
#include <stdio.h>
int main()
{
int p,n;
float r,si;
printf("enter values of p,n,r");
scanf("%d %d %f",&p,&n,&r);
si = p*n*r/100;
printf("%f" , si);
return 0;
}
This is a common "issue" when trying to printf() and scanf() after one another. The operating system buffers stdin and stdout in order to increase performance and only flushes them when necessary. You can explicitly flush stdout by calling fflush(stdout); right after printf().
See C/C++ printf() before scanf() issue.
Your code would end up looking like
#include <stdio.h>
int main()
{
int p,n;
float r,si;
printf("enter values of p,n,r\n");
fflush(stdout); // Force stdout to be flushed
scanf("%d %d %f",&p,&n,&r);
si = p*n*r/100;
printf("%f" , si);
return 0;
}
I think you are just not looking at your console properly. The output "si" is getting printed on the same line as your first printf since you do not have a newline inserted at the end of it.
When you say
not showing anything on screen
(emphasis added), I take you to mean that when you run it, it's not displaying the data-entry prompt you're printing. This would be because the standard output is line-buffered by default when connected to a terminal -- it will buffer the output in memory before printing it, until either the buffer is filled, or (because line buffered) a newline is output, or the stream is closed (including when the program terminates normally).
One way to cause the prompt to be displayed, therefore, is to append a newline to it:
printf("enter values of p,n,r\n");
Another is to use an output function that automatically appends a newline:
puts("enter values of p,n,r");
If you want to ensure that the output appears even though no newline has been sent then you can flush the stream (standard output in this case) instead:
printf("enter values of p,n,r: ");
fflush(stdout);
Your code worked perfectly for me in my Ubuntu system.This may be because of the mistake of the IDE you are using.
Try running code in Ubuntu this way:-
save file as program.c on desktop.
open terminal and type cd Desktop (navigate to desktop).
compile code by typing gcc -o program program.c .
run program by typing ./program.
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
When I try to run the following program, it waits for the name input, but just skips to the end after 2 secs after asking for moduleNumber. I dont get a chance to input anything.
Anyone know why this is happening and what should I put in place to stop this happening?
many thanks.
#include<stdio.h>
#include <string.h>
char name[30];
int moduleNumber;
int main(int argc, char **argv)
{ //read name & module amount
printf ("Please enter your name: ");
gets(name);
printf ("How many Modules: ");
gets(moduleNumber);
return 0;
}
I am going to assume that you are running this on a windows machine. The reason it would be skipping is that gets() is only consuming the newline aka \n and leaving the carriage return \m which the next gets() read because it is already in the stdin buffer.
As others have said, do not use gets()
a) it is deprecated
b) these kind of functions are a source of major security bugs and allow hacking of systems. I suggest you read about buffer overflows
Always for user input you should use functions that require the size of the input buffer
There are other problems with your code, for example passing moduleNumber not as a pointer which causes a core dump. And the fact that gets() whats a char*
When you use gets() this is what your compiler should tell you.
warning: the `gets' function is dangerous and should not be used.
So, use a more tight warning configuration.
In your case you should use somenthing like this:
fgets(name, sizeof(name), stdin); //stdin -> standard input
to get an integer from console you can simply use:
scanf("%d", &moduleNumber);
No parsing needed because you get it in the desired type as opposed to gets() which gets a char stream.
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 4 years ago.
Improve this question
I'm trying to write a very simple win32 program. I open a file for reading using CreateFile(), and then read it's content using ReadFile()
HANDLE hfile=CreatFileW(L"Capturejpg.jpg", GENERIC_READ, 0,
NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,NULL);
bReadResult=ReadFile(hFile, ReadBuff, BUFFERSIZE-1, &dwBytes, &OL);
when I'm reading a .txt file for example, this works just fine, the problem is when the file I am reading contains NULL (which is the case with most files) then ReadFile function stops reading(or maybe writing bytes to ReadBuff) when reaching the first NULL.
before asking I searched and found two answers.
use something other that char array and char *: for this, I don't know what else I can use, cause my goal is to read the file and search for the files extension(for example if it's .gif then the first 3 characters read "Gif")
change DCB: the problem with this one is that I have no idea what DCB is actually, how to change it, and change exactly what in it.
EDIT: other posts with the same problem: this one and this one
ReadFile does not care one bit about the content that it reads. It will quite happily read zero bytes and continue reading beyond that point in the file. It wouldn't be much use if it could not do that.
You have just misdiagnosed the problem. You have read into a character array ReadBuffer and then printed like this:
printf("%s", ReadBuffer);
Now, printf will indeed stop when it reaches a zero byte. You will need to find some other way to output the content of this file.
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 7 years ago.
Improve this question
I have a program, divided in multiple files and with a lot of code containing arrays, pointers, structs and what not.
To come to the point: these are my last lines of my main() method;
...
printf("\nLast line!");
return 0;
}
After I compiled this program without warnings, I executed it and in my console I get the text: Last line! in my CMD and after that the program crashes (doesn't respond).
I often use printf()'s to debug my code and determine the line in my code where the program crashes. In this case I cannot wrap my head around the fact that the program crashes after the last line of code.
My questions:
Could return 0; in the main function cause a crash?
Is it possible the program crashes due to undefined behaviour earlier in my program?
If so, how can I debug my code? (I'm using Windows 7)
Note: I know for sure there is only one printf("\nLast line!"); in my code.
could return 0; in the main() function cause a crash?
As for the statement alone, No, it won't cause a crash.
And is it possible the program crashes due to undefined behavior earlier in my program?
Yes. Most likely so. You invoked undefined behaviour somewhere earlier in your code, and the result, as you know, is undefined.
If so, how can I debug my code?
Try to run your program through a debugger and memory checker, like gdb on linux and valgrind. Also, ry to add breakpoint in your code in tricky areas and step through the debugger while checking the actual value against the expected value. Most likely, you'll be able to spot the error.
Also stop using printf(); try a good debugger like(gdb) it's much more simpler and more faster to debug C code
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
I want to concatenate lots of strings together to produce a unique string which will be long enough. Here is an example code:
char *s;
s= strconcat("A big-length string",NULL);
fprintf(stdout,"%s",s);`
When I try to print it, the string is less that is printed is smaller than the actual string.
Assume that the strconcat() function works well. Do I have to malloc() some space for s??
Also when I try strlen(s) it shows me the right number like 1078. just the printing is the matter.
EDIT
The problem solved when after all strings I try to inject \n like this
s= strconcat("A big-length string","\n",NULL);
Output using printf/fprintf is usually buffered.
If the output file is connected to a terminal, output is line buffered, so the buffer will be flushed wfter each \n.
If the output file is not connected to a terminal, output is fully buffered, so the buffer will be flushed when it is full. Typical buffer sizes is 1kb-8kb.
to ensure that the buffer is flushed call:
fflush(stdout);
You can flush all file buffers with:
fflush(NULL);
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Why does printf not flush after the call unless a newline is in the format string? (in C)
I faced this problem while doing a networking project. I was able to narrow down the issue and reproduce it like this:
If you run this code, it wont display the text on the screen. Although it displays the text if you put \n at the end of the text or use fflush() after the printf statement.
int main(){
printf("started") ;
while(1){
}
}
Can anyone please explain this behavior?
The output just doesn't get flushed to the screen without the \n.
Add fflush(stdout); after the printf and you should see the output.