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?
Related
#include <stdio.h>
int main() {
for (/*1*/ int i = 0; /*2*/i < 10 ; /*3*/i++) { /*4*/printf("hello\n");}
return 0;
}
it seems vscode couldn't stop on breakpoint(in comments) in for statments
I'm not sure if this is a feature commonly supported. And TBH, I'd say it's a code smell if you have a need for it. Your example is trivial, and there it's no need to know what's going on between 2 and 3. And whenever you actually DO have a need for it, then your conditions and statements in the for header are likely too complicated.
And besides, most of the breakpoints. can be easily moved to equivalent positions.
/*1*/
/*2*/ First time
for (/*1*/ int i = 0; /*2*/i < 10 ; /*3*/i++)
{
/*4*/ printf("hello\n");
/*2*/ Second time and later
}
But it's not possible to put a break between 2 and 3 without rewriting the code.
If you REALLY want this and your debugger don't support it, you can rewrite it in the following way. Change this:
for (<init>;<cond>;<inc>)
{
// Loop body
}
to this:
/*1*/
for (<init>;;)
{
/*2*/ if(!(<cond>)) break;
/*4*/ // Loop body
/*3*/ <inc>;
}
From comments:
thanks, I know that the snippets post in description is of bad coding style, mainly reason behind this question is that I couldn't format existed code and I need to debug this code of bad coding style...
I completely understand your problem, but before debugging, it's often a very good advice to try to refactor the code to debuggable code before starting to debug.
Comparing to JS
You mentioned that you were able to do this in javascript. One good thing to remember here is that javascript is an interpreted language, while C and C++ are compiled languages. The compiled code does not necessarily have a good 1 to 1 correspondence with the source code. The debugger tries it best to do a good mapping, but it may fail. Especially with heavy optimization activated.
I'm writing a program in C for OS X (for the terminal). As mentioned in the title, just need the id and/or name of the application receiving the keystrokes i.e. the focused window.
I've found the you can use frontmostApplication but I can't use it in C or can't figure out how to do it. I'm new to writing stuff in macOS, any help much appreciated.
The UI layer on macOS is all written in Objective-C, which you can't easily call from straight C code (well, technically, you could use objc_msgSend(), but trust me, you don't want to do that). Fortunately, the amount of Objective-C code you'll need to do this is quite small, and you can separate it out into a separate file which you include from your C code:
GetFrontApplication.h:
#ifndef GETFRONTAPPLICATION_H
#define GETFRONTAPPLICATION_H
char *GetFrontmostApplication(void);
#endif /* GETFRONTAPPLICATION_H */
GetFrontApplication.m:
#import Cocoa;
char *GetFrontmostApplication(void) {
#autoreleasepool {
NSRunningApplication *frontApp = [[NSWorkspace sharedWorkspace] frontmostApplication];
return strdup(frontApp.localizedName.UTF8String);
}
}
Now you can just #include "GetFrontApplication.h" and call GetFrontmostApplication() from your C code, and you'll get a char * containing the name of the application. Make sure to free() the string after you're done with it.
I've seen lots of different ways this can be done, none of them seem ideal in terms of having to use lots of wrappers and callbacks. Is there a simple way of doing this?
For example, we have this:
//foo.go
package foo
import "C"
//export SayFive
func SayFive() int {
return 5
}
This has been stripped down to the minimum now, and all I want to be able to do at this point is call that SayFive function in C.
However, not at the top of this file. It's very simple and useful to be able to do that, but I'm looking for a way like this:
//foo.c
#include <stdio.h>
int main() {
int a = SayFive();
}
I've seen in examples that are like the above, that #include "_cgo_export.h" which makes total sense, but when I've done that and tried to compile it, it fails.
Could anyone explain the whole process involved that would allow us to do this?
You can call C from Go and Go from C, but only within the framework of a Go program.
So your example of a C program with a main() won't work, because that would be calling Go from within the framework of a C program.
In other words, Go can't make objects you can link statically or dynamically with C programs.
So you'll have to turn what you want to do on its head and make the Go program the master, and call the C program parts from it. That means the program with the main() function must be a Go program.
Hope that makes sense!
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).
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.