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.
Related
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,
Why is a linking phase needed?
If I have this program
#include <stdio.h>
#include <math.h>
int main(){
float c=pow(8,5.6);
printf(" Result =%f",c);
return 0;
}
I guess it needs to perform the linker phase for the library Math.h?
Even if you don't use any functions, the program still needs to be linked with the standard C runtime library. This provides the wrapper code that calls main(), and calls exit() when the main() function returns.
Your program use two functions you didn't defined yourself: printf and pow, so to produce an executable there must be a link to the code of these. But even if you don't use any external function (fairly rare and not commonly useful) there is a need to set up the runtime before going into your defined code (some different technical things are usually needed for your code to behave correctly).
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!
I know that probably there is no concept of scope for macros, but please help me understand the following output - which seems to suggest that macros are local to functions:
#include<stdio.h>
#include<stdlib.h>
#define A 100
void fun();
int main()
{
fun();
printf("%d\n",A);
system("pause");
return 0;
}
void fun()
{
#undef A
}
The output of the program is 100 though according to me it should have been a compiler error. Please explain why?
The pre-processor works on the text of your source code and it does it before the compiler proper ever starts to run.
In essence your compiler works on a file that looks like
/* Lots of code from the included files omitted */
void fun();
int main()
{
fun();
printf("%d\n",100);
system("pause");
return 0;
}
void fun()
{
}
So running and printing 100 is exactly what you would expect.
Notice that all the pre-processor directives are gone, and that all instances of A between the define and the undef have been replaced with 100.
The thing to remember is:
The pre-processor runs, changing the text
Then the compiler runs on the result
The preprocessor makes a single pass though the program text at compile time. By the time the program runs all of the directives are long gone.
Originally, the preprocessor was actually a separate program that understood C only to the extent that it parsed tokens the same way. After creating a macro-expanded version of the program as a temporary file, the real compiler was run. Today, it's integrated into the compiler, but in such a way that the effect is the same.
This is how the convention of using all-upper-case macro names began, i.e., in order to emphasize their substantially different nature. You can still get the compiler to output the expanded-but-uncompiled intermediate text. This is occasionally useful when tracking down bugs and understanding complicated conditional compilation.
You can't run preprocessor commands in functions. They're removed from compiled code. That's why they're called preprocessor: they are executed and removed before the program is compiled. In fun(), you are undefining the number 100.
Macros are executed at compile time (in fact before the compiler).
macro expansion is done at preprocessing step, which is a step before compiling.
If you like to see how the code looks after this step, try compile with preprocessing only option.
e.g.
gcc -E myfile.c > myfile.ppout
and read the output.
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