banner



Which Data Types Are Used To Store Whole Numbers?

Data Types in C - Integer, Floating Point, and Void Explained

Data Types in C

In that location are several different ways to store information in C, and they are all unique from each other. The types of data that information can be stored as are called data types. C is much less forgiving about data types than other languages. As a result, information technology's important to make sure that y'all sympathise the existing information types, their abilities, and their limitations.

One quirk of C's data types is that they depend entirely on the hardware that you're running your lawmaking on. An int on your laptop will exist smaller than an int on a supercomputer, so knowing the limitations of the hardware you lot're working on is of import. This is also why the data types are divers as beingness minimums- an int value, as you volition larn, is at minimum -32767 to 32767: on sure machines, it volition exist able to store fifty-fifty more than values that this.

There are two categories that nosotros tin pause this into: integers, and floating point numbers. Integers are whole numbers. They can be positive, negative, or zero. Numbers similar -321, 497, 19345, and -976812 are all perfectly valid integers, but 4.5 is not because 4.5 is not a whole number.

Floating point numbers are numbers with a decimal. Similar integers, -321, 497, 19345, and -976812 are all valid, but now 4.5, 0.0004, -324.984, and other non-whole numbers are valid too.

C allows us to choose between several dissimilar options with our data types because they are all stored in different ways on the computer. Every bit a outcome, information technology is important to be enlightened of the abilities and limitations of each information type to choose the most appropriate one.

Integer information types

Characters: char

char holds characters- things like messages, punctuation, and spaces. In a estimator, characters are stored as numbers, so char holds integer values that stand for characters. The actual translation is described by the ASCII standard. Here's a handy table for looking up that.

The actual size, like all other data types in C, depends on the hardware you're working on. Past minimum, information technology is at least eight $.25, and so you volition accept at least 0 to 127. Alternatively, you lot can utilise signed char to go at least -128 to 127.

Standard Integers: int

The corporeality of memory that a single int takes depends on the hardware. However, yous can expect an int to exist at to the lowest degree 16 bits in size. This means that it tin can store values from -32,768 to 32,767, or more depending on hardware.

Like all of these other information types, there is an unsigned variant that tin can be used. The unsigned int can exist positive and nada but not negative, so it can store values from 0 to 65,535, or more depending on hardware.

Short integers: short

This doesn't get used often, but information technology's good to know that it exists. Like int, it can store -32768 to 32767. Unlike int, nevertheless, this is the extent of its power. Anywhere you tin apply brusk, you lot tin can use int.

Longer integers: long

The long data type stores integers like int, but gives a wider range of values at the toll of taking more retentivity. Long stores at least 32 bits, giving it a range of -2,147,483,648 to ii,147,483,647. Alternatively, utilize unsigned long for a range of 0 to 4,294,967,295.

Even longer integers: long long

The long long data blazon is overkill for just well-nigh every application, but C will let you use it anyhow. It's capable of storing at least −9,223,372,036,854,775,807 to 9,223,372,036,854,775,807. Alternatively, get fifty-fifty more overkill with unsigned long long, which will give you at to the lowest degree 0 to 18,446,744,073,709,551,615.

Floating point number information types

Basic Floating betoken numbers: float

float takes at least 32 bits to store, but gives us 6 decimal places from ane.2E-38 to 3.4E+38.

Doubles: double

double takes double the memory of float (and then at to the lowest degree 64 $.25). In return, double can provide 15 decimal place from 2.3E-308 to ane.7E+308.

Getting a wider range of doubles: long double

long double takes at to the lowest degree fourscore bits. As a effect, we tin get nineteen decimal places from 3.4E-4932 to 1.1E+4932.

Picking the right data type

C makes pick the data type, and makes us be very specific and intentional almost the way that we do this. This gives you a lot of power over your code, but it's important to selection the right ane.

In full general, you should option the minimum for your task. If you lot know you'll be counting from integer 1 to x, yous don't need a long and you don't need a double. If you know that you volition never have negative values, look into using the unsigned variants of the information types. By providing this functionality rather than doing it automatically, C is able to produce very low-cal and efficient code. However, information technology'due south upward to you as the developer to understand the abilities and limitations, and choose appropriately.

Nosotros tin use the sizeof() operator to bank check the size of a variable. See the following C plan for the usage of the various information types:

                #include <stdio.h>  int principal() {     int a = 1;          char b ='K';          double c = iii.fourteen;          printf("Hello World!\north");       //press the variables divers to a higher place along with their sizes     printf("Hello! I am a character. My value is %c and "            "my size is %lu byte.\n", b,sizeof(char));     //can use sizeof(b) above also       printf("Hello! I am an integer. My value is %d and "            "my size is %lu  bytes.\north", a,sizeof(int));     //tin use sizeof(a) above as well       printf("Hello! I am a double floating indicate variable."            " My value is %lf and my size is %lu bytes.\northward",c,sizeof(double));     //can use sizeof(c) above besides       printf("Bye! See you soon. :)\n");     return 0; }              

Output:

                Hi World!Hello! I am a grapheme.  My value is Yard and my size is 1 byte. Hello! I am an integer.  My value is 1 and my size is 4 bytes. Hello! I am a double floating point variable.  My value is 3.140000 and my size is viii bytes. Bye! Encounter you lot soon. :)              

The Void type

The void type specifies that no value is available. It is used in iii kinds of situations:

1. Function returns equally void

In that location are various functions in C which practise not return any value or you can say they render void. A function with no return value has the render type equally void. For example, void exit (int status);

2. Function arguments every bit void

There are various functions in C which do not take whatever parameter. A function with no parameter tin take a void. For example, int rand(void);

3. Pointers to void

A pointer of type void * represents the accost of an object, but not its type. For example, a memory allocation function void *malloc( size_t size); returns a arrow to void which tin be casted to whatever data type.


Acquire to code for free. freeCodeCamp'south open up source curriculum has helped more than 40,000 people get jobs as developers. Get started

Which Data Types Are Used To Store Whole Numbers?,

Source: https://www.freecodecamp.org/news/data-types-in-c-integer-floating-point-and-void-explained/

Posted by: herndonpudel1948.blogspot.com

0 Response to "Which Data Types Are Used To Store Whole Numbers?"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel