Data types in the basic tutorial of C language

  • 2020-05-30 20:51:18
  • OfStack

The data types in the C language are integer, floating point (that is, decimal), character, string, array, struct, and so on. Don't learn too much at the beginning. Learn the basics of integers, floats, and characters first.

For learning programs, the most important thing is to get your hands dirty.

Write the program first:


#include <stdio.h>

int main()
{
 int a = 1;
 printf("a = %d\n", a);
 
 float b = 2.2;
 printf("b = %f\n", b);
 
 char c = 'A';
 printf("The char is %c\n", c);
 
 return 0;
}

The results


a = 1
b = 2.200000
The char is A

Program analysis:
(1)

int a = 1;

A variable a of type integer is defined here. Once you've defined it, assign 1 to a, so that a has a value of 1.
In C, the first occurrence of a variable must be defined, otherwise the compiler would not recognize the variable.

(2)
printf("a = %d\n", a);

As we said in the last video, the purpose of printf is to print content at the console. %d is a formatting symbol that indicates that the place is to be replaced with an integer. \n for newline. The rest is output as-is. Therefore, the printed content is:
a = 1

(3)

float b = 2.2;

float stands for floating point, which in a program is known as the decimal type.

(4)

printf("b = %f\n", b);

Here %f is the formatting symbol of floating point number, which needs to be replaced by 1 floating point number. The value of b after the comma is used to replace %f. So the output is
b = 2.200000
This shows six decimal places behind the decimal point, because floating point Numbers are six digits by default in a computer.

(5)
char c = 'A';
Here we define the 1 character type variable c, assigning the character A to c.
char is short for character. A is enclosed in single quotes and cannot be enclosed in double quotes. Because if you include it in double quotation marks, it represents a string. I'll save strings for later.

(6)
printf("The char is %c\n", c);
Here %c is the formatting symbol for the character, which needs to be replaced by a real character. The value of c after the comma is used to replace %c. The final output
The char is A


Related articles: