-3

I came across the following code in "C++ from Beginner to Master" by "明日科技":

#include <iostream>
#include <cstdio>
using namespace std;
int main() 
{
    int B_number=66;
    char B_as_cha=static_cast<char>(B_number);
    printf("%c",B_number);
}

Why do I need to include both <iostream> and <cstdio>? What is the purpose of using namespace std;? I noticed the variable B_as_cha is defined but not used in the printf function. Is there a reason for this?

HolyBlackCat
  • 78,603
  • 9
  • 131
  • 207
daishuge
  • 1
  • 3
  • 1
    Don't learn C++. It's a large and quite complicated. Learn just enough of C++ to do what you need to do (build something useful). There are many sites with example problems if you need inspiration. That said your question is not really suitable (way too broad) and coaching is not really what SO is about. – Allan Wind Aug 26 '23 at 02:58
  • 4
    Always practice after reading each chapter. Go back to the chapter you didn't understand. Or maybe the book itself is bad, here are the good ones: https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list – HolyBlackCat Aug 26 '23 at 04:43
  • 1
    Don't make drastic changes to a question after you ask it. Post a separate question instead. – HolyBlackCat Aug 26 '23 at 09:56
  • 1. This doesn't need ``. 2. *"`B_as_cha` is defined but not used"* Yes, looks like a typo to me. They probably meant to print it. 3. *"`using namespace std;`"* This one is easy to google. Overall, if your book has typos such as (1) and (2), maybe it's not a good book. – HolyBlackCat Aug 26 '23 at 09:57
  • Sometimes books are bad. Regardless, the **only** person who can tell you the reasoning or thought process behind the code in a book is **the author of that code**. (Also, based on the machine translation result, I'm going to guess that 明日科技 is a pseudonym or the name of a publisher, not an actual person.) – Karl Knechtel Aug 26 '23 at 09:58

0 Answers0