Basic Types and Conversion Operations
Data Types
The basic types in C language are as follows.
| Type | Storage Size | Value Range |
| :----------------- | :----------- | :-------------------------------------------------- |
| char | 1 byte | -128 to 127 or 0 to 255 |
| unsigned char | 1 byte | 0 to 255 |
| signed char | 1 byte | -128 to 127 |
| int | 2 or 4 bytes | -32,768 to 32,767 or -2,147,483,648 to 2,147,483,647 |
| unsigned int | 2 or 4 bytes | 0 to 65,535 or 0 to 4,294,967,295 |
| short | 2 bytes | -32,768 to 32,767 |
| unsigned short | 2 bytes | 0 to 65,535 |
| long | 4 bytes | -2,147,483,648 to 2,147,483,647 |
| long long long int | 8 bytes | -9223372036854775808 to +9223372036854775807 |
| unsigned long | 4 bytes | 0 to 4,294,967,295 |
| float | 4 bytes | 1.2E-38 to 3.4E+38 |
| double | 8 bytes | 2.3E-308 to 1.7E+308 |
| long double | 16 bytes | 3.4E-4932 to 1.1E+4932 |
The basic data types in C# and Java are as follows.
| C# Type | Java Type | Description | Default Value |
| :------- | ------------------ | :------------------------------------- | :------------ |
| bool | boolean | Boolean value | False |
| byte | byte | 8-bit unsigned integer | 0 |
| char | char | 16-bit Unicode character | '\0' |
| decimal | BigDecimal (not basic) | 128-bit precise decimal value, 28-29 significant digits | 0.0M |
| double | double | 64-bit double precision float | 0.0D |
| float | float | 32-bit single precision float | 0.0F |
| int | int | 32-bit signed integer type | 0 |
| long | long | 64-bit signed integer type | 0L |
| sbyte | None | 8-bit signed integer type | 0 |
| short | short | 16-bit signed integer type | 0 |
| uint | None | 32-bit unsigned integer type | 0 |
| ulong | None | 64-bit unsigned integer type | 0 |
| ushort | None | 16-bit unsigned integer type | 0 |
In C language, char
is one byte and uses ASCII encoding, while in C# and Java, the character type (char) is two bytes using Unicode encoding.
In both C# and Java, whether on a 32-bit machine or a 64-bit machine, int
is 4 bytes and long
is 8 bytes.
Examples of Data Type Usage
C/C++
An example of using basic types in C language is as follows.
short a = 1; // short int a = 1;
int b = 1;
long c = 1L; // long type, the number must have L
long long d = 1LL; // two ls
char e = '1';
float f = 1.0;
double g = 1.0F; // use F
The default integer type in C language is int
, and the default floating-point type is float
.
Therefore, when assigning constants, if it is not the default type, you need to add L or F at the end of the number.
No need to add for short
; for double
, add F.
In C language, the representation methods for different bases are:
- Default is base 10, e.g., 10, 20, can be assigned directly.
- Base 8: prefix with 0, like 012, 013.
- Binary: prefix with 0b, like 0b11.
- Base 16: prefix with 0x, case insensitive, like 0x21458adf.
Note that only integers can have these base representations; floating-point types cannot.
C# and JAVA
Using the same sample in C# and JAVA as follows:
short a = 1;
int b = 6_666_666;
long c = 1L; // long type, the number must have L
char e = '1';
float f = 1.0F;
double g = 1.0;
byte h = 1;
In C# and JAVA, the default integer type is int
, and the floating-point type is double
.
Thus, float
must have F.
The base representation in C# and JAVA is the same as in C language.
Additionally, C# and JAVA allow the use of underscores to separate numbers for improved readability. For example:
int b = 6_666_666;
int b = 0b00000000_00000000_00000000_00000001;
About Boolean Types
C language does not have a boolean type.
C++ uses bool
, C# uses bool
, and Java uses boolean
.
In C language, there is no bool
type, but many places require true
and false
. How to solve it? Generally, in C language, 1
and 0
, or non-zero and 0
are used to represent true
and false
.
In some places, \0
also represents false
.
For example:
int a = 6666;
int b = 161616;
printf("%s", a & b ? "true" : "false");
The result of a & b
is a number; as long as it's greater than 0 or less than 0, it is true
.
Also, in C# / Java, the ?:
operator's left side condition must be bool
, not a number.
Operations with Basic Types
Where Do Operation Methods Come From
High-level languages provide a wealth of excellent library code, and by calling this code, we can achieve many sophisticated operations and reduce our workload.
First, let's look at a part of the code in C# and Java.
// C#
int a = new int();
a = 666;
Int32 b = new Int32();
b = new int();
b = 666;
// JAVA
int a = 666;
Integer b = new Integer(666);
In C#, each predefined type (basic data type) is an abbreviation for a system-provided type.
For example, the keyword int
refers to the struct System.Int32
.
That is to say, in C#, int
and System.Int32
are equivalent.
In JAVA, int
is a basic data type, while Integer
is a type that wraps int
and provides a rich set of operations.
Thus, using int a = new int()
is incorrect in JAVA.
The int
type in C# has some methods, but not many.
The Integer
class in JAVA provides some rich operation methods.
For instance, converting strings to numbers; C# primarily relies on Convert
for conversion, while JAVA can operate through Integer
.
In C language, methods for basic types come from library functions.
String to Numeric Conversion
C
It is important to note that there is no string type (string) in C language.
In C language, the stdlib.h
header file defines several basic types and some functions.
We can convert a string to an int
type like this:
char a[] = "123";
int b = atoi(a);
Here are some commonly used conversion functions I copied from the internet:
● atof()
: Converts a string to a double precision floating-point value.
● atoi()
: Converts a string to an integer value.
● atol()
: Converts a string to a long integer value.
● strtod()
: Converts a string to a double precision floating-point value and reports all remaining numbers that cannot be converted.
● strtol()
: Converts a string to a long integer and reports all remaining numbers that cannot be converted.
● strtoul()
: Converts a string to an unsigned long integer and reports all remaining numbers that cannot be converted.
● itoa()
: Converts an integer to a string.
● ltoa()
: Converts a long integer to a string.
● ultoa()
: Converts an unsigned long integer to a string.
● gcvt()
: Converts a floating-point number to a string, rounded.
● ecvt()
: Converts a double precision floating-point value to a string; the result does not include a decimal point.
● fcvt()
: Specifies the number of digits for conversion precision, the rest is the same as ecvt()
.
C++
In C++, after including #include <iostream>
, we can directly use the above C functions.
C++ can use #include <string>
for string types; the string type is defined in the string
library.
#include <sstream>
can also facilitate the conversion between strings and numeric values. For example:
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
int main()
{
// Convert character to number
string str1 = "2018219";
string str2 = "2018.219"; // The effective number after conversion to float is 6 digits
int num1 = 0;
double num2 = 0.0;
stringstream s;
// Convert to int type
s << str1;
s >> num1;
// Convert to double type
s.clear();
s << str2;
s >> num2;
cout << num1 << "\n" << num2 << endl;
return 0;
}
// Example source: https://www.jianshu.com/p/5c6a6fdb1a41
Additionally, we can use sprintf()
and sscanf()
functions to achieve conversions between strings and numeric values.
C#
Converting a string to a numeric value in C# is very simple:
string a = "123";
int b = int.Parse(a); // int also contains some common methods
int c = Convert.ToInt32(a); // The static class Convert has many conversion methods and overloads
Convert provides various methods for converting between basic types.
JAVA
In JAVA, we can convert like this:
String a = "123";
int b = Integer.valueOf(a); // Through static method
Integer c = new Integer(a); // Converts Integer class to int
b = c.intValue();
It can be seen that, in JAVA, the basic data types do not have many methods, just simply represent values.
In C#, apart from the methods provided by the type itself, rich functionality is achieved through Convert
for type conversions.
In JAVA, we need to use corresponding data type objects to operate, for example, int
is Integer
, double
is Double
.
String a = "123.123";
double b = Double.valueOf(a);
Default Values and Initialization
C# and JAVA
In C# and JAVA, any local variable that is declared but not initialized cannot be used directly.
In some cases, if not initialized, the system automatically sets default values.
For example, in C# and JAVA:
static void Main(string[] args)
{
Test(b);
Console.ReadKey();
}
public static int b;
public static void Test(int a)
{
Console.WriteLine(a);
Console.WriteLine(b);
}
public static void main(String[] args) {
Test(b);
}
public static int b;
public static void Test(int a) {
System.out.println(a);
System.out.println(b);
}
The output result is both 0
.
In C#, these cases will automatically initialize to default values:
- Static variables.
- Instance variables of class instances.
- Array elements.
The default value situation in JAVA is the same as in C#.
Additionally, C# has a default
keyword that can automatically assign default values:
int a = default(int);
// or
int a = default;
Using default for values that don't need initial value or in situations where the compiler doesn't allow it can be particularly convenient.
C Language
Let's write an example to run:
#include <stdio.h>
int a;
char b[5];
int main()
{
printf("%d", a);
printf("\n\n");
printf("%s", b);
printf("\n\n");
return 0;
}
If nothing goes wrong, a
will be 0
, and b
will be blank.
Let's revise the code and run it again:
#include <stdio.h>
int main()
{
int a;
printf("%d", a);
printf("\n\n");
char b[5];
printf("%s", b);
return 0;
}
The above code can compile in c-free and similar compilers but may not in Visual Studio (due to variable a
).
If forced to pass, the runtime could produce:
3567616
亐wP@
Modify it to look like this, and running in Visual Studio will produce 烫烫烫烫烫烫烫烫烫烫烫烫烫烫
...
#include <stdio.h>
int main(){
char b[5];
printf("%s", b);
return 0;
}
The examples above illustrate that C language also has default values for static variables.
When declaring a character array, the system allocates memory and sets every byte to a value.
However, the char
type has a maximum range of only 256.
文章评论