Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I want to clear terminal in C without using #include.
Here is my code:
int printf(const char *format, ...);
int main()
{
printf("%c2J", 27);
}
You likely won't be able to clear a terminal without using functions in the standard library, but probably the closest thing would be to use ANSI escape sequences, which might work depending on your terminal.
You'll have to output \x1b[2J (probably followed by \x1b[H). These are the ANSI terminal escape sequences for clearing the screen and repositioning the cursor in the upper left "home" position, respectively.
#include <stdio.h>
int main(void) {
printf("\x1b[2J\x1b[H");
return 0;
}
Please note that even if you specify the function prototype for printf(), as you have done above, you are still using the standard library and not making your own. I can only think of one reason you'd ever want to do that: to make a quine fit on one line.
If you want to write your own function you should not be using the standatd library. Invlude does not matter as .h files are not libraries. You should compile the code with -nostdlib option and write your own function.
In your code you simply use the standard library function - not your own one.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 days ago.
This post was edited and submitted for review 7 days ago.
Improve this question
I have to write some Windows code, and I want to separate it from the Linux part as much as possible.
At one point I need to check for NULL in the Windows code, but I don't want to include stdio.h or stdlib.h.
I strongly suspect that NULL is defined somewhere in windows.h, but I can't find the page. I found this, which is interesting, but doesn't tell me what I want to know.
NULL is defined in the standard C header stddef.h, period.
If you run for example gcc/mingw port in Windows, you can just tell any half-decent IDE to find the declaration of NULL and end up in stddef.h where it says #define NULL ((void *)0).
You can also create a source file like this:
// main.c
#include <windows.h>
int main (void)
{}
Then compile with gcc main.c -H. This will expand all header dependencies, so you'll see which header that includes what other headers. You'll get a whole flood of them and you'll notice that stddef.h is indirectly included at some 2-3 different locations.
Conclusion: NULL is not defined by windows.h or any other windows-specific header that you should be including directly.
If you need to use NULL, then the correct approach is to #include <stddef.h> regardless of OS.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 2 years ago.
Improve this question
I'm building a portable C library that needs to interact with a user-defined peripheral.
Here's an example,
My library needs to use a user-defined putc() and a getc().
To my understanding, there's two way to do this cleanly:
Using an "install" function that requires user to define interface with function pointers
// Inside mylib.h
typedef int (*mylib_port_putc)(char c);
typedef int (*mylib_port_getc)(char *c);
void mylib_install_port(mylib_port_putc, mylib_port_getc);
Using the extern keyword to let the user decide where to define the interfaces.
// Inside mylib.h
extern int mylib_port_putc(char c);
extern int mylib_port_getc(char *c);
What's the best way to do this?
I understand that "best" is difficult to define, but your opinion would be greatly appreciated.
EDIT:
I disagree with the StackOverflow maintainers to flag this question as inappropriate. I don't think this website should be limited to "how do I do X?" questions. I understand that asking for opinions will probably not generate a clear answer, but, to me, this is what mentorship looks like.
When you're starting, there's a lot of value in studying knowledgeable people debating tradeoffs.
Sorry for the rent. I know your job is hard. Happy holidays.
I think that the only correct way is the first -
you provide a library initialize(...) function for registering user callback functions - port_putc, port_getc, maybe also callbacks for you library logging, etc.
Such an approach is flexible, and very common.
In the second case, you require the library users to define 2 functions with predefined names in their code.
What happens if he didn't?
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I am trying to build a game in C language using raylib library and I wanted to deploy the sleep function that is defined in library. The latter generates a problem in the build of raylib library
Let's say that you have two headers, header1.h and header2.h, both containing a function named foo. Then you can define a new header/source pair:
mynewheader.h:
int header2_foo(int n);
mynewheader.c:
#include <header2.h>
int header2_foo(int n) {
return foo(n);
}
Of course, you can choose any prefix you want, or rename the function completely for that matter. This kind of mimics the namespace feature in C++.
If sleep is the only function you need from Windows.h then use _sleep() from stdlib.h instead. Check this MSDN discussion for further reference.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
On windows, the programmer could do something like: system("ls > outputFile.txt")
Is there a platform independent way to access the command line, or a least a way to determine which platform the program is being executed on (because calls for the same functionality vary quite a bit)?
The system(3) function is standard ANSI C, it's already platform-independent. Any conforming C implementation will allow you to call it to run the system default command line processor/shell application. Of course, the actual programs you can run will vary from system to system (e.g. dir only works on Windows, while ls usually works on Unix-like platforms).
system() itself is a standard C function defined in stdlib.h. The way it interprets its argument, though, is not standard (e.g. ls in UNIX, dir in Windows/DOS, etc.). If you're really asking whether there's a platform-independent way to list the files in a directory, the answer is (unfortunately) no. Some libraries do provide portable (to some degree) implementations, most notably Boost: How can I get the list of files in a directory using C or C++?
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
Is there any way to write a C program without a main function? If so, how can that be achieved?
C defines the entry point in a hosted environment to be main. In a "freestanding" environment, however, the entry point can have some other name. That's about the only latitude the language (at least officially) allows in that particular respect.
Yes, you can.
_start function is the entry point of a C program which makes a call to main().
Going further into it, main() is the starting point of a C program from the programmer's perspective. Before calling main(), a process executes a bulk of code to "clean up the room for execution".
_start is the function which gets called first, which then allocates necessary resources and then calls main() which has to be defined by the programmer.
You can override _start and tell the compiler to not to look for main() by using "-nostartfiles" option.
#include <stdio.h> //for using printf()
_start()
{
printf("Hello world!!\n");
_exit(0);
}
To Compile : gcc -nostartfiles code.c -o a.out
Also look at http://linuxgazette.net/issue84/hawk.html for more basic information.
The following linker abuse
char main[] = { /* Machine code for your target implementation */ };
will work on some platforms.
No. C is totally based off of the assumption that you start the program in main(). Anyway, why would you want this? This would make inconsistencies for other programmers reading your code.
Maybe this could work:
http://www.gohacking.com/2008/03/c-program-without-main-function.html
An alternative is to write a C program and look at the Assembly output:
http://users.aber.ac.uk/auj/voidmain.shtml
More information about what happens before main() is called can be found here (How Initialization Functions Are Handled):
http://gcc.gnu.org/onlinedocs/gccint/Initialization.html