473,503 Members | 1,635 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

puzzle on c statement

52 New Member
Hi All.

Can we write the below mentioned 4 lines statements into 1 line statements? .

if(flag) // flag is a int variable
return 1;
else
return 0;

I dont want to use ternary operater. please let me know , is there any other way to do the same operation on single statement.

Thanks,
Sridhar.D
Feb 16 '10 #1
13 2007
Banfa
9,065 Recognized Expert Moderator Expert
Yes there is examine all the logical operators.
Feb 16 '10 #2
donbock
2,426 Recognized Expert Top Contributor
Is your goal to put this code snippet all on one line of the source file or to implement it in a single statement?

In most cases, you can insert or remove newlines from C source code without altering the meaning of the code. This is a trivial answer to your question.

Off-hand, I can think of three ways to accomplish this code snippet in a single statement. (One of these ways is with the ternary operator; which you prefer not to use.)

Just because you can do something doesn't mean that you should. As far as I'm concerned, you shouldn't combine this snippet into a single statement unless that form makes it easier to understand what the function does. A lot depends on the meaning of the return values. Does this function return a true/false boolean value that is used with logical operators; or does this function return an integer value that is used with arithmetic operators?
Feb 16 '10 #3
anurag275125
79 New Member
can anyone please give the answer?
Feb 19 '10 #4
whodgson
542 Contributor
if(! flag) return 0;
Feb 19 '10 #5
donbock
2,426 Recognized Expert Top Contributor
@anurag275125
I'm still waiting for clarification whether the goal is to put all of these statements on one line of source code or to reduce this snippet into a single statement.
Feb 19 '10 #6
newb16
687 Contributor
The goal is to make the student to remember certain properties of implicit casting to bool of whatever-is-not-zero of certain unary operator that were explained in the class.
Feb 19 '10 #7
donbock
2,426 Recognized Expert Top Contributor
@sridhard2406
Let's start by analyzing the original code snippet. The if statement executes the then leg if the argument is nonzero or it executes the else leg if the argument is zero. That is, we return "1" if flag is nonzero or we return "0" if flag is zero.

You want to collapse this snippet into a single statement. The one thing we can say with certainty is that if this is possible, then the one statement has to be a return statement. That is,
Expand|Select|Wrap|Line Numbers
  1. return expression;
Now the problem has shifted to finding an expression that evaluates to "1" when flag is any nonzero value, and that also evaluates to "0" when flag is zero. Banfa gave you an important clue in the first reply to this post: "examine all the logical operators". See if you can use the logical operators to construct the necessary expression.


By the way,
@whodgson
I'm afraid this isn't a complete answer because it doesn't provide a way to return "1".
Feb 19 '10 #8
alexis4
113 New Member
You could just write
Expand|Select|Wrap|Line Numbers
  1. return flag;
Feb 19 '10 #9
donbock
2,426 Recognized Expert Top Contributor
@alexis4
The original code returns only two distinct values (1 or 0). This suggestion doesn't map all nonzero flag values to "1".
Feb 19 '10 #10
alexis4
113 New Member
You are right, I was hasty. Flag is an int variable, so I missed the cast...
Feb 19 '10 #11
donbock
2,426 Recognized Expert Top Contributor
@alexis4
C89 does not have a boolean type. Programmers generally use int to hold true/false variables. The quoted messages illustrate a danger with that approach. Consider the following code snippet.
Expand|Select|Wrap|Line Numbers
  1. int flag = 2;
  2. if (flag)
  3.    <code executed when flag is true>
  4. if (flag != false)
  5.    <code executed when flag is not false, ie when it is true>
  6. if (flag != true)
  7.    <code executed when flag is not true, ie when it is false>
All three then-legs will be executed, so some parts of your program will act as if flag is true and others will act as if flag is false. This is a more pernicious bug than if flag were erroneously set to true when it should be false (or vice-versa). The way to avoid this problem is to never compare a logically boolean variable to "true".

How might that bad value have gotten into flag? The Standard guarantees that the results of the logical operators (!, ==, !=, <, etc) are always either 1 or 0 (true or false), so they didn't do it. The answer is that you must have put the illegal value in there explicitly. Perhaps you incremented the variable in a loop so it counted up past 1.
Feb 20 '10 #12
mush1578
15 New Member
if ur using templat function or macro's so u can do it in one statment
Feb 20 '10 #13
alexis4
113 New Member
C89 does not have a boolean type. Programmers generally use int to hold true/false variables.
I use 2 different compilers. The GCC based compiler doesn't accept bool. The other one does.
Either way, you can use a manual cast:
Expand|Select|Wrap|Line Numbers
  1. return(!(!flag));

That should work.
Feb 20 '10 #14

Sign in to post your reply or Sign up for a free account.

Similar topics

1
2216
by: Andr? Roberge | last post by:
I have the following two files: #--testexec.py-- def exec_code(co): try: exec co except: print "error" #-- test.py--
270
3990
by: Jatinder | last post by:
I found these questions on a web site and wish to share with all of u out there,Can SomeOne Solve these Porgramming puzzles. Programming Puzzles Some companies certainly ask for these...
8
2731
by: TheTeapot | last post by:
Hi all, Here's a puzzle: // Say I have an array of numbers set up like so: $arr = array(15,16,17,100,121,1000); // How can I create a function so that I can use it like so:...
1
13074
by: xavier vazquez | last post by:
I have a problem with a program that does not working properly...when the program run is suppose to generate a cross word puzzle , when the outcome show the letter of the words overlap one intop of...
0
2002
by: xavier vazquez | last post by:
have a problem with a program that does not working properly...when the program run is suppose to generate a cross word puzzle , when the outcome show the letter of the words overlap one intop of the...
5
4447
by: ashish0799 | last post by:
HI I M ASHISH I WANT ALGORYTHMUS OF THIS PROBLEM Jigsaw puzzles. You would have solved many in your childhood and many people still like it in their old ages also. Now what you have got to do...
3
3197
by: oncue01 | last post by:
Word Puzzle Task You are going to search M words in an N × N puzzle. The words may have been placed in one of the four directions as from (i) left to right (E), (ii) right to left (W), (iii) up...
6
2560
by: Phoe6 | last post by:
Hi All, I would like to request a code and design review of one of my program. n-puzzle.py http://sarovar.org/snippet/detail.php?type=snippet&id=83 Its a N-puzzle problem solver ( Wikipedia page...
3
1712
by: ayiha | last post by:
Problem statement: You might have come across a puzzle which contains 15 numbered square pieces, which can be moved horizontally or vertically. A possible arrangement of these pieces is shown...
4
19761
by: honey777 | last post by:
Problem: 15 Puzzle This is a common puzzle with a 4x4 playing space with 15 tiles, numbered 1 through 15. One "spot" is always left blank. Here is an example of the puzzle: The goal is to...
0
7202
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7280
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7332
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6991
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7462
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5578
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
3154
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1512
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
382
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.