473,721 Members | 2,235 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

comma operator and assignment operator

Hi,

I've read a book on C, and I understand how comma operators work, but
my book didn't say that the comma operators between function arguments
were not really comma operators (even though it seems obvious to me
that comma operators would serve no purpose between function
arguments). As per C, are those commas in function argument lists the
same comma operators?

Also, are the =s used in initializations the same as any other
=assignment operator? I know some people keep saying 'initialization s
and assignments are different!' But if they are truly different, then
the = used with initializations are not really 'assignment' operators.
So are they the same operator? If yes, then why do people complain that
initializations and assignments are different?

To me the following seem the same at execution time:

int a = 4;

vs.

int a;
a=4;

Nov 14 '05 #1
4 2197
G Patel wrote:

Hi,

I've read a book on C, and I understand how comma operators work, but
my book didn't say that the comma operators
separators
between function arguments
were not really comma operators (even though it seems obvious to me
that comma operators would serve no purpose between function
arguments). As per C, are those commas in function argument lists the
same comma operators?
No. See K&R2 p63 paragraph 1.
Also, are the =s used in initializations the same as any other
=assignment operator?
Same symbol, different purpose.
I know some people keep saying 'initialization s
and assignments are different!' But if they are truly different, then
the = used with initializations are not really 'assignment' operators.
So are they the same operator? If yes, then why do people complain that
initializations and assignments are different?

To me the following seem the same at execution time:

int a = 4;

vs.

int a;
a=4;


Try this:

char a[] = {1, 2, 3};

vs.

char a[];
a = {1, 2, 3};
Nov 14 '05 #2
Neo

"G Patel" <ga********@gma il.com> wrote in message
news:11******** **************@ l41g2000cwc.goo glegroups.com.. .
Hi,

I've read a book on C, and I understand how comma operators work, but
my book didn't say that the comma operators between function arguments
were not really comma operators (even though it seems obvious to me
that comma operators would serve no purpose between function
arguments). As per C, are those commas in function argument lists the
same comma operators?

Also, are the =s used in initializations the same as any other
=assignment operator? I know some people keep saying 'initialization s
and assignments are different!' But if they are truly different, then
the = used with initializations are not really 'assignment' operators.
So are they the same operator? If yes, then why do people complain that
initializations and assignments are different?

To me the following seem the same at execution time:

int a = 4;

vs.

int a;
a=4;


Have a look at this:

int status = 1;
int main(void)
{
....
}

differs from

int status;
int main(void)
{
status = 1;
....
}

in first program status is initialized with value 1, in second program space
is reserved for the variable status, value is assigned later when main()
starts executing.

-Neo
Nov 14 '05 #3
On Sun, 06 Feb 2005 22:44:35 -0800, G Patel wrote:
Hi,

I've read a book on C, and I understand how comma operators work, but
my book didn't say that the comma operators between function arguments
were not really comma operators (even though it seems obvious to me
that comma operators would serve no purpose between function
arguments). As per C, are those commas in function argument lists the
same comma operators?
No, the comma character like some others is used in various different
syntactic contexts. Just because it is the same character doesn't imply it
means the same thing. The contexts include

expr1, expr2 /* comma operator */
func(arg1, arg2) /* function call */
int main(int argc, char *argv) /* function declaration or definition */
int a, b, c /* declarator list */
enum { A, B, C } /* enumerator list */
int a[] = { 1, 2, 3 } /* Initialiser list */

Another example is the ( and ) characters

(expr) /* grouping parentheses */
func(args) /* function call */
(type)expr /* Cast */
Also, are the =s used in initializations the same as any other
=assignment operator? I know some people keep saying 'initialization s
and assignments are different!'
Yes, again it is a different syntactic construct
But if they are truly different, then
the = used with initializations are not really 'assignment' operators.
Correct, although there are some similarities.
So are they the same operator? If yes, then why do people complain that
initializations and assignments are different?
Consider for example the initialisatiion

char a[] = "A string";

There is no direct equivalent assignment e.g.

a = "A string"; /* Invalid */
To me the following seem the same at execution time:

int a = 4;

vs.

int a;
a=4;


Those do have the same effect, but that's not always true, for example

static int a = 4;

and

static int a;
a=4;

do not behave the same.

