-
Integer data type allows a variable to store numeric values.
-
“int” keyword is used to refer integer data type.
-
The storage size of int data type is 2 or 4 or 8 byte.
-
It varies depend upon the processor in the CPU that we use. If we are using 16 bit processor, 2 byte (16 bit) of memory will be allocated for int data type.
-
Like wise, 4 byte (32 bit) of memory for 32 bit processor and 8 byte (64 bit) of memory for 64 bit processor is allocated for int datatype.
-
int (2 byte) can store values from -32,768 to +32,767
-
int (4 byte) can store values from -2,147,483,648 to +2,147,483,647.
-
If you want to use the integer value that crosses the above limit, you can go for “long int” and “long long int” for which the limits are very high.
Note: -
We can’t store decimal values using int data type.
-
If we use int data type to store decimal values, decimal values will be truncated and we will get only whole number.
-
In this case, float data type can be used to store decimal values in a variable.
1)编写示例程序,判断short、unsigned short、int、unsigned int、long、unsigned long占用内存的字节数。
2)选择题:请问int的取值范围是多少?
(A)二十多亿 (B) -2147483648~2147483647 © 0~4294967295
3)选择题:请问long的取值范围是多少?
(A)很多个亿 (B) 足够大 © -9223372036854775808~9223372036854775807
4)编写示例程序,从界面上输入数字的字符串,存放在字符串变量中,然后用atoi函数转换为整数,加上100后再输出到屏幕。
5)在C语言中,还有一种long long int的整数,各位写一个程序,测试它占用内存的字节数和取值范围,并思考long long int类型是否具备实用价值。
6)编写示例程序,测试short、unsigned short、int、unsigned、long、unsigned long赋值超出了取值范围的后果。
7)重写整数的abs和labs库函数,实现其功能,函数的声明如下:
int ABS(const int j); // 求int整数的绝对值
long LABS(const long int j); // 求long整数的绝对值
8)利用已经学习的知识,自定义一个函数,函数名是ctoi,把字符的’0’、‘1’、‘2’、‘3’、‘4’、‘5’、‘6’、‘7’、‘8’、'9’转换为整数的0、1、2、3、4、5、6、7、8、9。函数的声明如下:
// 用于把数字的字符转换为整数,chr为用字符方式表示的数字,函数的返回值为数字的整数。
// 如果chr不是数字的字符,函数返回-1。
int ctoi(const char chr);
提示:采用if或switch语句,判断chr的值,直接返回结果。1)编写示例程序,判断short、unsigned short、int、unsigned int、long、unsigned long占用内存的字节数。
2)选择题:请问int的取值范围是多少?
(A)二十多亿 (B) -2147483648~2147483647 © 0~4294967295
3)选择题:请问long的取值范围是多少?
(A)很多个亿 (B) 足够大 © -9223372036854775808~9223372036854775807
4)编写示例程序,从界面上输入数字的字符串,存放在字符串变量中,然后用atoi函数转换为整数,加上100后再输出到屏幕。
5)在C语言中,还有一种long long int的整数,各位写一个程序,测试它占用内存的字节数和取值范围,并思考long long int类型是否具备实用价值。
6)编写示例程序,测试short、unsigned short、int、unsigned、long、unsigned long赋值超出了取值范围的后果。
7)重写整数的abs和labs库函数,实现其功能,函数的声明如下:
int ABS(const int j); // 求int整数的绝对值 long LABS(const long int j); // 求long整数的绝对值
8)利用已经学习的知识,自定义一个函数,函数名是ctoi,把字符的’0’、‘1’、‘2’、‘3’、‘4’、‘5’、‘6’、‘7’、‘8’、'9’转换为整数的0、1、2、3、4、5、6、7、8、9。函数的声明如下:
// 用于把数字的字符转换为整数,chr为用字符方式表示的数字,函数的返回值为数字的整数。 // 如果chr不是数字的字符,函数返回-1。 int ctoi(const char chr);
提示:采用if或switch语句,判断chr的值,直接返回结果。
9)自定义一个函数,函数名是POW,利用已经学习的知识,求一个数的n次幂,函数的声明如下:
// 求x的y次幂,函数返回值为x的y次幂。
unsigned long POW(const unsigned int x,unsigned const int y);
调用示例:
printf("POW(2,3) is %lun",POW(2,3)); // 输出结果是8
printf("POW(10,3) is %lun",POW(10,5)); // 输出结果是100000
答案
1)sizeofint.c
#include#include #include int main() { printf("short in memory uses:%ld bytes.n", sizeof(short)); printf("unsigned short in memory uses:%ld bytes.n", sizeof(unsigned short)); printf("n"); printf("int in memory uses:%ld bytes.n", sizeof(int)); printf("unsigned int in memory uses:%ld bytes.n", sizeof(unsigned int)); printf("n"); printf("long in memory uses:%ld bytes.n", sizeof(long)); printf("unsigned long in memory uses:%ld bytes.n", sizeof(unsigned long)); printf("n"); printf("long long int in memory uses:%ld bytes.n", sizeof(long long int)); printf("unsigned long long int in memory uses:%ld bytes.n", sizeof(unsigned long long int)); double baselen = pow(2.0, 8.0); double shortlen = sizeof(short); printf("max short is:%dn",(int)(pow(baselen,shortlen) - 1)/2); double intlen = sizeof(int); printf("max int is:%ldn",(long)(pow(baselen,intlen) - 1)/2); double longlen = sizeof(long); printf("max long is:%ldn", (long)(pow(baselen, longlen) - 1)/2); }
2)3)5)
见1)代码中的验证
4)
8)
#include// 用于把数字的字符转换为整数,chr为用字符方式表示的数字,函数的返回值为数字的整数。 // 如果chr不是数字的字符,函数返回-1。 int ctoi(const char chr); int main() { char cc = 0 ; printf("plz input a char:"); scanf("%c", &cc); printf("you have inputed %dn", cc); int ii = ctoi(cc); printf("your char now is a number: %dn", ii); } int ctoi(const char chr) { switch(chr){ case '0': return 0; case '1': return 1; case '2': return 2; case '3': return 3; case '4': return 4; case '5': return 5; case '6': return 6; case '7': return 7; case '8': return 8; case '9': return 9; default: return -1; } }
.h
// 求x的y次幂,函数返回值为x的y次幂 unsigned long POW(const unsigned int x, unsigned const int y);
unsigned long POW(const unsigned int x, unsigned const int y)
{
int ii = 0;
unsigned long res = 1;
for (ii = 0; ii < y; ii++)
{
res *= x;
}
return res;
}
#include#include "_public.c" int main() { unsigned int x = 0; unsigned int y = 0; printf("plz intpu x:"); scanf("%u", &x); printf("plz input y:"); scanf("%u", &y); unsigned long res = POW(x, y); printf("the result is:%lun", res); }



