Jump to content

can't make sense of the working of a code written by me some months ago

- - - - -

  • Please log in to reply
25 replies to this topic

#1
jackson6612

jackson6612

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 304 posts
Hi

I wrote the code below some months ago. Now I'm unable to understand it's working. At the bottom is given its output which also has my queries inline. Please help me with them. Thanks.



#include <iostream>

#include <cstdlib>


using namespace std;


const int C = 5;

char str[C];


int main()

{

    cout << "\"";

    for (int i = 0; i < C; ++i)

    {

        cout << str[i];

    }

    cout << "\"" << endl;


    for (int i = 0; i < C; ++i)

    {

        cout << static_cast<int>(str[i]);

    }

    cout << endl;


    cout << static_cast<int>(' ') << endl;


    system("pause");

    return 0;

}


OUTPUT:

"     " // [COLOR="#FF0000"]it shows as if each element of char str[C] contains empty space, shouldn't it be some random value[/COLOR]

00000 // [COLOR="#FF0000"]why all these 0s?[/COLOR]

32 // [COLOR="#FF0000"]what is this "32"?[/COLOR]

Press any key to continue . . .


I'm an outright beginner, learning C++. Using Win XP Pro and Code::Blocks. Be nice to me, please.:)

#2
mebob

mebob

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 490 posts
1. That array was probably initialized to zeros (nulls) which I guess will appear as spaces on the command line.
2. Again, probably initialized.
3. That is the ASCII code for a space. Here is a table that maps ASCII codes to their respective characters: http://www.cdrummond...ages/ASCII1.GIF
Latinamne loqueris?

#3
jackson6612

jackson6612

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 304 posts
Thank you, Collin.

So, can I conclude that array are initialized to nulls by default? I seem to remember a character string is terminated by a null.

I believe there is a difference between a null and zero. A null is represented by "\0" while zero is just written "0". Please correct me.

Thank you for the help.
I'm an outright beginner, learning C++. Using Win XP Pro and Code::Blocks. Be nice to me, please.:)

#4
mebob

mebob

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 490 posts
Actually, there is no defined behavior for an uninitialized array. So it could be different on different compilers or systems.

And they are the same thing. When you do "\0" it is just a value of 0, really.
Latinamne loqueris?

#5
jackson6612

jackson6612

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 304 posts
Thanks.

But on the link you provided null is assigned decimal value of "0" and zero has value of 48.
I'm an outright beginner, learning C++. Using Win XP Pro and Code::Blocks. Be nice to me, please.:)

#6
mebob

mebob

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 490 posts
I know, I meant an integer value of zero when I said "value of 0".
Latinamne loqueris?

#7
AKMafia001

AKMafia001

    Programmer

  • Members
  • PipPipPipPip
  • 119 posts
"     " // it shows as if each element of char str[C] contains empty space, shouldn't it be some random value

00000 // why all these 0s?

32 // what is this "32"?

Press any key to continue . . .


I love to think in code.....

Some of the result is specific to your Code::Blocks. The variables doesn't initialize by default ( by itself ). We have to initialize them.

Code::Blocks uses MinGW compiler and,

cout << str[i]; \\ prints "     "

cout << static_cast<int>(str[i]); \\ prints 00000


While my MinGW with Eclipse's output is,

"#v#@#" \\ there is no # in the output. It's the small box which even doesn't compiles here :).

011827640 \\ same array with int conversion.

32


Let's talk about the 32.

cout << static_cast<int> (' ') ;

The above statment tells that, convert the character space to int. If you look at the ASCII character table the ASCII code of character space is 32.

Did I left something to discuss?

---------- Post added at 09:23 PM ---------- Previous post was at 09:18 PM ----------

jackson6612 said:

Thanks.

But on the link you provided null is assigned decimal value of "0" and zero has value of 48.

Dear jack!
The number 0 doesn't means to be zero. Taking it as character 0 will have Decimal - 48, Hexadecimal - 30, Octal - 060, Character - 0.
While considering it as a number 0 will have, Decimal - 0, Hexadecimal - 0, Octal - 0000 and character- NUL.
I think i'm able to write a code for printing "Hello, World!". Proud of that!

#8
jackson6612

jackson6612

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 304 posts

AKMafia001 said:

"     " // it shows as if each element of char str[C] contains empty space, shouldn't it be some random value

00000 // why all these 0s?

32 // what is this "32"?

