Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > how the following two code parts same

Reply
Thread Tools

how the following two code parts same

 
 
saipathakota@gmail.com
Guest
Posts: n/a
 
      12-07-2007
how the following two code parts same:

#include <stdio.h>

void main ()
{

char i = NULL;

char &q = i;
^^^^

printf ( "%d", i);

printf ("%d", q);
}



O/p.................. 0 0



#include <stdio.h>

void main ()
{

char i = NULL;

char q = i;
^^

printf ( "%d", i);

printf ("%d", q);
}


O/p ................ 0 0
 
Reply With Quote
 
 
 
 
fred.l.kleinschmidt@boeing.com
Guest
Posts: n/a
 
      12-07-2007
On Dec 7, 1:00 pm, saipathak...@gmail.com wrote:
> how the following two code parts same:
>
> #include <stdio.h>
>
> void main ()
> {
>
> char i = NULL;
>
> char &q = i;
> ^^^^
>
> printf ( "%d", i);
>
> printf ("%d", q);
>
> }
>
> O/p.................. 0 0
>
> #include <stdio.h>
>
> void main ()
> {
>
> char i = NULL;
>
> char q = i;
> ^^
>
> printf ( "%d", i);
>
> printf ("%d", q);
>
> }
>
> O/p ................ 0 0


They are the same except for the lines
> char &q = i;

and
> char q = i;


--
Fred
 
Reply With Quote
 
 
 
 
saipathakota@gmail.com
Guest
Posts: n/a
 
      12-07-2007
On Dec 8, 2:03 am, fred.l.kleinschm...@boeing.com wrote:
> On Dec 7, 1:00 pm, saipathak...@gmail.com wrote:
>
>
>
> > how the following two code parts same:

>
> > #include <stdio.h>

>
> > void main ()
> > {

>
> > char i = NULL;

>
> > char &q = i;
> > ^^^^

>
> > printf ( "%d", i);

>
> > printf ("%d", q);

>
> > }

>
> > O/p.................. 0 0

>
> > #include <stdio.h>

>
> > void main ()
> > {

>
> > char i = NULL;

>
> > char q = i;
> > ^^

>
> > printf ( "%d", i);

>
> > printf ("%d", q);

>
> > }

>
> > O/p ................ 0 0

>
> They are the same except for the lines
>
> > char &q = i;

> and
> > char q = i;

>
> --
> Fred


i am asking how both code is showing same output.
 
Reply With Quote
 
Lew Pitcher
Guest
Posts: n/a
 
      12-07-2007
wrote:
> how the following two code parts same:
>
> #include <stdio.h>
>
> void main ()


Nope. In a hosted environment, main() returns int by definition. You really mean
int main(void)
here

> {
>
> char i = NULL;


You should see a diagnostic indicating that you are converting a pointer to an
integer (type) without a cast. Having said that, i should now be initialized
to zero.

>
> char &q = i;


Syntax error. Your program should not compile.

>
> printf ( "%d", i);
>
> printf ("%d", q);
> }
>
>
>
> O/p.................. 0 0


I don't believe you.

>
>
>
> #include <stdio.h>
>
> void main ()

Again,
int main(void)

> {
>
> char i = NULL;


You should see a diagnostic indicating that you are converting a pointer to an
integer (type) without a cast. Having said that, i should now be initialized
to zero.

> char q = i;


q is initialized to the contents of i. Since i is initialized to zero and not
changed before this statement, q should now also be set to zero.

>
> printf ( "%d", i);
>
> printf ("%d", q);
> }
>
>
> O/p ................ 0 0


OK

So, what's your problem?

--
Lew Pitcher

Master Codewright & JOAT-in-training | Registered Linux User #112576
http://pitcher.digitalfreehold.ca/ | GPG public key available by request
---------- Slackware - Because I know what I'm doing. ------

 
Reply With Quote
 
saipathakota@gmail.com
Guest
Posts: n/a
 
      12-07-2007
On Dec 8, 2:13 am, Lew Pitcher <lpitc...@teksavvy.com> wrote:
> saipathak...@gmail.com wrote:
> > how the following two code parts same:

>
> > #include <stdio.h>

>
> > void main ()

>
> Nope. In a hosted environment, main() returns int by definition. You really mean
> int main(void)
> here
>
> > {

>
> > char i = NULL;

>
> You should see a diagnostic indicating that you are converting a pointer to an
> integer (type) without a cast. Having said that, i should now be initialized
> to zero.
>
>
>
> > char &q = i;

>
> Syntax error. Your program should not compile.
>
>
>
> > printf ( "%d", i);

>
> > printf ("%d", q);
> > }

>
> > O/p.................. 0 0

>
> I don't believe you.
>
>
>
> > #include <stdio.h>

>
> > void main ()

>
> Again,
> int main(void)
>
> > {

>
> > char i = NULL;

>
> You should see a diagnostic indicating that you are converting a pointer to an
> integer (type) without a cast. Having said that, i should now be initialized
> to zero.
>
> > char q = i;

>
> q is initialized to the contents of i. Since i is initialized to zero and not
> changed before this statement, q should now also be set to zero.
>
>
>
> > printf ( "%d", i);

>
> > printf ("%d", q);
> > }

