3 Replies - 780 Views - Last Post: 25 November 2013 - 05:37 PM

#1 AnalyticLunatic  Icon User is offline

  • D.I.C Lover

Reputation: 218
  • View blog
  • Posts: 1,004
  • Joined: 25-June 12

Exercise: Does Casting INT to CHAR in C++/Java result in Error?

Posted 25 November 2013 - 10:47 AM

So I was working through some exercises in one of my class books when I came to the following:

  • For your C/C++ compiler, verify whether or not the cast of the interger257 to a char results in an error
  • For your Java compiler/interpreter, verify whether or not the case of the integer 655537 to a char results in an error.


I don't have a Java compiler installed on this machine, so I tried both cases in a C++ Console App:

Code:
#include "stdafx.h"
#include <iostream>
using namespace std;

int main()
{
    int a = 257;
    char b = a;
    int c = 655537;
    char d = c;

    cout << a << " " << b << endl;
    cout << c << " " << d << endl;
    system("pause");
}



Output:
Attached Image

  • Why does the one output what appears to be a smiley face, and the other a box of static?
  • Would this be the same result in a Java Compiler?
  • Unrelated: At the bottom of my Text Editor it says "Used 9.75MB of your 9.77MB global upload quota (Max. single file size : 29.59K). What exactly does this mean and how do I reset/lower it?


Is This A Good Question/Topic? 0
  • +

Replies To: Exercise: Does Casting INT to CHAR in C++/Java result in Error?

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10188
  • View blog
  • Posts: 37,629
  • Joined: 27-December 08

Re: Exercise: Does Casting INT to CHAR in C++/Java result in Error?

Posted 25 November 2013 - 03:43 PM

Moved to Software Development.

Please stop posting programming questions in Student Campus, which is the wrong forum. There is a sticky saying "No Programming Questions Here." Shoot me a PM if there is any confusion with this. I'm tired of moving your threads.

You can use IDEOne.com to test Java code.
Was This Post Helpful? 1
  • +
  • -

#3 AnalyticLunatic  Icon User is offline

  • D.I.C Lover

Reputation: 218
  • View blog
  • Posts: 1,004
  • Joined: 25-June 12

Re: Exercise: Does Casting INT to CHAR in C++/Java result in Error?

Posted 25 November 2013 - 03:54 PM

Sorry Mac! I assumed that since it was relevant to classwork, student campus would be appropriate...

I'll check out the link, thanks.
Was This Post Helpful? 0
  • +
  • -

#4 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3177
  • View blog
  • Posts: 9,632
  • Joined: 05-May 12

Re: Exercise: Does Casting INT to CHAR in C++/Java result in Error?

Posted 25 November 2013 - 05:37 PM

The smiley face corresponds to the ASCII character 1, which has a smiley face glyph in DOS and its descendants. Scroll about halfway down this page to see the other characters for the low ASCII range: http://www.jimprice.com/jim-asc.shtml
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1