What is the c++ code of this c language code
printf("%d", b[i])
??
cout << b[i] << endl;
printf("%d", b[i]);
C++ is a super-set of C. Which makes the code valid C++ as well.
If you want to use a strictly C++ construct, you'll have to use the <iostream> header instead of the <stdio.h> header (<cstdio> in c++) and use the cout object:
std::cout << b[i] << std::endl;
std::cout<<int(b[i]);
It means output b as an integer. If b could not converse to int, it causes a compile-error.
In C language, if b is not a integer type, the output dose not make sense
std::cout<<b[i];
It outputs the b by its own type.
char c = 'A';
printf("%d", c); //Not A, treat c as an integer
cout<<int(c); //Not A, treat c as an integer
cout<<c; //It is A
thanks.. how about
scanf("%d", &n)
??
It would be the same type of situation, but you would use the cin object instead:
std::cin >> n;
Also, notice the use of the '>>' operator instead of the '<<' operator.
You have to be cautious about this situation though. If 'n' is a numeric type (like in your C code) and the user inputs char type data (i.e. "hello", or 'a') you will wind up with a corrupted input stream. If that happens, read this.
There are other input operators/functions available which help avoid the situation (such as get()), but this is probably the most-direct translation of the posted code.
thanks again! God bless!
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.