>
> > O/p ................ 0 0

>
> OK
>
> So, what's your problem?
>
> --
> Lew Pitcher
>
> Master Codewright & JOAT-in-training | Registered Linux User #112576http://pitcher.digitalfreehold.ca/ | GPG public key available by request
> ---------- Slackware - Because I know what I'm doing. ------


i compiled in the vc++ .. o/p is same .. is it true with the C++
 
Reply With Quote
 
Default User
Guest
Posts: n/a
 
      12-07-2007
Lew Pitcher wrote:


> > char i = NULL;

>
> You should see a diagnostic indicating that you are converting a
> pointer to an integer (type) without a cast.


You mean you might see a diagnostic. There's an excellent chance that
NULL is not a pointer type at all.




Brian
 
Reply With Quote
 
Default User
Guest
Posts: n/a
 
      12-07-2007
wrote:


> > --
> > Lew Pitcher
> >
> > Master Codewright & JOAT-in-training | Registered Linux User


Please trim quotes to the minimum necessary for context. Especially
trim the signatures, the parts after -- (I left part in above as an
example).

> i compiled in the vc++ .. o/p is same .. is it true with the C++


VC++ can compile C programs. The first code you showed was NOT a C
program, as this:

char &q = i;

Is not a valid C construct. That looks a lot like a reference in C++.
You should probably be posting in comp.lang.c++.





Brian
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      12-07-2007
writes:
> how the following two code parts same:


please don't double-space posted code. It just makes it harder to read.

> #include <stdio.h>
> void main ()
> {
> char i = NULL;
> char &q = i;
> ^^^^
> printf ( "%d", i);
> printf ("%d", q);
> }


The above is not C. It appears to be C++. Consulting your C++
textbook or the C++ FAQ for information about "references" should
answer your question. Failing that, try comp.lang.c++.

> #include <stdio.h>
> void main ()


Wrong. Make this "int main(void)" (or "int main()" if you mean to
write C++).

> {
> char i = NULL;


NULL is a null *pointer* constant. Assigning it to a char object
might happen to work, but it makes no sense. You probably mean
char i = '\0';
or
char i = 0;
(the two are equivalent).

> char q = i;
> ^^
> printf ( "%d", i);
> printf ("%d", q);
> }


You claim that the output is "0 0", but neither of your printf calls
prints a space character. If your program compiles and runs at all,
I'd expect the output to be "00". Either the code you posted isn't
your actual code, or the output you posted isn't your actual output.
You should copy-and-paste both your code and your output *exactly*;
don't try to re-type or paraphrase them.

You should print a new-line ("\n") at the end of your output. You
should also return a value from main(); add "return 0;" before the
closing brace. (This isn't strictly necessary in some circumstances,
but it's always a good idea.)

If you want the output to be understandable, consider something like:

printf("i = %d, q = %d\n", i, q);

--
Keith Thompson (The_Other_Keith) <kst->
Looking for software development work in the San Diego area.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
jameskuyper@verizon.net
Guest
Posts: n/a
 
      12-07-2007
Default User wrote:
> wrote:

....
> > i compiled in the vc++ .. o/p is same .. is it true with the C++

>
> VC++ can compile C programs. The first code you showed was NOT a C
> program, as this:
>
> char &q = i;
>
> Is not a valid C construct. That looks a lot like a reference in C++.
> You should probably be posting in comp.lang.c++.


When you do post to comp.lang.c++, they will tell you that the results
you got are precisely the results you should have expected to get
(ignoring the defective declarations of main()). Save them some time,
and in your first message to that newsgroup, try to explain why it is
that you are surprised by these results. Then they will be able to
explain to you what it is that you're misunderstanding; because you
are misunderstanding something.
 
Reply With Quote
 
jameskuyper@verizon.net
Guest
Posts: n/a
 
      12-07-2007
Keith Thompson wrote:
> writes:

....
> > #include <stdio.h>
> > void main ()
> > {
> > char i = NULL;
> > char &q = i;
> > ^^^^
> > printf ( "%d", i);
> > printf ("%d", q);
> > }

>
> The above is not C. It appears to be C++. Consulting your C++
> textbook or the C++ FAQ for information about "references" should
> answer your question. Failing that, try comp.lang.c++.
>
> > #include <stdio.h>
> > void main ()

>
> Wrong. Make this "int main(void)" (or "int main()" if you mean to
> write C++).
>
> > {
> > char i = NULL;

>
> NULL is a null *pointer* constant. Assigning it to a char object
> might happen to work, but it makes no sense. You probably mean


Given that it is C++ code, it will work. In C++, NULL has to have a
value of 0; it cannot have a pointer type.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Serious issue: parts of my page render as not logged in, parts as logged in. Help! pcloches@gmail.com ASP .Net 1 04-12-2007 12:50 AM
Difference between "library parts" of C99 and "language parts" of C99 albert.neu@gmail.com C Programming 3 03-31-2007 08:14 PM
Parts parts....PARTS!!! ARGHHH dstvns A+ Certification 8 01-07-2004 07:57 PM
How come most multi parts dont have all parts? 123 Computer Support 2 09-22-2003 01:55 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57