Clearing screen and kbhit() function - c

I got some problems writing my snake game program. I need to make game working on linux and windows. I found some topics how to clear screen on linux and windows using the #ifdef Windows etc. The problem is i need to use C89 standard, and im not sure that system("cls") is in C89. Could you help me with finding C89 functions to clear screen, and tell me something about kbhit() function on linux? Sorry for my english, and thanks for help.

C89 does not have terminal handling functions. Instead, you should use OS specific functions. So you need to have, say, a source file only for windows functions and another for linux. Another option is to use a cross platform library. I would choose ncurses for this task:
http://www.tldp.org/HOWTO/NCURSES-Programming-HOWTO/
It works on any unix system, including linux and Mac OS. For windows versions, see:
Is ncurses available for windows?
With ncurses, you have functions like erase() and clear() to clear the screen.

On Unix-liked systems including Linux and macOS, you can use ncurses library (POSIX API). In case of Windows (or even Linux or macOS), the following code will work on ANSI terminals on any systems.
printf("\033[2J\033[H");
/* or */
printf("\033[0;0f");

Related

Is there a way to manipulate terminal zoom and size with code using the standard library and essential POSIX libraries?

I am searching for a way to change the:
terminal zoom (primary)
terminal dimensions (secondary)
using only the standard C library and established essential libraries such as <unistd.h> and <termios.h>, etc. Using ncurses is not allowed.
The Standard C library (as in ISO 9899:2018 or similar) doesn't know what a terminal is, much less how to change one. The answer to that is "NO" — the Standard C library has no such functions.
Originally (once upon a long time ago), terminals were hardware screens attached to a computer via an RS232 cable — Wyse 60 and DEC VT100 are two examples. Such terminals could not be resized — though you could sometimes change the display so that instead of 24x80 you got some larger number of columns to use.
If you're referring to a graphical terminal window on a modern Unix-based system, there probably are ways to change the size, but they involve using the insides of X11 — definitely not particularly easy, and definitely not provided by POSIX via <unistd.h> or <termios.h>; I don't think standard <curses.h> or <ncurses.h> would help either. And the mention of X11 immediately implies that it won't be portable to Windows, and what might work on Windows won't work on Unix — the API for Windows will be different.

conio.h is missing from Windows

