The following C program doesn't printing anything on the screen.
I compiled the program with gcc:
#include<stdio.h>
main()
{
printf("hai");
for(;;);
}
Most likely, stdout is line buffered. Your program does not call fflush or send a newline so the buffer does not get written out.
#include <stdio.h>
int main(void) {
printf("hai\n");
for(;;)
;
return 0;
}
See also question 12.4 and What's the correct declaration of main()? in the C FAQ.
This is caused by the buffering which takes place in stdio (i.e. it is not output immediately unless you tell it to by including a \n or fflush). Please refer to Write to stdout and printf output not interleaved which explains this.
(p.s. or the compiler is not happy about the typo in #include)
Standard output tends to be line buffered by default so the reason you're not seeing anything is because you haven't flushed the line.
This will work:
#include <stdio.h>
int main (int argC, char *argV[])
{
printf("hai\n");
for(;;)
;
return 0;
}
Alternatively, you could fflush standard out or just get rid of the infinite loop so the program exits:
#include <stdio.h>
int main (int argC, char *argV[])
{
printf("hai");
return 0;
}
but you probably want the newline there anyway.
Your for(;;) loop stops the stream from being flushed. As others have suggested, add a newline to the string being output, or flush the stream explicitly:
fflush( stdout );
after your printf. And correct the spelling of #include.
Related
So here is my code:
#include <stdio.h>
typedef struct dat{
int broj;
int sir;
}sir;
int main() {
sir sir;
FILE* fordat;
int i=0;
fordat = fopen("dattk.txt","w+");
while(i<100) {
i++;
sir.broj = i;
sir.sir=-i;
fprintf(fordat,"%d %d", sir.broj, sir.sir);
}
// fseek(fordat,0,SEEK_SET);
//rewind(fordat);
// fscanf(fordat,"%d %d",sir.broj,sir.sir);
printf("%d% %d",sir.broj,c);
fclose(fordat);
return 0;
}
I am studying in C and made this simple example program of writing and reading from a file...
If I remove the comments from either fseek or rewind or fscanf, the program runs.
However, if I remove the comments from fseek AND fscanf the program compiles, but crashes on run.
Can't figure out why...
Your fscanf call is broken - change:
fscanf(fordat,"%d %d",sir.broj,sir.sir);
to:
fscanf(fordat,"%d %d",&sir.broj,&sir.sir);
Important: if you had compiled with warnings enabled (e.g. gcc -Wall ...) then the compiler would have helpfully pointed out this mistake to you, thereby saving you time and effort. Always enable compiler warnings and take heed of them.
And one more thing: you have absolutely no error checking in your code - you should check for failure after fopen and all other calls which might potentially fail.
Today , When i coding, met a question..my Code as follow:
#include<stdlib.h>
void main()
{
system("dir");
getch();
}
The question : The user Screen is nothing..Why ? where is my result?
If you want the output when using system, at least into something you can read in your application, you need to pipe the output:
system("dir > /tmp/output.txt");
FILE *f = fopen("/tmp/output.txt", "r");
char text[1024]; // max sizeof of 1 kb, any more and I'd consider using `malloc()` instead.
fread(text, 1, 1024, f);
printf("%s\n", text);
fclose(f);
There are some problems in your program, at least one of which has already been mentioned.
void main() should be int main(void).
As I recall, the Windows/DOS getch function is declared in <conio.h>; you should have a #include directive for it. Be aware that both <conio.h> and getch are non-standard.
Since main returns int, you should return an int result.
But none of these problems explain the problem you're seeing.
With these changes:
#include <stdlib.h>
#include <conio.h>
int main(void)
{
system("dir");
getch();
return 0;
}
This should work; it should show a directory listing of whatever directory your program runs in (which is determined by TC; I don't know the details).
It's possible that the program is running in an empty directory, which means the dir command wouldn't show any files, but it should still produce some output.
Try commenting out the system() call and adding a printf call (note the added #include <stdio.h>):
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
int main(void)
{
printf("Hello, world\n");
getch();
return 0;
}
This should open a console window, print "Hello, world" in it, and wait for you to type Enter.
If you still don't see any output (either no console window, or a console window with nothing in it), then you have a problem that's not related to the system() call. Most likely the problem has to do with the way you're using Turbo C (I presume that's what "TC" stands for).
The main function in every C program is supposed to return an int you are returning void
Change void to int:
#include<stdlib.h>
int main()
{
system("dir");
getch();
}
When I tested, the dir command ran in my console and printed to standard out.
May be he is the running the program directly in the Turbo C IDE and hence his output is not visible. If he runs the program directly from cmd line it works. I remember you need to run Alt - F5 or some other combination to see the output window in Turbo C++
I'm trying to force my C program to line buffer (stdout is going to be captured by a java program), but it always seems to fully buffer instead. Here is some sample code:
#include <stdio.h>
#include <stdlib.h>
int main(){
char c;
setvbuf(stdout, NULL, _IOLBF, BUFSIZ);
printf("Hello world\n");
c = getchar();
printf("got char: %c\n", c);
}
If I specify _IOLBF or _IOFBF, then I don't see an output until I input a char. Only if I use _IONBF will I see output before the getchar(). Shouldn't _IOLBF do the same since "Hello World\n" contains a '\n'?
I am using visual c++ 2005.
Thanks
According to this Microsoft documentation:
_IOLBF:
For some systems, this provides line buffering. However, for Win32, the behavior is the same as _IOFBF - Full Buffering.
for example there is code
#include <algorithm>
#include <stdio.h>
#include <iostream>
int intcomp(int *x,int *y) { return *x-*y;};
int a[10000];
int main(void){
int i; int n=0;
while (scanf("%d",&a[n])!=EOF)
n++;
qsort(a,n,sizeof(int),intcomp);
for (int i=0;i<n;i++)
printf("%d\n",a[i]);
return 0;
}
how tell computer that EOF is reached?
You mean when entering input interactively?
In a windows shell, ctrl+z on a line on its own. In a *nix shell, ctrl+d. Or just put your input in a file and pipe it, then not only will eof be detected at the appropriate time but also you can automate your testing.
You should use CTRL+Z combination (or somehow input character with code 26, for example, by pressing ALT+2+6 on additional keyboard)
The following code outputs "Illegal seek":
#include <stdio.h>
#include <errno.h>
#include <string.h>
int main() {
errno = 0;
getchar();
getchar();
getchar();
ftell( stdin );
printf( "%s\n", strerror(errno) );
}
This occurs when I run cat script | ./a.out as well as when I just run ./a.out. The problem is with ftell, of course. My question is: why does this occur? I would think stdin can be seekable. fseek also causes the same error. If stdin is not seekable, is there some way I can do the same sort of thing?
Thank you for your replies.
Fifos aren't seekable. They are simply a buffer. Once data has been read() from a fifo buffer, it can never be retrieved.
Note that if you ran your program:
./a.out < script
then standard input would be a file and not a fifo, so ftell() will then do what you expect.