#include<stdio.h>
#include<conio.h>
void pass(int);
void main()
	{
		int a=23;
		int c;
		clrscr();
		pass(a);

		printf(" %d ",c);

		getch();
	}
void pass( i)
	{
		//int i;
		if(i>40)
			return 1;
		else
			return 0;

	}

Recommended Answers

All 17 Replies

That depends on what arun means.

I think "arun" means invoke undefined behavior. Because that's what the program does.

first, the declaration and definition of the function anthet should match.

#include<stdio.h>
#include<conio.h>
void pass(int);
void main()
	{
		int a=23;
		int c;
		clrscr();
		pass(a);

		printf(" %d ",c);

		getch();
	}
void pass( i)
	{
		//int i;
		if(i>40)
			return 1;
		else
			return 0;

	}

its run not arun

commented: That's obvious. -4

1. change the definition of function pass to

int pass(int i);   //- first int specifies the return type

return type cannot be void since you return 1 or return 0;

in the declaration of the function copy the definition, that is to say

int pass(int i){
                 if(i>40)
			return 1;
		else
			return 0;

}

Now suppose you want to check the value of a (>40 or <40)and store the result (1 or 0) of the function pass in variable c, and then print it. This is how you could do it, based on your original code:

#include<stdio.h>
#include<conio.h>
int pass(int i);
void main()
	{
		int a=23;
		int c;
		clrscr();
		[U]c = pass(a);
[/U]
		printf(" %d ",c);

		getch();
	}
int pass(int i)
	{
		//int i;
		if(i>40)
			return 1;
		else
			return 0;

	}

hope that works out for you!

best regards

Why do <where ever you are from> teach using void main and conio? I guess you are using a Borland compiler?

thanks for the reply
but my question is why the given program run?
this question ask by interviewer
whether it run or not ?if run then why?

and that progran was runn successfully

i'm 100% sure it will NOT run, in the original form.

If left aside the freaky thing with the function declaration and definition, it will have an epic FAIL due to the void return type specified for the pass function which is stated to return 1 or return 0.

So the answer to your interviewer (if your posted code is complete) is that the program over there is as good as a bike with no wheels, aka it will not work

I would complain to the interview-- tell them the code uses void main and conio. Many modern compilers will not compile that code and thus it won't run.

Furthermore, the code doesn't store or do SOMETHING with the return value from pass so it's pretty pointless. The program will print a garbage value from c's memory location.

It may run on the right compiler, but the behavior is undefined and pointless.

i ran that code on turbo c++ compiler and it ran successfully

i ran that code on turbo c++ compiler and it ran successfully

I ran that code with my mental compiler (it's very forgiving) and it also ran successfully, but that doesn't mean the code is correct in any way, shape, or form. For example, my mental Lint wouldn't shut up.

TC is a Borland compiler. It is very old and uses libraries and methods which are strange by today's standards. Did you ask the interviewer which compiler they would expect the code to run on?

You shouldn't use it.

According to the standards of 'C' language there is nothing written about a function cannot return which has return type void.
Any function which has void return type may not return value, but if we want we can return. Its not the compulsory condition.
But we should not assign the return value of the function. Because it violates the declaration rules.

According to the standards of 'C' language there is nothing written about a function cannot return which has return type void.
Any function which has void return type may not return value, but if we want we can return. Its not the compulsory condition.
But we should not assign the return value of the function. Because it violates the declaration rules.

What's your point?

I want to how and why given program run?

I want to how and why given program run?

Why don't you keep reading the standard? It has the answers:

A return statement with an expression shall not appear in a function whose return type is void.

Violation of a "shall" constraint invokes undefined behavior, which is defined as:

behavior, upon use of a nonportable or erroneous program construct or of erroneous data, for which this International Standard imposes no requirements

Translation: Anything can happen. If your compiler isn't nice enough to halt compilation when you do stupid shit, "anything" includes running. The answer to your question is that you got lucky (or unlucky, depending on how you view completely unpredictable behavior).

commented: Well said +17
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.