what does this print?

Apr 23, 2022 at 1:11am
1)
void fun();
void main()
{
int x = 1;
fun();
printf("%d", x);
}
void fun()
{
int x = 2;
printf("%d", x);
}
Apr 23, 2022 at 1:28am
Have you tried running it? What do YOU think will be displayed?
Apr 23, 2022 at 1:28am
It probably took more effort to post on this buggy forum than to copy and paste that into a text file and compile it.
Apr 23, 2022 at 1:30am
I tried running it but i keep getting an error message. its for a test lol
Apr 23, 2022 at 1:36am
Works on my machine.
Apr 23, 2022 at 1:36am
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 Apr 23, 2022 at 2:24am
Apr 23, 2022 at 1:37am
Im just copying and pasting what the test says
Apr 23, 2022 at 1:37am
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 Apr 23, 2022 at 1:38am
Apr 23, 2022 at 1:41am
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?"
Apr 23, 2022 at 1:42am
sorry I am just tryna pass this class
Apr 23, 2022 at 1:45am
Pasting code and expecting others to do all the the work for you won't help you.
Apr 23, 2022 at 2:04am
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.
Apr 23, 2022 at 2:06am
I never tried to compile it. The assignment never said to compile it. You assume too much!
Last edited on Apr 23, 2022 at 2:12am
Apr 23, 2022 at 2:16am
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 Apr 23, 2022 at 2:19am
Apr 23, 2022 at 11:13am
what does this print?


I refer to my answer here http://www.cplusplus.com/forum/general/283283/
Apr 23, 2022 at 11:48am
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.
Apr 23, 2022 at 11:56am
... 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).
Apr 23, 2022 at 12:17pm
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.
Apr 23, 2022 at 2:20pm
@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.