FAQ: Variables: Lesson - Casting Types Continued

This community-built FAQ covers the “Casting Types Continued” exercise from the lesson “Variables: Lesson”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Learn C

FAQs on the exercise Casting Types Continued

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!
You can also find further discussion and get answers to your questions over in #get-help.

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head to #get-help and #community:tips-and-resources. If you are wanting feedback or inspiration for a project, check out #project.

Looking for motivation to keep learning? Join our wider discussions in #community

Learn more about how to use this guide.

Found a bug? Report it online, or post in #community:Codecademy-Bug-Reporting

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

Hello, fellow coders, am I supposed to see a pattern or is there a way I could predict the output target 7 before the code is executed?

1 Like

Hi mega!
I just tried a lot of possibilities out and here is a list of what I found:

  • 0 to 32 → nothing
  • 33 to 47 → punctuation marks, symbols
  • 48 to 57 → numbers (0 through 9)
  • 58 to 64 → punctuation marks, symbols
  • 65 to 90 → alphabet in capital letters
  • 91 to 96 → punctuation marks, symbols
  • 97 to 122 → alphabet in lowercase letters
  • 123 to 127 → puncutation marks, symbols
  • above → undefined
    Beware not to enter 128 (or above) and cast it to a char. It is not defined and the program will not execute, since chars are only defined from 0 to 127. Of course the same goes for values below 0.

Hello, yes, you can predict number 7 before execute code. Here how it works.
First, when you give targetChar value of sourceInt, casting it with (char) type, you get letter c (lowercase), because 99 is ASCII value letter c . They mention that 97 is letter a . Next, when you change value on variable targetChar to sourceDouble, because value of sourceDouble is 55.67 , you will get 7 , because 55 is ASCII value for character 7. It’s using 55, not 55.67 because in C , you don’t round like in mathematics, program only uses whole number. If you had 55.99, it will use 55 again.
Char 7 here is not same like number 7 .
For example :

int sourceNum = 55;
char Num1 = (char)sourceNum;
int Num2 = 7;

When you print Num1 and Num2, you will get both value of 7 , but they are not same. First is char 7, and second is number 7 .
If you want to sum them, you won’t get 14 .

#include <stdio.h>
int main() {
  int sourceNum=55;
  char Num1;
  int Num2=7;
  Num1=(char)sourceNum;
  // Output logic
  printf("%c + %d = %d\n", Num1,Num2,Num1+Num2);
}
3 Likes

I barely understand this part.
Why? Because nowhere it is mentioned that ASCII takes part here, that would help to know about it, because on the second exercise the console outputs 7 but with what we’re given with we can’t understand if we don’t dig deep until finding in forums.

1 Like