i want to make a code that it will tell me the size of a char,long,long,long.
but it always tell me its 4byte.
is it true?!
1 2 3 4 5 6 7 8 9 10 11 12 13
#include <iostream>
void main(){
usingnamespace std;
char a;
long b, c, d;
cout << "enter what you want to know the size" << endl;
cin >> a,b,c,d;
cout << "the size of what you entered is " << sizeof(a+b+c+d) << " byte" << endl << "my c++ codes are awful D: !!" << endl;
}
and also how can i make a code that it wil give me a mix of number and character?
#include <iostream>
int main()
{
std::cout << "Computing the size of some C++ built-in variable types\n\n";
std::cout << "Size of bool: " << sizeof(bool) << '\n';
std::cout << "Size of char: " << sizeof(char) << '\n';
std::cout << "Size of unsigned short int: " << sizeof(unsignedshort) << '\n';
std::cout << "Size of short: " << sizeof(short) << '\n';
std::cout << "Size of unsigned long int: " << sizeof(unsignedlong) << '\n';
std::cout << "Size of long: " << sizeof(long) << '\n';
std::cout << "Size of int: " << sizeof(int) << '\n';
std::cout << "Size of unsigned int: " << sizeof(unsignedint) << '\n';
std::cout << "Size of float: " << sizeof(float) << '\n';
std::cout << "Size of double: " << sizeof(double) << "\n\n";
std::cout << "The output can change with compiler, processor type and OS\n\n";
std::cout << "C++11 added four new standard variable types.\n";
std::cout << "The values can change whether compiled as 32 or 64 bit.\n\n";
std::cout << "Size of unsigned long long: " << sizeof(unsignedlonglong) << '\n';
std::cout << "Size of long long: " << sizeof(longlong) << '\n';
std::cout << "Size of long double: " << sizeof(longdouble) << '\n';
std::cout << "Size of nullptr: " << sizeof(nullptr) << '\n';
}
32-bit output:
Computing the size of some C++ built-in variable types
Size of bool: 1
Size of char: 1
Size of unsigned short int: 2
Size of short: 2
Size of unsigned long int: 4
Size of long: 4
Size of int: 4
Size of unsigned int: 4
Size of float: 4
Size of double: 8
The output can change with compiler, processor type and OS
C++11 added four new standard variable types.
The values can change whether compiled as 32 or 64 bit.
Size of unsigned long long: 8
Size of long long: 8
Size of long double: 8
Size of nullptr: 4
thanks but it doesnt matter for me that what is the size of these things.
only i wanted to learn coding and undrestand my mistakes.
but also thanks for this code.
i will practice this one too.
Line 8 cin >> a,b,c,d; is not doing what you think. It only actually reads a value into a and leaves b, c and d unchanged. If you want to read values into all of the variables you need to use the >> operator between all of the variables instead of the comma operator. cin >> a >> b >> c >> d;
And if you want to print the sum of these variables you should not use sizeof.
When using char with the cin >> operator it reads a character. So if you enter "2" as input for a it will store the ASCII value for '2', which is 50, in a so when you use a to calculate the sum the result will be 48 more than you expected. Another problem is that if you input more than one digit it will still only read the first one so the rest of that number will end up being read as the value for b.
Example:
Input buffer: "272 10 5 94"
cin >> a
a gets the value 50 which is the ASCII code for '2'.
Input buffer: "72 10 5 94"
cin >> b
b gets the value 72 because that's what's found next in the input stream.
Input buffer: " 10 5 94"
cin >> c
c gets the value 10.
Input buffer: " 5 94"
cin >> d
d gets the value 5.
Input buffer: " 94"
Left over for other read operations to read, if there are any.
If you want to read numbers you should not use char.
char is more suitable for situations when you want to read individual characters, such as letters or digits.
#include <iostream>
usingnamespace std;
int main(){
char a;
long b, c, d;
cout << "Enter what you want to know the size of " << endl;
cin >> a >> b >> c >> d;
cout << "The size of what you entered is " << sizeof(a) + sizeof(b) + sizeof(c) + sizeof(d) << " bytes" << endl << "My c++ codes are awful D: !!" << endl;
return 0;
}
//COMPILE USING TURBO CPP COMPILER
#include<iostream.h>
#include<conio.h>
void main(){
char a;
long b,c,d;
cout << "Enter what you want to know the size of " << endl;
cin >> a >> b >> c >> d;
cout << "The size of what you entered is " << sizeof(a) + sizeof(b) + sizeof(c) + sizeof(d) << " bytes" << endl << "My c++ codes are awful D: !!" << endl;
}