Thứ Bảy, Tháng Ba 8, 2025
spot_img
HomeStudyData types in C programming

Data types in C programming

If you don't know Which of the following data types is considered a basic data type in the C programming language?the article will give you the answer.

C programming language Used quite a lot in application and web development. Understanding the components in the C programming language will help you use them properly. In this article, let's learn about data types in C language.

In the C programming language, data types refer to system extensions used for declaring variables of different types. The type of a variable determines how much memory is used to store that variable and how bits are stored when interpreted.

Variable types in C are divided as follows:

No Type and description
1 Basic style

These are arithmetic data types and include two main types: a) integer types and b) floating point real number types.

2 Listing type

These are arithmetic types and are used to define variables that can be assigned a certain number of integer values ​​throughout the program.

3 Type void

Identifier type void. void is a special type that represents no value.

4 Type of development from the basics

Including types: a) pointer, b) array type, c) structure type, d) union type and e) function type.

Array and structure data types are used in collections as aggregate data types. Types are functions that specify the type of type the function returns. We will look at the basic data types below, with the remaining types being mentioned in later chapters.

Integer type (int) in C

The following table gives you detailed information about the integer type and its storage size and limits:

Xem thêm  Learn about the most popular RDBMSs
Type Storage size Value range
char 1 byte -128 to 127 or 0 to 255
unsigned char 1 byte 0 to 255
signed char 1 byte -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. long 4 bytes -2,147,483,648 to 2,147,483,647
unsigned long 4 bytes 0 to 4,294,967,295

You can get the exact size of the types of variables on specific platforms, you can use the operator sizeof. Expression sizeof(size) Returns the size of the object or type in bytes. Below is an example to get the size of an int object on any computer.

#include 
#include 

int main()
{
   printf("Kich co luu tru cho so nguyen (int) la: %d \n", sizeof(int));
   
   return 0;
}

Compiling and running the above C program will produce the following results:

Kich co luu tru cho so nguyen (int) la: 4

Floating-Point type in C

The following table gives you a detailed understanding of the standard floating point types with their storage sizes and ranges of values ​​and precision:

Type Storage size Value range Accuracy
float. float 4 bytes 1.2E-38 to 3.4E+38 6 decimal places
double 8 bytes 2.3E-308 to 1.7E+308 15 decimal places
long double 10 bytes 3.4E-4932 to 1.1E+4932 19 decimal places

float.h The Header file defines macros that allow you to use these and other specific types of binary representation of real numbers in your program. Below is an example that prints the size of a float as well as its range:

#include 
#include

int main()
{
printf("Lớp lưu trữ cho số thực (float) là: %d \n", sizeof(float));
printf("Giá trị số thực dương nhỏ nhất là: %E\n", FLT_MIN );
printf("Giá trị số thực dương lớn nhất là: %E\n", FLT_MAX );
printf("Độ chính xác: %d\n", FLT_DIG );

return 0;
}

Compiling and running the above C program will produce the following results:

Lớp lưu trữ cho số thực (float) là: 4 
Giá trị số thực dương nhỏ nhất là: 1.175494E-38
Giá trị số thực dương lớn nhất là: 3.402823E+38
Độ chính xác: 6

Type void in C

The void type defines no value. It is used in the following 3 cases:

Xem thêm  bool() function in Python

Function returns void: There are many functions in C language that return no data and you can say that it is a void function. A function that returns no value has type void. For example: void exit (int status);

Function with void parameter: There are functions in C that do not accept any parameters. A function with no parameters can be accepted as a void. For example: int rand(void);

Pointer to void: A pointer of type void * represents the address of an object, not a type. Example memory allocation function void *malloc (size_t size); Returns a void pointer that can be cast to any object.

You may not understand these points about the void type, we should move on and in the next chapters we will reiterate these points.

Defines the basic format

The C programming language has different format definitions for each data type. Here are a few of them:

Specify format Data type Sample code
%d or %i int
#include 

int main() {
  int myNum = 5;  // integer
  
  printf("%d\n", myNum);
  printf("%i\n", myNum);
  return 0;
}
%f float. float
#include 

int main() {
  float myFloatNum = 5.99;  // Floating point number
  
  printf("%f", myFloatNum);
  return 0;
}
%lf double
#include 

int main() {
  double myDoubleNum = 19.99;  // Double (floating point number)
  
  printf("%lf", myDoubleNum);
  return 0;
}
%c char
#include 

int main() {
  char myLetter="D"; // Character
  
  printf("%c", myLetter);
  return 0;
}
%s Used for strings (text).
#include 

int main() {
  char greetings[] = "Hello World!";
  printf("%s", greetings);
 
  return 0;
}

Previous lesson: Basic syntax of C programming

Next lesson: Variables in C programming

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments