what does this print?

1)
void fun();
void main()
{
int x = 1;
fun();
printf("%d", x);
}
void fun()
{
int x = 2;
printf("%d", x);
}
Have you tried running it? What do YOU think will be displayed?
It probably took more effort to post on this buggy forum than to copy and paste that into a text file and compile it.
I tried running it but i keep getting an error message. its for a test lol
Works on my machine.
FYI, it is int main(), not void main().
https://www.hellocodies.com/int-main-void-main-and-main/

And,
Learn to use code tags, they make reading and commenting on source code MUCH easier.

How to use code tags: http://www.cplusplus.com/articles/jEywvCM9/

There are other tags available.

How to use tags: http://www.cplusplus.com/articles/z13hAqkS/

HINT: you can edit your post and add code tags.

Some formatting & indentation would not hurt either


This isn't the first time you have been asked to use code tags.
Last edited on
Im just copying and pasting what the test says
The function is called 'fun', but this is anything but!

Im just copying and pasting what the test says
Well then, paste the error message you got as your answer. Because that is, empirically, the answer to the question "what does this print".
Last edited on
Im just copying and pasting what the test says

So you expect us to provide answers to your test, for free?

Guess again. This isn't the first time you pasted buggy/bad/incomplete code, without code tags, and expected us to give you answers to "what does this do?"
sorry I am just tryna pass this class
Pasting code and expecting others to do all the the work for you won't help you.
Ganado wrote:
Works on my machine.

I'd wager you added at least one C header to get this to work, right?

Without headers trying to compile this monstrosity should generate compile-time errors or some serious warnings.
I never tried to compile it. The assignment never said to compile it. You assume too much!
Last edited on
I prefer compilers that at least try to conform to a recent standard, be it C or C++.

And that doesn't mean having this #include <bits/stdc++.h> as THE include.
Last edited on
what does this print?


I refer to my answer here http://www.cplusplus.com/forum/general/283283/
roccosd26 wrote:
I am just tryna pass this class

Joda has a saying about that.

If you have learned the subject of the class, then you should understand the code.
If you have failed to learn, then by definition you should not pass.
... and please learn how to use code tags to format code


[code]
the formatted code goes here
[/code]


1
2
3
4
5
6
7
8
9
10
11
void fun();

void main() {
	int x = 1;
	fun();
	printf("%d", x);
}
void fun() {
	int x = 2;
	printf("%d", x);
}


what does this print?


As it is invalid code and doesn't (shouldn't) compile then there is no compiled code to run - so nothing prints (except perhaps an error re no such program).
Rather than "invalid", that is "an incomplete program". A part of program.
A human reader should know what the "printf()" is; a compiler has to be told.

Well, the 'void' in 'void main' is not valid by both C and C++ standards.
Looks C, rather than C++. Probably ancient fragment of code.
@keskiverto,

I tried the OP's code as given, as C code, in both VS 2022 and TDM-GCC (Dev-C++). 2022 spit up compile-time errors, TDM-GCC compiled the hot mess with several warnings.

The OP's compiler could be one that accepts non-standard usage.
Topic archived. No new replies allowed.