1

While writing a simple C Program i encountered the problem that "printf" doesn't generate any outcome. Code:

#include<stdio.h>
int main()
{
printf("Hello World\n");
}

Cygwin log after compiling and running:

MMGV@Philipp /cygdrive/c/users/MMGV/Desktop/Programme
$ gcc test.c -o test.exe

MMGV@Philipp /cygdrive/c/users/MMGV/Desktop/Programme
$ test.exe

MMGV@Philipp /cygdrive/c/users/MMGV/Desktop/Programme
$

No error message, simply nothing. Opening the generated .exe in the Windows GUI does not work either. Thanks for any help!

8
  • 1
    try adding \n after your message. Buffers may not be flushed if no end of line. Commented Nov 4, 2016 at 19:41
  • did not work :/ Commented Nov 4, 2016 at 19:45
  • 1
    A newline should be there in any case. If you can confirm that the problem still occurs with the newline, please add it to the code in the question. How exactly did you try to run the program? Did you type something like ./hello at a shell prompt? Did you double-click on the .exe file in the Windows GUI? Commented Nov 4, 2016 at 19:47
  • I do not know about cygwin, but usually in Windows if you run a program with console output it will open the console, print the message, then close the console before you can even know it has happened. Solution: add getchar() at the end of the program, or run it from a console window. Commented Nov 4, 2016 at 19:51
  • 1
    "Long story short,"? I think you rather made a short story long! "Did not work" is not a helpful diagnostic. I suggest that you edit your question, remove the unnecessary back story and apologies and other waffle, and post the exact code (including the \n advised by Jean-F), the command line you have used to build it, all the compiler output generated during build, and the command line you have used to run the code - a verbatim log of your entire Cygwin session in short. Commented Nov 4, 2016 at 19:52

3 Answers 3

2

Change your program to

#include<stdio.h>
int main()
{
    printf("Hello World\n");           // add newline
    getchar();                         // wait for an input (newline)
}

Because in Windows, the console window probably closes before you have a chance to see it.

Sign up to request clarification or add additional context in comments.

2 Comments

Does work for the Windows GUI, thanks. But why do i still see nothing in Cygwin?
cygwin is unknown to me. I just use MSVC from console, not even the IDE.
2

You are not running your program. Cygwin shell by default doesn't have current directory in the path. test.exe is resolved to a standard Unix-like utility (try which test). You need to specify current directory explicity:

$./test.exe 

Comments

0

This worked!

#include <stdio.h>
#include <stdlib.h>

int main(void){
  printf("Hello World.\n");
  return EXIT_SUCCESS;
}

cmd:

HP@LAPTOP-VUS0RJO0 /cygdrive/c/cygwin64/home/shri
$ gcc -o first first.c

HP@LAPTOP-VUS0RJO0 /cygdrive/c/cygwin64/home/shri
$ ./first.exe
Hello World.

HP@LAPTOP-VUS0RJO0 /cygdrive/c/cygwin64/home/shri
$

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.