Press any key to continue . . .


I love to think in code.....

Some of the result is specific to your Code::Blocks. The variables doesn't initialize by default ( by itself ). We have to initialize them.

Code::Blocks uses MinGW compiler and,

cout << str[i]; \\ prints "     "

cout << static_cast<int>(str[i]); \\ prints 00000


While my MinGW with Eclipse's output is,

"#v#@#" \\ there is no # in the output. It's the small box which even doesn't compiles here :).

[B][COLOR="#FF0000"]011827640[/COLOR][/B] \\ same array with int conversion.

32


Let's talk about the 32.

cout << static_cast<int> (' ') ;

The above statment tells that, convert the character space to int. If you look at the ASCII character table the ASCII code of character space is 32.

Did I left something to discuss?

---------- Post added at 09:23 PM ---------- Previous post was at 09:18 PM ----------



Dear jack!
The number 0 doesn't means to be zero. Taking it as character 0 will have Decimal - 48, Hexadecimal - 30, Octal - 060, Character - 0.
While considering it as a number 0 will have, Decimal - 0, Hexadecimal - 0, Octal - 0000 and character- NUL.


Thanks a lot, AK. It's good to see you again! :) The queries below are going to be somewhat verbose. Please don't mind. Thanks.

So, I conclude that char array contains just some random values. In my case they were zeros.

"011827640" - This output we get after the conversion. e.g. something was converted and we get decimal 7. Therefore we can look up ASCII table to see which character has the designated "7" as its decimal value. It's character BEL. Is my interpretation correct? In my original post as you see I get "00000" after the conversion, and "0" stands for NUL. But in this line
"     " // it shows as if each element of char str[C] contains empty space, shouldn't it be some random value
the output consisted of empty spaces not any NUL. Is NUL an empty space?
I'm an outright beginner, learning C++. Using Win XP Pro and Code::Blocks. Be nice to me, please.:)

#9
AKMafia001

AKMafia001

    Programmer

  • Members
  • PipPipPipPip
  • 119 posts

    for(i = 0; i < 5; i++)

    {

        printf("\n%s",str[i]);

    }


Output:

(null)




    for(i = 0; i < 5; i++)

    {

        printf("\n%d",str[i]);

    }


Output:

0's


They are not actually random values. It's a data stored by another program which occupied this location. It seems wired to us -- so we call it garbage value.

Quote

"011827640"

We can't figure it out like this, we don't know what this might be -- a calculated sum, some input or output to this location or something else.
I think i'm able to write a code for printing "Hello, World!". Proud of that!

#10
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1
Global variables are initialized to zero. It's a cheap trick when you're too lazy to run an array through a loop to initialize it.
A conclusion is where you got tired of thinking.
#define class struct    // All is public.

#11
jackson6612

jackson6612

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 304 posts
Thank you, AK.

jackson6612 said:

Hi

I wrote the code below some months ago. Now I'm unable to understand it's working. At the bottom is given its output which also has my queries inline. Please help me with them. Thanks.



#include <iostream>

#include <cstdlib>


using namespace std;


const int C = 5;

char str[C];


int main()

{

    cout << "\"";

    [B]for (int i = 0; i < C; ++i)

    {

        cout << [COLOR="#00FF00"]str[i][/COLOR];

    }[/B]

    cout << "\"" << endl;


    for (int i = 0; i < C; ++i)

    {

        cout << static_cast<int>(str[i]);

    }

    cout << endl;


    cout << static_cast<int>(' ') << endl;


    system("pause");

    return 0;

}


OUTPUT:

"     " // [COLOR="#FF0000"]it shows as if each element of char str[C] contains empty space, shouldn't it be some random value[/COLOR]

00000 // [COLOR="#FF0000"]why all these 0s?[/COLOR]

32 // [COLOR="#FF0000"]what is this "32"?[/COLOR]

Press any key to continue . . .


Thank you, Dutchman.

That means the array used by me had been initialized to 0's. Then, why did I get empty spaces when I cout'ed the array elements. I only got 0's when I used static_cast<>. Could you please explain the output in my post? Thanks.

Edited by jackson6612, 10 October 2011 - 11:58 AM.

I'm an outright beginner, learning C++. Using Win XP Pro and Code::Blocks. Be nice to me, please.:)

#12
mebob

mebob

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 490 posts
The console displays nulls as 'spaces'. At least, the Windows console does.
Latinamne loqueris?




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users