75860

what does the colon do in c?

I was given this example in class and I am unsure what it does. I understand the colon adds a bit-field, but I am still uncertain about this question:

a = b > 0 ? 3:1;

Answer1:

?: operator is called the conditional operator.

If b value is > 0, the value 3 is assigned to a else the value 1 is assigned to a.

Take your Kernighan & Ritchie book 2nd edition, chapter 2.11 Conditional expressions, the behavior of the operator is explained.

Answer2:

? is a conditional operator:

a = b > 0 ? 3:1;

is equivalent to:

if(b > 0) a = 3; else a = 1;

Answer3:

This is the conditional operator. It's equivalent to:

if (b > 0) a = 3; else a = 1;

Read it as "a = if b > 0 then 3 else 1".

Answer4:

It's the conditional operator (generally called the ternary operator), which is used as a short way of writing if statements.

In general, it can be read:

condition ? value_if_true : value_if_false

So, in your case:

a = b > 0 ? 3:1;

Can be rewritten as:

if(b > 0) a = 3; else a = 1;

The colon in this example doesn't mean anything related to bit fields - it's just the second part of the conditional.

Recommend

  • When do I need to cast the result of malloc in C language? [duplicate]
  • Variable Length Arguments without using stdarg.h
  • sp_refreshView replaces view implementation when sp_rename has occured
  • sbt stuck while downloading
  • What are scala's rules for resolving conflicting implicit values
  • Compile Spark for Scala 2.11
  • When is it better for an assembler to use sign extended relocation like R_X86_64_32S instead of zero
  • adMob | Extend Banner size?
  • The C Programming Language K&R exercise 1- 9
  • CSS Local Font not showing up
  • SQL Count. How can I count how many distinct values are in a table when an other two columns are mat
  • iText 7: How to build a paragraph mixing different fonts?
  • Does Julia have something equivalent to Ans (Matlab) or Last.value (R)
  • Android App with ASP.NET WebAPi Server - send complex types
  • SOLR - Querying Facets, return N results per Facet
  • how to force the use of cmov in gcc and VS
  • Conditional serialization with protobuf-net
  • cannot be assigned to — it is read only - C#
  • Compiling Haskell programs in Windows: is it possible without downloading something such as Cygwin?
  • iText as text Extracting/Reading from PDF on android
  • Which table should be Parent table and which should be child table?
  • Any command in mysql equivalent to Oracle's autotrace for performance turning
  • draw a B+ tree in latex
  • Add custom field for WooCommerce CSV Export plugin - For customer first order [closed]
  • how to automatically enter password when using ssh?
  • Capturing STDOUT in RSpec
  • ASP.NET MVC2 Error: No parameterless constructor defined for this object
  • CUDA NSight is not installed with CUDA 5.0 installation file on Windows 8? [closed]
  • How to load gif image while ajax content is loading and javascript [duplicate]
  • How to assign byte[] as a pointer in C#
  • Meteor: Do Something On Email Verification Confirmation
  • Sails.js/waterline: Executing waterline queries in toJSON function of a model?
  • Rearranging Cells in UITableView Bug & Saving Changes
  • Why winpcap requires both .lib and .dll to run?
  • Circular dependency while pushing http interceptor
  • Linker errors when using intrinsic function via function pointer
  • How to set the response of a form post action to a iframe source?
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • Are Kotlin's Float, Int etc optimised to built-in types in the JVM? [duplicate]
  • unknown Exception android