Data type:
1. Basic data types
Integral type: basic integer int short integer Long
Character Char
Float type (real type): Single type float double
2. Construction type
Array type
struct type struct
Common body Type Union
Enum type enum
3. Pointer type
4. Empty type void
Integer variable:
[Signed] int-32 768~32 767 (16-bit system);
-2 147 483 648~2 147 483 647 (32-bit system);
unsigned [int] 0~65 535 (16-bit system)
0~4 294 967 295 (32-bit system)
[Signed] short [int]
unsigned short [int]
[Signed] long [INT]
unsigned short [int]
L/s/u/lu/su
Floating point type:
Single Precision type float
Double Type Doubles
Long double precision type long double-1.2e-4932~1.2e4932
Character type:
Character constants:
#include <stdio.h>intMain () {Putchar ('H'); Putchar ('e'); Putchar ('L'); Putchar ('L'); Putchar ('o'); Putchar ('\ n'); System ("Pause"); return 0; }
String constants:
#include <stdio.h>
int main ()
{
printf ("What a nice day!\n");
System ("pause");
return 0;
}
/* The system will automatically add "\" at the end of the string as the end of the string */
Character variables:
#include <stdio.h>
int main ()
{
Char C1 = C2 = 98;
printf ("%c,%c\n", C1, C2); Output C1 as a character, C2, output is a, b
printf ("%d,%d\n", C1, C2); Output C1, ASCII value of C2 output result is 97, 98
System ("pause");
return 0;
}
#include <stdio.h>
int main ()
{
Char cChar1 = ' a ', cChar2 = 97; /* character variable cChar1, cchar2*/
int iInt1 = ' a ', IInt2 = 97; /* Integer variable iInt1, iint2*/
printf ("%c\n", cChar1); /* Output result is a*/
printf ("%d\n", cChar2); /* Output result is 97*/
printf ("%c\n", iInt1); /* Output result is a*/
printf ("%d\n", IInt2); /* Output result is 97*/
System ("pause");
return 0;
}
Escape characters:
Character constants can be divided into two types:
1. General character constants
2. Special character constants (escape characters)
#include <stdio.h>
int main ()
{
Putchar (' H ');
Putchar (' \ n ');
Putchar (' e ');
Putchar (' \ n ');
Putchar (' l ');
Putchar (' \ n ');
Putchar (' l ');
Putchar (' \ n ');
Putchar (' O ');
Putchar (' \ n ');
System ("pause");
return 0;
}
Symbolic constants:
#define Symbol constant name constant value
#include <stdio.h>
#define PI 3.14/* Define Symbolic constants */
int main ()
{
Double Fradius; /* Define RADIUS variable */
Double fresult=0; /* Define result variable */
printf ("Please enter the radius of the Circle:");/* Prompt */
scanf ("%lf", &fradius); /* Input data */
Fresult=fradius*fradius*pi; /* For calculation */
printf ("Area of the Circle:%lf\n", fresult);/* Show Results */
return 0; /* End of program */
}
Type conversions:
Different types can be converted to each other.
Char, short---->int---->unsigned---->long---->double
FLOAT---->double
#include <stdio.h>
int main ()
{
int iint = 1;
Char CChar = ' A ';
float ffloat = 2.2f;
Double Reselt = iint + CChar + ffloat;
printf ("%f\n", Reselt);
System ("pause");
return 0;
}
Data is lost in assignment operations, data is rounded and output (Java directly discarded)
#include <stdio.h>
int main ()
{
float a = 3.1415926;
printf ("%f\n", a);
System ("pause");
}
/* Result output: 3.141593*/
========================================================================================
Input and output:
Character input/character output
#include <stdio.h>
int main ()
{
Char cchar1;/* declaring variable */
CChar1 = GetChar ();/* Get characters in input device */
Putchar (CCHAR1);/* Output character */
Putchar (' \ n ');/* Output escape character for line break */
GetChar ();/* Get newline character */
Putchar (GetChar ());/* Get input character direct output */
Putchar (' \ n ');/* Line break */
System ("pause");
return 0;
}
/* In the above program, there is a place where the GetChar () function is used to receive line breaks, and when the first character is entered,
To make sure that the input is complete, you will determine it by line break. Where newline characters are also counted, and if you do not get them, the next time you use
A newline character is obtained when the individual inserts () function. */
Format input
scanf () function when the C language provides the format input function. The general format of the scanf () function is:
SCANF (format control, address list);
The format character of the scanf () function and its function description:
Format character Function description
D, I enter a signed decimal integer
U unsigned decimal integer
O unsigned octal integers
x, x unsigned hexadecimal integer (case function is the same)
...
Format input:
#include <stdio.h>
int main ()
{
Long ilong=100000;/* defines a length variable, assigning it a value */
printf ("The Long is%ld\n", ilong);/* Output Long Integer variable */
printf ("The string is:%s\n", "Love");/* Output String */
printf ("The string is:%10s\n", "Love");/* Use the M control output column */
printf ("The string is:%-10s\n", "Love");/* Use-means to move to the left */
printf ("The string is:%10.3s\n", "Love");/* Use N to take the number of characters */
printf ("The string is:%-10.3s\n", "Love");
return 0;
}
/* Output Result:
The Long is 100000
The string is:love
The string is:love
The string is:love
The string Is:lov
The string Is:lov
*/
#include <stdio.h>
int main ()
{
Long ilong;/* type variable */
Short ishort;/* variable */
int inumber1=1;/* integer variable, assigning it a value of 1*/
int inumber2=2;/* integer variable, assigning it a value of 2*/
Char cchar[10];/* defines the character array variable */
printf ("Please enter a long integer variable value \ n");/* OUTPUT information hint */
scanf ("%ld", &ilong);/* Input Long Integer data */
printf ("Please enter a short integer value \ n");/* OUTPUT information hint */
scanf ("%hd", &ishort);/* Enter short integer data */
printf ("Please enter an integer: \ n");/* OUTPUT information hint */
scanf ("%d*%d", &inumber1,&inumber2);/* Input integer data */
printf ("Please enter a string, but only the first three characters \ n");/* OUTPUT information hint */
scanf ("%3s", CChar);/* Input String */
printf ("Number of long integers:%ld\n", Ilong); /* Show Long Integer value */
printf ("Number of short integers:%hd\n", ishort);/* Displays short integer values */
printf ("Integer value 1:%d\n", iNumber1);/* Displays the value of the integer INumber1 */
printf ("Integer value 2:%d\n", iNumber2);/* Displays the value of the integer INumber2 */
printf ("first three bits of output string:%s\n", CChar);/* Display String */
}
/* Output Result:
Please enter a long integer variable value
111111111111
Please enter a short integer value
11
Please enter an integer:
1
Please enter a string, but only the first three characters are displayed on the output
Acvv
The number of long integers is:-558038585
The number of short integers is: 11
Integer value 1 is: 1
Integer value 2 is: 2
Top three bits of the output string: ACV
*/
On-Machine Practice:
1. Let the user enter the length and width of the rectangle and then output the area to the computer screen.
#include <stdio.h>
int main ()
{
float Length,width;
printf ("Enter the length and width of the rectangle \ n");
scanf ("%f%f", &length,&width);
printf ("The area of the rectangle is:%10.3f\n", length*width);
System ("pause");
return 0;
}
/* Output Result:
Enter the length and width of the rectangle
20 1.1
The area of the rectangle is: 22.000
*/
2. Convert uppercase letters to lowercase
#include <stdio.h>
int main ()
{
Char ch;
printf ("Enter an uppercase letter: \ n");
Ch=getchar ();
ch=ch+32;
printf ("The lowercase form of the letter is:%c\n", ch);
return 0;
}
/* Output Result:
Enter an uppercase letter:
D
The lowercase form of the letter is: D
*/
Chapter three basic data types