473,379 Members | 1,201 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

How does the comma acts here as an operator or as a separator?

plz explain the working of below code ,here why both the printf functions are getting executed when comma is here acting like an operator and not like a separator.
Expand|Select|Wrap|Line Numbers
  1. #‎include‬<stdio.h>
  2. int main()
  3. int i;
  4. int g= i=8?(printf("hello"),printf("life")):printf("kill");
  5. printf("%d",g);
  6. return 0;
  7. }
Feb 21 '15 #1
6 1323
weaknessforcats
9,208 Expert Mod 8TB
You have stumbled into the C "ternary operator" ? , :

It works like this:

? (condition), do if true : do if false.

So let's say we will test x and y. If x is less than y, print "LESS". If x is not less than y print "NOT LESS".

You could write:

Expand|Select|Wrap|Line Numbers
  1. if (x < y)
  2. {
  3.    printf("LESS");
  4. }
  5. else
  6. {
  7.    printf("NOT LESS");
  8. }
Or the same logic could use the ternary operator:
Expand|Select|Wrap|Line Numbers
  1. ?(x < y),  printf("LESS"): printf("NOT LESS");
Feb 21 '15 #2
Sir,I am asking about actually how the comma is acting here as an operator or as a separator,because if I write int a=(7,8) a gets the value 8 ,so then why here both the printf statements are getting printed.
Even When i write
if(10>9)
{
printf("kill"),printf("heloo");
}

so basically how this code works?
Feb 22 '15 #3
When comma used inside a paranthesis then the last argument gets executes ,so the last printf statement should execute so then why here both the printf statements get executed
Feb 22 '15 #4
donbock
2,426 Expert 2GB
A common way to write a for loop is
Expand|Select|Wrap|Line Numbers
  1. for (i=0; i<10; i++)
  2.   dosomething();
A not uncommon idiom is
Expand|Select|Wrap|Line Numbers
  1. for (i=0,j=0; i<10; i++)
  2.    dosomething();
Note the comma operator between i=0 and j=0. The effect of this operator is "first do the thing to my left, then do the thing to my right"; so both i and j get set to 0 before the loop is entered.

The comma operator isn't used much, which leads to confusion when it is used, which leads people who don't want their code to be confusing to avoid using it, and so it goes.

The comma operator introduces a sequence point. This means that the compiler is obligated to execute the arguments from left to right. The commas in a function call are separators, so not sequence points, so no similar assurance of the order that function arguments are evaluated. For
Expand|Select|Wrap|Line Numbers
  1. dosomething(j++, abc(j,k), --k)
you can't know what order the increment, abc() call, or decrement take place. Even if you instrument your code so that you think you know the order, you can't count on the order being the same the next time this snippet executes.

If this seems confusing to you then you're very perceptive.
Feb 22 '15 #5
donbock
2,426 Expert 2GB
Line 5 of the OP set i to 8, compares 8 to 0, finds that. 8 does not equal 0 (that is, that 8 is true), and prints "hello" and "world".

It only prints "kill" if 8 is equal to 0 (that is, if 8 is false ).
Feb 22 '15 #6
weaknessforcats
9,208 Expert Mod 8TB
In both C and C++ the same symbol is used for multiple purposes. This is called operator overloading and what is it means is that you must use context to determine what the meaning is.

For example:

&

1) this is the address-of operator
2) this is the bitwise AND operator
3) in C++ this is the reference operator
4) this s part of the logical AND operator

These uses are mutually exclusive. So it requires the compile to see how you use the operator in order to determine how to compile it.

The same is true with the comma.

1) this is the sequence operator
2) this is the TRUE portion of the ternary operator.

So this example:
Expand|Select|Wrap|Line Numbers
  1. if(10>9)
  2. {
  3. printf("kill"),printf("heloo");
  4. }
  5.  
is examined by the compiler and it does not find the ? of the ternary operator making the comma the sequence operator. Hence, the printf calls are done one after the other.
Feb 22 '15 #7

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

Similar topics

4
by: PKH | last post by:
This is a part of an autopointer template i'm using. I originally had a Get() member that returned the pointer, but thought it would nice to be able to use the -> operator. The question is why...
4
by: Radde | last post by:
Hi, class String { public: String(char* str); void operator char*();
16
by: Mars | last post by:
I am reading the code of a program from the examples, but really don't understand this one........ int i,j; ..................................................... while (scanf("%d",&i),j) {...
2
by: Robert W. | last post by:
I seem to remember in VB6 that one could add a string item consisting of two hypens and that would force a line to be drawn in a listbox. How does one add a separator bar in a listbox in C#.net?...
0
by: Kaja | last post by:
Hi, Compare validator throws error if I have thousand seperator in the double value. ControlToValidate: TextBox1 Operator: DataTypeCheck Type: Double Any help appreciated.
13
by: Jon Cosby | last post by:
VB .Net does support operator overloading, doesn't it? It seems like this should overload binary addition, but VB doesn't recognize "Operator" Public Shared Operator +(ByVal c1 as cnum, ByVal c2...
4
by: gobis | last post by:
Hello everyone, After I could not overload the << operator as a friend operator in my template class (yes, it did not have to be a template class, but I was testing something for the class I was...
13
by: Tristan Wibberley | last post by:
Hi I've got implementing overloaded operator new and delete pretty much down. Just got to meet the alignment requirements of the class on which the operator is overloaded. But how does one...
2
by: john | last post by:
Hi, in TC++PL3 on page 665, regarding valarray member functions, it is mentioned: "valarray operator-() const; // result= -v for every element // similarly: +, ~, !" I checked the web and...
1
by: pozzobon | last post by:
Hello I live in Brazil and here we use comma (,) as decimal separator and period (.) as thousands separator. How can I show my values in MySQL using the Brazilian way? MySQL shows this:...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.