C Language Chapter II data types, variables, and input functions

Source: Internet
Author: User

Directory

    • Introduction of data type
    • Ii. Definition of variables
      • 2.1. Name rules for variable names
      • 2.2. Various forms of variable definition
    • Integral type (int)
    • Four, character type (char)
    • Single-precision floating-point type (float)
    • Six, double-precision floating-point type (double)
    • Seven, input scanf
      • 7.1. Specify delimiter when entering
      • 7.2. Clear the buffer
      • 7.3. Input string
      • 7.4. Limit the input range
      • 7.5, limit the input range to reverse
Introduction of data type

In C, a data type refers to a wide range of systems used to declare variables or functions of different types. The type of variable determines how much space the variable store occupies and how to interpret the stored bit pattern.

Type conversions:

type Storage size Value Range
Char 1 bytes 128 to 127 or 0 to 255
unsigned char 1 bytes 0 to 255
Signed Char 1 bytes 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
unsigned long 4 bytes 0 to 4,294,967,295

1.2. Get the length of the data type
#include "stdio.h" void Main () {    //sizeof is used to get the length of a number type    //sizeof is a keyword (32)    printf ("Char takes up bytes:%d \ n", sizeof (char));    printf ("Short takes up bytes:%d \ n", sizeof (short));    printf ("int occupies bytes:%d \ n", sizeof (int));    printf ("Long takes up bytes:%d \ n", sizeof (long));    printf ("Float takes up bytes:%d \ n", sizeof (float));    printf ("Double takes bytes:%d \ n", sizeof (double));     }

Ii. Definition of variables

Is the space that is opened up in memory with the data type.
A variable should have a name, which is the name that our programmer gives to the space called the variable name.
It occupies a certain storage unit in memory.
The variable definition must be placed before the variable is used, which is defined and used, and is generally placed at the beginning of the function body.

Variable: The amount of variation that can be varied during a program's run.

Constant: The amount of content that cannot be changed during a program's operation.

#include "stdio.h" void Main () {    int i=100;    i++;   I=i+1    printf ("i=%d \ n", i);//101    i--;  I=i-1    printf ("i=%d \ n", i);//100    i+=100;  i=i+100        printf ("i=%d \ n", i);    i/=3;  i=200/3=66.666667=66          printf ("i=%d \ n", i);    i+7;  66+7 completes the operation in memory, but the value is not written back to                printf ("i=%d \ n", i);  66}

2.1. Name rules for variable names

The first character can only be a letter or an underscore (_);

The first character can only be followed by letters, numbers, and underscores (_);

Keywords (those words that will change color in writing) cannot be used as variable names;

Case sensitive;

First (a-za-z_) follow-up (a-za-z_0-9)

Which of the following is illegal, please?

1name
_product
_9527
Double
Int
_product
_product
Total

2.2. Various forms of variable definition
/* Note:your choice is C IDE */#include "stdio.h" void Main () {    //define variable and assign value    int i=100;        Define the re-assignment    float J first;    j=99.5f; Error, the variable definition must be placed before the variable is used, which is defined after use, usually placed at the beginning of the function body        //At the same time define multiple variables    char a,b,c;        Define multiple variables at the same time and assign    double x=1.5,y=1.6,z;        j=99.5f;    z=x+y;    }
Integral type (int)
#include "stdio.h" void Main () {    int i;    i++;   I=i+1    printf ("i=%d \ n", i);//101    i--;  I=i-1    printf ("i=%d \ n", i);//100    i+=100;  i=i+100        printf ("i=%d \ n", i);    i/=3;  i=200/3=66.666667=66          printf ("i=%d \ n", i);    i+7;  66+7 completes the operation in memory, but the value is not written back to                printf ("i=%d \ n", i);  66}

Four, character (char) five, single-precision float type (float) six, double precision floating point type (double) seven, input scanf
#include "stdio.h" void Main () {    //judgment age if less than 18 years old hint young really good, otherwise output age        //define variable    int ages;        User input Age    printf ("Please enter Age:");    From the keyboard input,& to take the variable address    scanf ("%d", &age);        Condition judgment if    (age<18) {  //If the condition is set up       printf ("nice Young");    }    else{  //If the condition is not set//       output       printf ("Your Age is:%d", "ages");    }}

scanf ("Format control", address list);
The meaning of "format control" is the same as printf function;
An "address List" is a table column consisting of several addresses, either the address of a variable or the first address of a string

%f Single Precision

%LF Double Precision

%c receives one character from the keyboard
%d receive decimal integers from the keyboard
%s receives a string from the keyboard, indicating that a space, tab, or newline character is received.
%[] receives a character enclosed in parentheses from the keyboard, so long as it encounters a character that is not inside.

Enter if you encounter a space Carriage return tab indicates the end

7.1. Specify delimiter when entering
#include "stdio.h" void Main () {    int a,b,c;    printf ("Please enter year-month-day:");    scanf ("%d-%d-%d", &a,&b,&c);    printf ("%d%d months%d days", a,b,c);}

7.2. Clear the buffer
#include "stdio.h" void Main () {    char x, y, Z;    printf ("Input:");    scanf ("%c", &x);    printf ("Input:");    scanf ("%c", &y);    printf ("Input:");    scanf ("%c", &z);    printf ("%c,%c,%c", X, Y, z);}

#include "stdio.h" void Main () {    char x, y, Z;    printf ("Input:");    scanf ("%c", &x);    Fflush (stdin);  Clear buffer    printf ("Input:");    scanf ("%c", &y);    Fflush (stdin);  Clear buffer    printf ("Input:");    scanf ("%c", &z);    Fflush (stdin);  Clear buffer    printf ("%c,%c,%c", X, Y, z);}

7.3. Input string
#include "stdio.h" void Main () {    char a[100];  Array, char can only be a single character    scanf ("%s", a);  Do not fetch    the address printf ("%s", a);}

7.4. Limit the input range
#include "stdio.h" void Main () {    //Limit input range    char a[100];    scanf ("%[0123456789]s", a);//can only enter 0-9, other means end    printf ("%s", a);}

7.5, limit the input range to reverse
#include "stdio.h" void Main () {    char a[100];    scanf ("%[^a]s", a);  ^ Reverse, if it is a then the end    //scanf ("%[^\n]s", a);  If this is the end of a newline, other characters can be    printf ("%s", a);}

Practice:

#include "stdio.h" void Main () {    int nianling;  Age    float Fenshu;  Fractional    Char aihao[30];  Hobby    printf ("Please enter Age:");    scanf ("%d", &nianling);        printf ("Please enter a score:");    scanf ("%f", &fenshu);        printf ("Please enter hobby:");    scanf ("%s", Aihao);        printf ("Your age is%d, score:%f, Hobby:%s", Nianling,fenshu,aihao);}

C Language Chapter II data types, variables, and input functions

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.