Learn point C language (16): Data type

Source: Internet
Author: User
Tags include printf

曾经对float num=3.14f; 这样的赋值非常疑惑,其实现在也不明白.
既然说明了是float类型,又何必在3.14后面挂个f呢?

书上说:
int num=100; 一个整数常量将默认为int类型(除非常数有后缀或超出了int的范围)
double num=3.14; 一个浮点数常量将默认为double类型

并要求:
long num=100L;
long long num=100LL;
unsigned long=100UL;
unsigned long long num=100ULL;

float num=3.14f;

这些后缀是大小写无关的、(U和L是)无顺序的; 我反复测试,看不出没有区别.

我觉得这些东西用于 #define 还说得过去,用于指定类型的变量有必要吗?
我猜唯一的必要性或许是让编译器少转换一次.

不过知道还是有好处,以免看不懂别人的代码.

总之,前人应该自有道理,希望谁知道能告诉我.

另外,如果是十六进制或八进制常数,将从int、unsigned、long、unsigned long中自动选择最小的类型.

Other cases:

#include <stdio.h>
#include <stddef.h>

int main(void)
{
  char c='a';
  char cs[] = "abc";
  wchar_t wc = L'A'; /* 这个 L 也可以省略 */
  wchar_t ws[] = L"ABC"; 
  int n1 = 65535;
  int n2 = 0xFFFF; /* 十六进制  */
  int n3 = 0Xffff; /* 大小写都行 */
  int n4 = 0177777; /* 八进制   */

  printf("%c,%s,%c,%S\n",c,cs,wc,ws);
  printf("%d,%d,%d,%d\n",n1,n2,n3,n4);

  getchar();
  return 0;
}

Back to "Learn point C-Directory"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.