Ncurses No Output - c

Platform: Linux 3.2.0 x86 (Debian Wheezy)
Compiler: GCC 4.7.2 (Debian 4.7.2-5)
I am writing a program that requires advanced terminal control that is provided by ncurses but I cannot get my program to print anything to stdscr. For example if I compiled the following code I would not see "Testing.. Testing" on the screen. I have used ncurses before and I have never encountered such a problem. I do not know if this is relevant or not but I am running a fresh install of Debian (I literally installed it a couple of hours ago).
#include <ncurses.h>
int main()
{
initscr();
printw("Testing... Testing");
refresh();
return;
}
Also the above progam was compiled with,
gcc --all-warnings --extra-warnings -std=c11 filename.c -lncurses

If you want to see the text, maybe you should keep the program running when you're printing it.
#include <ncurses.h>
int main()
{
initscr();
printw("Testing... Testing");
refresh();
getch(); // Wait the user input in order to keep the program active and showing text.
endwin(); // Terminate the window to clean all memory allocations.
return;
}
You can get more informations on the ncurses "hello world": http://tldp.org/HOWTO/NCURSES-Programming-HOWTO/helloworld.html

Related

Why does the terminal keep returning a zsh parse error no matter my input?

I am incredibly new to the C language: I am trying to run sample C programs from codecademy, such as the hello world command below:
#include <stdio.h>
int main() {
// output a line
printf("Hello World!\n");
}
Bottom line is, every time I try to run any code in my macOS terminal, I always get the following error:
zsh: parse error near `\n'
What can I do to resolve this problem?
c is a language where you need to compile the code you've written. You do that by starting a C compiler and give the file containing your C code as input.
Example:
File: myfirstcprogram.c
#include <stdio.h>
int main(void) {
printf("Hello World!\n");
}
Then at the zsh prompt, invoke the compiler:
cc myfirstcprogram.c -o myfirstcprogram
-o myfirstcprogram is here an argument to the compiler telling it what to call the final program.
cc may be clang or gcc or any other compiler you've got installed if cc isn't already linked to the proper compiler.
When the compilation is done, the executable myfirstcprogram should have been created. You can now run it from your zsh prompt:
./myfirstcprogram
You can run it without recompiling it as many times as you like. Only when you change the source code (myfirstcprogram.c) do you need to compile the program into an executable again.

Unicode box drawing characters in a portable C program

I'm trying to use Unicode box drawing characters in a C program that is intended to be portable across a range of operating systems, C compilers and environments. Example:
#include <stdio.h>
#include <stdlib.h>
int main (int argc, char *argv[])
{
printf ("┌─────┐\n");
printf ("│┼┼┼┼┼│\n");
printf ("└─────┘\n");
return EXIT_SUCCESS;
}
The above works just fine in Linux with either gcc or icc, in Windows WSL with gcc, in Windows with Cygwin gcc in both Command Prompt and Cygwin Terminal windows, and with Windows MinGW in a Command Prompt window.
That is, in all those cases, the output looks like this:
┌─────┐
│┼┼┼┼┼│
└─────┘
But with MinGW in a Cygwin Terminal, the output looks like this:
ÔöîÔöÇÔöÇÔöÇÔöÇÔöÇÔöÉ
ÔöéÔö╝Ôö╝Ôö╝Ôö╝Ôö╝Ôöé
ÔööÔöÇÔöÇÔöÇÔöÇÔöÇÔöÿ
The problem doesn't seem to be the Cygwin Terminal font, because the same program compiled with Cygwin gcc displays correctly in the same Cygwin Terminal. Also, the same MinGW program displays correctly in a Windows Command Prompt window, just not in a Cygwin Terminal.
Windows compiler versions used:
gcc (MinGW.org GCC-6.3.0-1) 6.3.0
gcc (GCC) 11.3.0 - with Cygwin
What do I need to do to fix this please?
Thanks, Peter McGavin.

Error running *.c file in Eclipse Kepler

I've started programming in C so weeks ago, and I choose Eclipse Kepler as my IDE for C, since I had already used it for programming in some other languages and really liked.
However, after I had installed Cygwin and the C programming tools in Eclipse, I tried to run the old "Hello World" and it didn't work, It didn't appeared anything in the Console, only the message "Terminated".
#include <stdio.h>
int main(){
printf("Hello World!");
return 0;
}
Anyone has any idea what the problem could be?
Thanks to all of you. I tried everything you said but I wasn't able to get my problem solved. I gave up! Right now, i'm using as workspace in eclipse the Cygwin home. I write the program in the Eclipse and run it in the Cygwin command line.
Again, thanks to all for trying to help, it won't be forgotten. You're a really awesome crowd!
Maybe the program was too fast for you to see it?
The window with the program output appeared and disappeared in the blink of an eye.
You could try to prevent that from happening by changing your program to do its stuff AND wait for ENTER before it terminates.
#include <stdio.h>
int main(){
printf("Hello World!");
getchar(); /* simple wait for ENTER, error prone in more complicated programs */
return 0;
}
Note: your original program "prints a string"; this version "prints a string and waits for ENTER". It's a program with different requirements. If you want to keep to your initial requirements, try running the program from the DOS console.
you could try to system("pause") may this can help
#include <stdio.h>
int main(){
printf("Hello World!");
system("pause");
return 0;
}

C90 printf with \n or \r\n not working in cygwin; but fflush(stdout); works fine. why?

Cygwin64 bit
Command to compile:
gcc hello.c -o hello -ansi -pedantic-errors
Command to run
./hello
hello.c
#include<stdio.h>
int main() {
/*setbuf(stdout, 0); I KNOW THIS WILL WORK IF ADDED, it is a solution, but I want to know why line break itself is not working*/
printf("hello world!\n");
printf("hello world again!\r\n");
/*fflush(stdout); without fflush, the above strings are not showing*/
while(1)
{
}
}
Questions:
I don't want a fflush after every printf to let the terminal show the string in time, then how?
ANSWER: setbuf(stdout, 0);
Why is "\n" or "\r\n" not working in my case considering lots of posts pointed out a line break will fix the problem?
Is it true that cygwin's terminal behaves differently than normal Linux's terminal? Since I don't have linux installed, anyone give a test for me?
Or let me ask a more general question: On which kinds of terminals, the sentence "a new line will force flush" is true?
Thanks
It seems that in Cygwin, stdout isn't identified as a terminal (but as a pipe), so it isn't line-buffered by default.
Based on this answer, perhaps linking with the Cygwin DLL would help.
That program works for me under 32-bit Cygwin. Specifically, when I compile and execute this program:
#include<stdio.h>
int main() {
/*setbuf(stdout, 0); I KNOW THIS WILL WORK IF ADDED, it is a solution, but I want to know why line break itself is not working*/
printf("hello world!\n");
printf("hello world again!\r\n");
/*fflush(stdout); without fflush, the above strings are not showing*/
while(1)
{
}
}
it produces this output:
hello world!
hello world again!
and then hangs until I kill it with Ctrl-C.
I get the same behavior invoking the program from bash under the Windows console, mintty, and xterm (I doubt that the terminal would make any difference).
I'm using 32-bit Cygwin under 64-bit Windows 7. You say you're using 64-bit Cygwin, which was just announced a few days ago; I haven't tried it yet.
I suspect an issue with 64-bit vs. 32-bit Cygwin. I suggest you post to the Cygwin mailing list.
Here's a cleaned-up version of your program that should exhibit the same issue (please verify that the comments are correct):
#include <stdio.h>
int main(void) {
/* Adding setbuf(stdout, 0) here makes the output appear */
printf("hello world!\n");
/* Adding fflush(stdout) here makes the output appear */
while(1) {
/* nothing */
}
}
I know nothing about cgywin. but here I do a test in Linux.
I try the code below, and compile by : gcc -std=c90 filename.c
Without fflush It print all words before loop,so I think the newline flush the buffer! It just work!
I use SUSE gcc .
#include<stdio.h>
int main() {
/*setbuf(stdout, 0); I KNOW THIS WILL WORK IF ADDED, it is a solution, but I want to know why line break itself is not working*/
printf("hello world!\n"); /*without fflush, not shown*/
printf("hello world again!\r\n"); /*without fflush, not shown*/
/* fflush(stdout);*/
while(1)
{
}
}
I've just had this problem. I've got some code I've been working on a while and had to reinstall Cygwin on my PC. I installed the 64-bit version this time, it was x86 previously.
Built my code, ran it in the bash shell and no output.
What I didn't realize is that I have a Cygwin1.dll from the previous 32-bit version of cygwin still in my bin folder. I renamed this and ran my program and the output worked.
You may have a different issue, but if you've got different dll versions in use your bin other folders, it may cause this problem.

How to run Pthreads on windows

I used to use a mac to write some C programs but it's not working now.
I have to use an old windows laptop for a while.
I installed codeblocks and tested a simple program using Pthreads. Unfortunately it didn't work.
pthread_create(&thrd1, NULL, thread_execute, (void *)t);
It keeps saying undefined reference to _imp__pthread_create
How can i fix it?
You've clearly got a version of pthreads for Windows. You just haven't included the .lib file in your linker settings. Do that and you should be golden.
You need to grab pthreads-win32 as pthreads is a Unix component not a Windows one.
If you are using MinGW you can MinGW installation manager and install packages that need to execute pthreads and openmp related tasks. Here is the procedure.
After opening the installation manager go to all packages and select the select packages named using mingw32-pthreads-w32 and select them for installation.
Then go to the installation -> Apply changes to install new packages. The you can use pthread.h and omp.h inside your c or c++ program without any problem.
This code works fine in an MSYS2 terminal on Windows.
All you need to do is to install gcc. (See further below.)
// hello.c
#include <omp.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
void *print_hello(void *thrd_nr) {
printf("Hello World. - It's me, thread #%ld\n", (long)thrd_nr);
pthread_exit(NULL);
}
int main(int argc, char *argv[]) {
printf(" Hello C code!\n");
const int NR_THRDS = omp_get_max_threads();
pthread_t threads[NR_THRDS];
for(int t=0;t<NR_THRDS;t++) {
printf("In main: creating thread %d\n", t);
pthread_create(&threads[t], NULL, print_hello, (void *)(long)t);
}
for(int t=0;t<NR_THRDS;t++) {
pthread_join(threads[t], NULL);
}
printf("After join: I am always last. Byebye!\n");
return EXIT_SUCCESS;
}
Compile and run as follows:
gcc -fopenmp -pthread hello.c && ./a.out # Linux
gcc -fopenmp -pthread hello.c && ./a.exe # MSYS2, Windows
As you can see, the only difference between Linux and MSYS2 on Windows
is the name of the executable binary. Everything else is identical.
I tend to think of MSYS2 as an emulated (Arch-)Linux terminal on
Windows.
To install gcc in MSYS2:
yes | pacman -Syu gcc
Expect output similar to:
Hello C code!
In main: creating thread 0
Hello World. - It's me, thread #0
In main: creating thread 1
Hello World. - It's me, thread #1
After join: I am always last. Bye-bye!
Reference
https://www.msys2.org/wiki/MSYS2-installation/

Resources