TurboC++ graphics.h conflict with conio.h's clreol() - c

Note: This is TurboC++ so please don't expect STL
I have this simple code that have no other graphics.h functions rather than it's driver's declaration and call. I aim to:
Print a first string (A longer one)
Go to the first string's coordinates, clear that string (using clreol())
Print the second string which is shorter.
But I rather get this output on print of second string:
Shorter phrase.██████████████████████████████████████████████████████████████████
Here's my code:
#include <stdio.h>
#include <conio.h>
#include <graphics.h>
int gdriver=DETECT, gmode;
void main(){
clrscr();
initgraph(&gdriver,&gmode,"C:\\TURBOC3\\BGI");
printf("Longer phrase than next.");
getch();
gotoxy(1,1);
clreol();
printf("Shorter phrase.");
getch();
}
When I remove the initgraph() function, it works fine, so there might be the problem, but of course I need it.

Haha - coding problems from stoneage ;). Thanks for this - it activated some nice memories.
My guess would be that you run into problems because you are mixing BGI (graphics) functions and "normal" text output. Try replacing the text output calls with calls to the corresponding BGI functions (if I remember correctly, this was called outtextxy() or something).

Related

getch undefined while compiling for the second time

I am using visual studio 2017 .
First I wrote the following code :
void main()
{
printf("abcdefgh %d hjhjh %d", 5, 6);
getch();
}
It ran perfectly fine .
But after that I modified the code to the following :
void main()
{
char abc[100];
strcpy_S(abc, "premraj");
printf("%s", abc);
printf("abcdefgh %d hjhjh %d", 5, 6);
getch();
}
But now I am getting an error with getch stating that "'getch' undefined, assuming extern returning int"
But this new code has been built on the existing code which recognized getch perfectly , how can it not recognize getch the second time ?
I checked out the following question :
getch() is working without conio.h - how is that possible?
which also carried a similar problem but here with modifications only I got this error .
There is an informative answer there by user named "Fatal Error" but still I would like to know about this intriguing phenomenon that is coming in after modifications . What can be the reason behind this ?
P.S : The following was my header file declarations for the first time :
#include <stdio.h>
and the following for the second time :
#include <stdio.h>
#include <string.h>
Once upon a time, if you called a function which the compiler had never heard of, like this:
#include <stdio.h>
int main()
{
int x = foo();
printf("%d\n", foo);
}
Anyway, if you did that, the compiler quietly assumed that foo() was a function returning int. That is, the compiler behaved just as if you had typed
extern int foo();
somewhere before you called foo.
But in, I think, C99, the language was changed. It was no longer legal to call a function you had not explicitly declared. Because there was lots and lots of code out there that was written under the previous set of rules, most compilers did not immediately begin rejecting the old-style code. Some continued to quietly assume that unrecognized functions returned int. Others -- like yours -- began noisily assuming that unrecognized functions returned int, emitting warnings along the lines of "'foo' undefined, assuming extern returning int".
It sounds like your question is that some time ago, your code containing calls to getch() was accepted without warning, but today, you're getting the warning "'getch' undefined, assuming extern returning int". What changed?
One possibility is that your code changed slightly. If your code used to contain the line
#include <conio.h>
somewhere, that file would have contained a declaration along the lines of
extern int getch();
and this would have goven the compiler the declaration that it needed, and you would not have gotten the warning. But if today your code does not contain that #include line, that explain why the warning started cropping up.
It's also possible that your compiler has changed somehow. It's possible you're using a new version of the compiler, that's more fussy, that has gone from quietly assuming, to normally assuming. Or, it's possible that your compiler options have changed. Many compilers can be configured to accept different variants of the language, corresponding to the different versions of the language standards that have been released over the years. For example, if some time ago your compiler was compiling for language standard "C89", but today, it's doing "C99" or "C11", that would explain why it's now being noisy with this warning.
The change in the compiler could be a change in the defaults as configured by a system administrator, or a change in the way you're invoking the compiler, or a change in your project's Makefile, or a change in the language settings in your IDE, or something like that.
A few more points:
getch is not a Standard C function; it's specific to Windows. Your program would be more portable, in general, if you didn't use it. Are you sure you need it? (I know what it's for; what I don't know if there's some other way of keeping your program's output window on the screen after if exits.)
You should get in the habit of declaring main() as int, not void. (void will work well enough, but it's not correct, and if nothing else, you'll get lots of negative comments about it.)
I think there's something wrong with your call to strcpy_S, too,

Create simple display

Sorry for this simple question but I don't really understand how correctly create consol on C. I wanna have several dynamic lines that will be update. I know that I can use \r but it is only for one line. I want for several lines. system("cls") not working good for this. Maybe you can help me.
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
int main() {
int currInt = 0;
while (true) {
system("cls");
printf("%d", currInt);
currInt++;
if (currInt == 5) {
currInt = 0;
}
}
}
I will be have asynchronous input data that will be display on several lines and I need have update this screen. I think about system("cls") but it not clear screen in loop. Endless loop is important.
I doubt it that printf() will help you achieve your goals. If I were you, I would give a shot with Ncurses. Check this question too: Where can I find a complete reference of the ncurses C API?
If this library won't satisfy you, then I would suggest curses.h. However I doubt this will do, since Ncurses is a modern implementation of the original curses.
For Windows: Is ncurses available for windows?

error: identifier "sytem" is undefined

I have just started to learn C language and I'm just trying to write Hello World to get started but I get this error message. I'm sure the answer is obvious but can someone please tell me what I need to do? This is my code:
#include <stdio.h>
int main()
{
printf("Hello World ");
system("Pause");
return 0;
}
#include<stdlib.h>
Include this header file..
You need to add another header file:
#include <stdlib.h>
When you have an undefined call like this you can always throw "man 3 system" and you'll get something like this so you can see if you're missing a header file.
FYI, for your specific program, you may want to consider no using system("Pause") since it's system dependent. It would be better to pause with a break point (if you're using an IDE) or something more C standard like getchar()
You need to #include <stdlib.h>
If you aren't sure which header a standard function is defined in, its man page will tell you.
Insert
#include <stdlib.h> //in C
or
#include <cstdlib> //in C++
before your main() function.
Note that your IDE should refrain from closing your program. If it doesn't, change IDE.
You should include the following library.
#include <stdlib.h>
It's simple as that.
I hope you find this useful.
As the others said, you need to include an header; if you're running on Linux, you may install "manpages-dev" package, and then tape "man system" which will tell you what are the headers you need to use.

How to write a quine program without main()

I went through all sorts of quine problems, but my task was to get a quine problem without main(), and loops are also forbidden.
Without loop, it is easy, but I can't figure out how to write one without main(). Can anyone help me or provide me with a link?
You cannot create a (non-freestanding) C program without a main() function. Thus, creating a quine in C without a main() is impossible in the usual sense.
That said, depending on how you define a quine, you might be able to construct a source file which fails to compile, but for which the compile error (on a certain specific compiler) is the contents of the source file.
First thing its impossible to write program without main function because compiler always starts execution from main() function, without main function linker will not be aware of start of data segment.
Yeah but playing with some tricks with preprocessor you can do it, but this is not a good method to do that.
http://www.gohacking.com/2008/03/c-program-without-main-function.html
This might help you.
Take a look here too:
Is a main() required for a C program?
#include <stdio.h>
int
foo(void) {
printf("pong!\n");
return 0;
}
int main() __attribute__((weak, alias("foo")));
There is main() declaration, but not definition.

Is it possible to write a C program without using header files?

Is it possible to write a C program without using header files? If it is, how?
Of course:
int main() {
return 0;
}
Or even:
int printf(const char *format, ... ); // could be copied from stdio.h
int main() {
printf("Hello, world!\n");
return 0;
}
The #include directive effectively just includes the header file's content in the source file.
Of course.
A header file is just a file that gets included in some source files, and when you include a file you just copy its content.
You can write any program you want to without any #include, but you'd have to manually put the stuff you need in your source files.
Yes it is possible to write a simple program without header files, but why would you do that ?
Header files are useful to share definitions, constants, functions prototypes, etc between multiple files or modules.
Sure. Because header files written in C. But it's hard.
printf example:
int printf(const char *format, ...);
scanf example:
int scanf(const char *format, ...);
and more...
Absolutely yes, you can even inlinme the function prototype you possibly need in the c file itself
I was trying to write the shortest code possible in c, so i tried removing the header files from source code.To my surprise even a program with printf compiled with just a warning and ran successfully.How does that happen??
main()
{
printf("Hello World\n");
}
It's possible, but by all means, avoid from not using it if not necessary.
Yes you can wirte a program without #include , but it will increase the complexity of the programmer means user have to write down all the functions manually he want to use.It takes a lot of time and careful attention while write long programs.Yes ,simple program like given above have no problem to write without including any library function call.
#include<"filename">
will help you to implement and use the functions present in the file,
i.e.
#include< stdio.h>
will help us to use the built functions present in the stdio.h file - printf and scanf
When you dont use #include< stdio.h> in your program , it still will not cause any problems, its only when you may use printf or scanf it may cause the program to generate a warning at the time of compilation (for implicit declaration of function printf.)
More Details on the same , Below link is the screenshot for the same for the printf used without specifying #include<stdio.h>
image

Resources