how the following two code parts same

Discussion in 'C Programming' started by saipathakota@gmail.com, Dec 7, 2007.

  1. Guest

    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
    , Dec 7, 2007
    #1
    1. Advertising

  2. Guest

    On Dec 7, 1:00 pm, 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
    , Dec 7, 2007
    #2
    1. Advertising

  3. Guest

    On Dec 8, 2:03 am, wrote:
    > On Dec 7, 1:00 pm, 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.
    , Dec 7, 2007
    #3
  4. Lew Pitcher Guest

    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. ------
    Lew Pitcher, Dec 7, 2007
    #4
  5. Guest

    On Dec 8, 2:13 am, Lew Pitcher <> wrote:
    > 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++
    , Dec 7, 2007
    #5
  6. Default User Guest

    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
    Default User, Dec 7, 2007
    #6
  7. Default User Guest

    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
    Default User, Dec 7, 2007
    #7
  8. 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) <>
    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"
    Keith Thompson, Dec 7, 2007
    #8
  9. Guest

    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.
    , Dec 7, 2007
    #9
  10. Guest

    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.
    , Dec 7, 2007
    #10
  11. writes:
    > Keith Thompson wrote:
    >> writes:

    [...]
    >> > {
    >> > 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.


    True, but using NULL for anything other than a pointer value is poor
    style in any case.

    --
    Keith Thompson (The_Other_Keith) <>
    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"
    Keith Thompson, Dec 8, 2007
    #11
  12. James Kuyper Guest

    Keith Thompson wrote:
    > writes:
    >> Keith Thompson wrote:
    >>> writes:

    > [...]
    >>>> {
    >>>> 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.

    >
    > True, but using NULL for anything other than a pointer value is poor
    > style in any case.


    Agreed.
    James Kuyper, Dec 8, 2007
    #12
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    3
    Views:
    582
    Keith Thompson
    Mar 31, 2007
  2. Replies:
    1
    Views:
    938
    =?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=
    Apr 12, 2007
  3. kizk
    Replies:
    0
    Views:
    560
  4. bluebaron
    Replies:
    3
    Views:
    726
    Jonathan N. Little
    Nov 4, 2009
Loading...

Share This Page