了解整数类型

有四种整数类型,具体取决于需要保存变量的数值长度。下面给出了32位和64位系统的典型范围。

char  myChar  = 0; /* -128   to +127 */
short myShort = 0; /* -32768 to +32767 */
int   myInt   = 0; /* -2^31  to +2^31-1 */
long  myLong  = 0; /* -2^31  to +2^31-1 */

C99增加了对long long数据类型的支持,保证长度至少为64位。

long long myLL = 0; /* -2^63 to +2^63-1 */

要确定数据类型的大小,请使用sizeof运算符。sizeof运算符返回类型占用的字节数。sizeof运算符返回的类型是size_tsize_t是整数类型的别名。
在C99中引入了修辞符%zu以使用printf格式化此类型。Visual Studio不支持此修辞符,可以使用%Iu

#include <stdio.h>

int main(void) {
  size_t s = sizeof(int);
  printf(%zu, s); /* 4 (C99) */
  printf(%Iu, s); /* 4 (Visual Studio) */
}

也可以使用八进制或十六进制表示法分配整数。以下值均表示相同的数字,十进制表示法为50

#include <stdio.h>

int main() {

  int myDec = 50;   /* decimal notation */
  int myOct = 062;  /* octal notation (0) */
  int myHex = 0x32; /* hexadecimal notation (0x) */

  printf(%d, myDec);
  printf(%d, myOct);
  printf(%d, myHex);
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


声明一个计数循环
咕咕咕
#include <stdio.h> int main (void) { printf ("tutorial from book 2s.c om.\\n"); printf ("from book2s.com.\\n");
#include <stdio.h> int main (void) { printf ("Testing...\\n..1\\n...2\\n....3\\n"); return 0;//fromwww.y iiba i .co m
#include <stdio.h> int main (void) { int sum;//fromw w w. yiib ai . com sum = 50 + 25; printf ("The sum of 50 and 25 is %i\\n", sum);
#include <stdio.h> int main (void) { int value1, value2, sum; value1 = 50;/*ww w.yIi ba i. com*/ value2 = 25;
#include <stdio.h> int main()/* from w ww.yi ba I.c om*/ { int planets = 8; int friends = 6; int potterBooks = 7;
要求用户输入浮点,整数并将其打印在屏幕上,以下是代码的实现: #include <stdio.h>
C语言在屏幕上打印一条消息,参考以下实现代码: #include <stdio.h> int main() { printf("this is a test");
#include <stdio.h> int main(void) { int total; int cats; int dogs; int ponies; int others; cats = 4; dogs = 3;
#include <stdio.h> int main(void) { int ch; int ct = 0; while ((ch = getchar()) != EOF) ct++; printf("%d characters read\\n", ct);
switch 不使用 break 语句: #include <stdio.h> int main() { int iResponse = 0; printf("\\n1\\tSports\\n");
#include <stdio.h> int main() { char cResponse = '\\0'; printf("\\na\\tTurn the AC on\\n");
#include <stdio.h> int main() { int iSelection = 0; float fTransAmount = 0.0; float fBalance = 100.25;
#include <stdio.h> int main(void) { const float INCHES_PER_FEET = 12; float height; char name[40]; printf("What is your name?: ");
用双引号将其打印将其打印在20个字符宽的字段中,整个字段用引号括起,名称在字段的右端。在20字符宽的字段的左端打印它,整个字段用引号括起来。将其打印在比名称宽三个字符的字段中。
#include <stdio.h> int main(void) {// printf("a a\\n"); printf("a \\n a \\n");
#include <stdio.h> int main(void) { int ageyears;/* age in years */ int agedays;/* age in days*/ //fromwww .yi ib ai .co m
#include <stdio.h> void functionA(void); void functionB(void); int main(void){ functionA();/*fromwww.yi i bai.com*/
#include <stdio.h> void my_function(void);/* C function prototyping */ int main(void){ printf("main.\\n");