Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does one execute a no-op in C/C++?

Tags:

c++

for the following:

( a != b ) ? cout<<"not equal" : cout<<"equal";

suppose I don't care if it's equal, how can I use the above statement by substituting cout<<"equal" with a no-op.

like image 686
Juba Avatar asked Nov 18 '08 21:11

Juba


People also ask

What is a no op in C?

In computer science, a NOP, no-op, or NOOP (pronounced "no op"; short for no operation) is a machine language instruction and its assembly language mnemonic, programming language statement, or computer protocol command that does nothing.

What does (( void 0 do in C?

It should be noted that, when used as a macro (say, #define noop ((void)0) ), the (void) prevents it from being accidentally used as a value (like in int x = noop; ).

What is a no op build?

A no op (or no-op), for no operation , is a computer instruction that takes up a small amount of space but specifies no operation. The computer processor simply moves to the next sequential instruction. The no op is included in most assembler languages.

What is a no op Python?

Last updated: April 1, 2017. In Python programming, pass is a null statement typically used as a placeholder. In contrast to a comment statement which is ignored by the Python interpreter, a pass statement is not ignored but nothing happens when it is executed. It results in No Operation (NOP).

What is no operation instruction in C?

No-operation instruction. Alternatively referred to as a do-nothing instruction, the no-operation instruction, NO-OP or NOP instruction in programming tells a program to do nothing if a conditional statement is met.

What does NOP mean in C programming?

Alternatively known as a do-nothing instruction, the no-operation instruction, NO-OP or NOP instruction in programming tells a program to do nothing if a conditional statement is met. In C a semicolon ( ; ) by itself or an empty block ( {} ) is an NOP.

How does a C program execute?

How does a C program executes? - GeeksforGeeks How does a C program executes? Whenever a C program file is compiled and executed, the compiler generates some files with the same name as that of the C program file but with different extensions.

What does no op mean in programming?

The no op is included in most assembler languages. It may have a label and can serve as a placeholder for a useful instruction to be inserted later during code development. The New Hacker's Dictionary reports as a derivative meaning "A person who contributes nothing to a project, or has nothing going on upstairs, or both.


3 Answers

If it really is for a ternary operator that doesn't need a second action, the best option would be to replace it for an if:

if (a!=b) cout << "not equal";

it will smell a lot less.

like image 118
Vinko Vrsalovic Avatar answered Oct 01 '22 23:10

Vinko Vrsalovic


Simple: I would code it as

if (a != b)
   cout << "not equal";

The ternary operator requires the two results to be of the same type. So you might also be able to get away with

(a != b) ? cout << "not equal" : cout;

because the stream operator (<<) just returns the ostream reference. That's ugly and unnecessary in my opinion though.

like image 39
altruic Avatar answered Oct 02 '22 00:10

altruic


The following will achieve what you're looking for, however, it may not be clear to people reading your code why it works:

(a != b) && (cout << "equal");

Personally, I agree with this answer from Vinko Vrsalovic.

like image 42
Richard Corden Avatar answered Oct 02 '22 00:10

Richard Corden