Learn point C language (19): Data type

Source: Internet
Author: User
Tags printf

1. The logo of the array is []:

#include <stdio.h>

int main(void)
{
  int nums[3];

  nums[0] = 11;
  nums[1] = 22;
  nums[2] = 33;

  printf("%d,%d,%d",nums[0],nums[1],nums[2]);

  getchar();
  return 0;
}

2. Size and dimensions of the array:

#include <stdio.h>

int main(void)
{
  int nums[10];

  printf("数组大小是: %d\n",sizeof(nums));
  printf("数组维数是: %d\n",sizeof(nums)/sizeof(nums[0]));
  getchar();
  return 0;
}

3. Iterate through the array:

#include <stdio.h>

int main(void)
{
  int nums[10];

  int i;
  for (i = 0; i < sizeof(nums)/sizeof(nums[0]); i++) 
    nums[i] = i * i;

  for (i = 0; i < sizeof(nums)/sizeof(nums[0]); i++) 
    printf("%d\n",nums[i]);

  getchar();
  return 0;
}

For an array of strings, we can also think ...

#include <stdio.h>

int main(void)
{
  char cs[] = "ABCDEFG";
  int i;
  for (i = 0; cs[i]; i++) {  /* cs[i] 为假时,就到了那个空字符了 */
    printf("%c\n",cs[i]);
  }

  getchar();
  return 0;
}

Related Article

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.