Gulnar
Should I use for loop?
Vladimir
Should I use for loop?
I suppose so, judging by the information you provided.
Anonymous
/get cbool
Anonymous
/get cbool
Anonymous
/get cbook
Anonymous
anyone to teach me how to run windows files on mac?
Savta
I want to write a command line interface program in c that will run all the time, how do I achieve that?
Savta
The purpose of the program is to catch specific characters/expressions I write and execute them for me
Pavel
The purpose of the program is to catch specific characters/expressions I write and execute them for me
Make an infinite loop while(true) and process your commands inside it.
Vlad
Make an infinite loop while(true) and process your commands inside it.
Just make sure to not fall into the trap of infinite loop without side effects
Savta
Vlad
Usually I put volatile bool just to be safe
Savta
I just want to write a program that auto-translate chars I write in my language to English when I type them by mistake
Savta
I don't want to execute a program each time I open my terminal
Pavel
Usually I put volatile bool just to be safe
I mean more like something like this while (true) { bool isExitSignal = processCommandBlocking(); if (isExitSignal) return; }
Pavel
Where process command is something like reading from cin and ifelseif
Vlad
What if it is pure and doesn't?
Vlad
And you can't tell by reading this snippet alone
Pavel
In this case you must be sure that this function would produce side effects
For sure, if it doesn't produce side effects (e.g. outputs results to the console) it is not particularly useful :)
Vlad
It changes the global state
Savta
But my question is how I achieve the condition where I type in terninal: "cookie" and the program writes for me "milk". I know how to achieve that by doing: ./myProg cookie But how do I skip that stage?
Pavel
But my question is how I achieve the condition where I type in terninal: "cookie" and the program writes for me "milk". I know how to achieve that by doing: ./myProg cookie But how do I skip that stage?
As I said already, you need to run your program in an infinity loop where on each iteration it will read from the console, calculate and print the result
B
anyone knows about sql here?
Savta
Because I want it to run without me activating the program every time
Pavel
Thank you, I will try it, just thought there might be some more efficient way to do it
If your read from console is blocking, there won't be any work done in between inputs (the app will be waiting for input), so it is not particularly inefficient
B
with dept_num
Pavel
This is not a correct chat to ask about SQL
Anonymous
Is SQL allowed here?
Savta
Make it a daemon
That is the word I was looking for. Thank you!
B
ok sorry anyway
Pavel
:)
Who knew, your question may have been related to C/C++
B
if someone could help dm please
klimi
you can ask about sql in the offtopic group
Eturnus
hi there! guys, can i get C++ ebook here?
MAC
Is SQL allowed here?
No, only C, C++
Gulnar
Hey guys, I have written a program with if statement, and at the end I want to ask for continuing at the same time i want if i write like answer Y or y it starts again but n or N it stops , how can i do it?
Gulnar
use a while loop
What should I write inside while loop?
Anonymous
Юрій
uint32_t myInt = 123456; std::cout << std::setfill('0') << std::setw(8) << std::hex << myInt << '\n';
Юрій
Anyone knows how to write it into string variable?
Anonymous
Anyone knows how to write it into string variable?
create an std::ostringstream from your std::string and store with that. alternatively, use std::format_to (fmt::format_to in fmtlib) for a more modern solution.
Юрій
Thanks)
Anonymous
Thanks)
corrected a small mistake
ая
site for solving problems especially Pointer (C) ?
Nils
Hi, how does this work without segfault? int main() { char *m = (char*)main; strcpy(m, "Hello world!"); puts(m); }
Anonymous
It can work any way
Anonymous
It can format your drive
Nils
It's undefined behavior
Yeah but I wonder… Why is there no segfault trying to overwrite the currently executed function
Nils
and isn't it in read-only memory?
Nils
Because it's undefined behavior
Which is not a proper explaination lol
Anonymous
Which is not a proper explaination lol
It is the ONLY explanation
Ammar
Hi, how does this work without segfault? int main() { char *m = (char*)main; strcpy(m, "Hello world!"); puts(m); }
It would overwrite .text section. If your .text section is writable, it's probably not segfault.
Ammar
It segfault from my end anyway: ammarfaizi2@integral:/tmp$ cat > qwe.c int main() { char *m = (char*)main; strcpy(m, "Hello world!"); puts(m); } ammarfaizi2@integral:/tmp$ gcc qwe.c -o qwe qwe.c: In function ‘main’: qwe.c:3:9: warning: implicit declaration of function ‘strcpy’ [-Wimplicit-function-declaration] 3 | strcpy(m, "Hello world!"); | ^~~~~~ qwe.c:3:9: warning: incompatible implicit declaration of built-in function ‘strcpy’ qwe.c:1:1: note: include ‘<string.h>’ or provide a declaration of ‘strcpy’ +++ |+#include <string.h> 1 | int main() { qwe.c:4:9: warning: implicit declaration of function ‘puts’ [-Wimplicit-function-declaration] 4 | puts(m); | ^~~~ ammarfaizi2@integral:/tmp$ ./qwe Segmentation fault (core dumped) ammarfaizi2@integral:/tmp$
Nils
Ah okay, so basically it makes use of improper protection?
And due to the CPU cache it still executes fine
Nils
right?
Anonymous
So you like offtoping, right?
Ammar
And due to the CPU cache it still executes fine
I don't think so, there is called PT_GNU_STACK that determines the permission of these sections.
Ammar
tcc -run
I don't have tcc, so IDK.
Ammar
So you like offtoping, right?
It's still discussing C, calm.
Nils
I don't have tcc, so IDK.
tcc basically can run the executable from memory
Nils
tcc basically can run the executable from memory
ohh yep, reexecuting the function at the end then causes segfault
Nils
int main() { char *m = (char*)main; strcpy(m, "Hello world! This is a test!"); puts(m); main(); } Results in: Hello world! This is a test! Segmentation fault
Anonymous
It's still discussing C, calm.
it's not a valid C program because it has UB, so it's not C https://t.me/programminginc/374520
Nils
it's not a valid C program because it has UB, so it's not C https://t.me/programminginc/374520
even if it is not a valid C program it is still a C program, but… Invalid lol
Nils
A broken keyboard is still a keyboard even though it does not work