I usually use VS but trying cygwin for the first time. I am using windows 7 but on compiling hello world program using gcc, it says "fatal error: conio.h: no such file or directory".
I am using Windows 7 and it seems conio.h is missing from my system. Can someone please tell me how to resolve this issue.
Thanks!!
In Cygwin there doesn't exist any such header file called conio.h! Also, you don't need it either because it automatically holds screen for you without using getch() and for clrscr() you do have system("clear") in Cygwin!
conio not being part of the standard library, you cannot expect it to be available cross-platform, or even between compilers on the same platform.
Being, non-standard, the name conio has been used by both Borland and Microsoft for libraries with differing APIs - Microsoft's is much smaller. So for that reason you might avoid it for portability.
It is not a matter of conio not being on Windows, Cygwin is a POSIX API layer and tool-chain for building and running POSIX code on Windows. The libraries provided with it are independent of those provided with Visual Studio.
There are a number of solutions including:
Use an alternative console I/O library, such as ncurses.
Use a conio source code implementation for Linux such as this (which uses ncurses and implements Borland's API).
The second solution is perhaps useful if you have a lot of legacy code using conio, but is overkill if you just want to prevent a console windows from closing. For that you could just use getchar() in any case and accept that you will have to press enter rather than any key.
If you are using Cygwin just to be able to use GCC on Windows, you might be better off using MinGW/GCC instead. This uses Microsoft's C runtime rather than GNU, and the Win32 API rather than POSIX.

How to add a header file to the c project?

Am using a Dev c++ compiler, to compile a c code. (I am a beginner)
When I compile, it says 'some' header files are missing.
How can i include those header files in my system, so as to be utilized by the program??
Thanks
A header such as <sys/sem.h> which is used for the function semget() among other things, is not generally available in Windows. It's a POSIX header, and Windows does not implement the POSIX standard out of the box.
You should maybe look at the Win32 API instead, for instance a function like CreateSemaphore().
The problem is that you are trying to use the Linux API on Windows. Here is what is going on: Every operating system has its own set of libraries for programmers to use to make programs on that platform. In this instance, you are attempting to use Linux libraries on Windows. Windows doesn't have a code location called sys/ipc or sys/sem.
Furthermore, since you said you are a beginner, try finding another tutorial. sys/ipc.h and sys/sem.h are not for beginners, are are libraries typically used for communication between processes. These concepts are way beyond you right now haha :P
Here is a better place to start: http://www.cprogramming.com/tutorial/c-tutorial.html

dos.h for Linux?

I have a C program which contains #include <dos.h> header. It shows a compile time error. I know that the dos.h header file is not valid in Linux.
Is there any other equivalent header for dos.h in Linux?
Linux is a Posix/Unix like system, so you should learn the system calls and facilities that you can use. Read the advanced unix programming book (or some equivalent; AUP is considered a very good book). You can also read advanced linux programming (even online, a copy is here). So Linux don't have a dos.h header.
You could also type man 2 intro to get an intro to syscalls, and their list in in syscalls(2) man page. From an application's point of view syscalls are elementary operations provided by the Linux kernel.
The GNU libc provides a big lot of functionality (e.g. standard C functions like malloc and fprintf, and system functions like fgetpwent to query user database, etc etc...) above the system calls. Almost every Linux program uses it.
If you care about coding stuff which should be portably runnable (after recompilation) on other similar systems (e.g. MacOSX or FreeBSD) consider following the Posix standard.
If you want to code a terminal screen application, consider using ncurses.
If you care about graphical interfaces, use a graphical toolkit like Qt or Gtk; they usually interact with an X11 server (and both Qt and Gtk are able to run on some other non Posix systems, e.g. Windows, by providing a common graphical abstraction layer.). Both Gtk and Qt are adding an abstraction layer (Glib and QCore respectively) above system functions and facilities (in particular above the pthreads standard thread library).
At last, Linux is free software; so you might find interesting to look inside the source code (of a library or utility) that you are using. You could even improve it and contribute to it.
In all these aspects, Linux programming is very different from Windows or DOS.
Don't try to mimic every Windows or Dos function into Linux (e.g. don't ask the equivalent of every dos.h function); learn the Posix/Unix way of thinking and coding.
The time(7) man page tells you a lot about time (various meanings and functions about it) on Linux.
Don't forget to ask warnings from the compiler with gcc -Wall -Wextra; as a general rule, improve your source code till you get no warnings.
There cannot be an exact Linux equivalent of dos.h because Linux (i.e. Unix or Posix spec) and Windows are systems with different features and concepts. However several free libraries (I mentioned Glib and QCore) are providing common abstractions to fit into Linux and into Windows, so if you want to develop software portable to Windows and to Linux I suggest using these libraries instead (use them both on Windows and on Linux).
(I also suspect that Microsoft would use legal threats -patent or copyright based- to avoid that free clone of their proprietary dos.h, given their monopolistic reputation and their aversion to standards and to free software; I admit I have strong opinions against Microsoft..)
dos.h header file is interface to the DOS operating system. They are not portable to operating systems other than DOS (means not works in Linux). Which functionality in dos.h you are going to use?
#include<dos.h> is not available for Linux
but if you want to use dos.h for displaying the time you can use the system function and do it like this
prototype -> system(command);
system("date +%H:%M:%S");
if you want your program to sleep for a specific seconds
try this
system("sleep 3") //sleep for a 3 seconds
or use this
std::this_thread::sleep_for(std::chrono::milliseconds(100));
but you have to include the thread header file #include<thread>

What libraries are needed for graphics like vim or nano?

What library is used to make a static terminal window like vim, nano, irssi or aptitude as opposed to a scrolling terminal?
I'm using C, but I'd be interested in libraries for other languages (for example, a C++ specific library).
I believe the library you are looking for is Curses (or NCurses). There is also PDCurses for cross-platform (includind Win32) development.
I also remember the days of Conio on DOS based systems.
Curses like library are usually used for that. pdcurses for windows, most *nixes come with a native version, or e.g. ncurses
That would be ncurses.
You can also use the termcap library (curses provides this) to lookup the terminal control strings for the terminal you're connected to, and send them yourself to implement whatever you like. Or, if you don't mind requiring a modern terminal that supports at least a common subset of the ANSI/ECMA standard, you can simply hard-code the standard terminal escapes.

Resources