Lawrence
Nov 14 '05 #4
On Mon, 7 Feb 2005 13:19:10 +0530, "Neo" <ti************ ***@yahoo.com>
wrote:

"G Patel" <ga********@gma il.com> wrote in message
news:11******* *************** @l41g2000cwc.go oglegroups.com. ..
Hi,

I've read a book on C, and I understand how comma operators work, but
my book didn't say that the comma operators between function arguments
were not really comma operators (even though it seems obvious to me
that comma operators would serve no purpose between function
arguments). As per C, are those commas in function argument lists the
same comma operators?

Also, are the =s used in initializations the same as any other
=assignment operator? I know some people keep saying 'initialization s
and assignments are different!' But if they are truly different, then
the = used with initializations are not really 'assignment' operators.
So are they the same operator? If yes, then why do people complain that
initializations and assignments are different?

To me the following seem the same at execution time:

int a = 4;

vs.

int a;
a=4;


Have a look at this:

int status = 1;
int main(void)
{
....
}

differs from

int status;
int main(void)
{
status = 1;
....
}

in first program status is initialized with value 1, in second program space
is reserved for the variable status, value is assigned later when main()
starts executing.


Actually, in the second, status is initialized to 0 and then its value
is changed to 1 when main executes.
<<Remove the del for email>>
Nov 14 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
8031
by: Paul Davis | last post by:
I'd like to overload 'comma' to define a concatenation operator for integer-like classes. I've got some first ideas, but I'd appreciate a sanity check. The concatenation operator needs to so something like this: 1) e = (a, b, c, d); // concatenate a,b,c,d into e 2) (a, b, c, d) = e; // get the bits of e into a,b,c, and d For example, in the second case, assume that a,b,c,d represent 2-bit integers, and e represents an 8-bit...
16
2615
by: Edward Diener | last post by:
Is there a way to override the default processing of the assignment operator for one's own __value types ? I realize I can program my own Assign method, and provide that for end-users of my class, but I would like to use internally my own = operator for some of my value types, so I can say "x = y;" rather than "x.Assign(y);". The op_Assign operator seems impossibly broken since it takes __value copies of the two objects. Perhaps there is...
5
2567
by: Derek | last post by:
I came upon the idea of writting a logging class that uses a Python-ish syntax that's easy on the eyes (IMO): int x = 1; double y = 2.5; std::string z = "result"; debug = "Results:", x, y, z; The above example outputs:
2
2290
by: benben | last post by:
I am looking for a good example of overloading operator , (operator comma) Any suggestions? Ben
11
2387
by: Shawn Odekirk | last post by:
Some code I have inherited contains a macro like the following: #define setState(state, newstate) \ (state >= newstate) ? \ (fprintf(stderr, "Illegal state\n"), TRUE) : \ (state = newstate, FALSE) This macro is called like this: setState(state, ST_Used);
21
3035
by: siliconwafer | last post by:
Hi, In case of following expression: c = a && --b; if a is 0,b is not evaluated and c directly becomes 0. Does this mean that && operator is given a higher precedence over '--'operator? as opposed to what is mentioned in precedence table? Also, with comma operator. consider,
7
2623
by: JAY | last post by:
There is an example which confuses me, int main() { int a,b,c,d; a=3; b=5; c=a,b; d=(a,b); printf("c=%d",c); printf("d=%d",d);
5
2292
by: raylopez99 | last post by:
I need an example of a managed overloaded assignment operator for a reference class, so I can equate two classes A1 and A2, say called ARefClass, in this manner: A1=A2;. For some strange reason my C++.NET 2.0 textbook does not have one. I tried to build one using the format as taught in my regular C++ book, but I keep getting compiler errors. Some errors claim (contrary to my book) that you cannot use a static function, that you must...
15
2628
by: Lighter | last post by:
In 5.3.3.4 of the standard, the standard provides that "The lvalue-to- rvalue(4.1), array-to-pointer(4.2),and function-to-pointer(4.3) standard conversions are not applied to the operand of sizeof." I think this rule is easy to understand. Because I can find the contexts of applying the rule as follows. (1) int* p = 0; int b1 = sizeof(*p); // OK, b1 = 4, *p would not be evaluated.
0
8730
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9367
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9215
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9131
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9064
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
4753
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3189
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 we have to send another system
2
2576
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2130
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.