Displaying the various C data types and their sizes program example

 

Compiler: Visual C++ Express Edition 2005

Compiled on Platform: Windows XP Pro SP2

Header file: Standard

Additional library: none/default

Additional project setting: Set project to be compiled as C

Project -> your_project_name Properties -> Configuration Properties -> C/C++ -> Advanced -> Compiled As: Compiled as C Code (/TC)

Other info: none

To do: Displaying the various C data types and their sizes program example

To show: How to print different C data types and their respective sizes in C programming

 

/* data type and their sizes */

#include <stdio.h>

 

/* main function */

int main(void)

{

/* declare and initialize variables */

int p = 2000; /* positive integer data type */

short int q = -120; /* variation */

unsigned short int r = 121; /* variation */

float s = 21.566578; /* float data type */

char t = 'r'; /* char data type */

long u = 5678; /* long positive integer data type */

unsigned long v = 5678; /* variation */

long w = -5678; /* -ve long integer data type */

int x = -171; /* -ve integer data type */

short y = -71; /* short -ve integer data type */

unsigned short z = 99; /* variation */

double a = 88.12345; /* double float data type */

float b = -3.245823; /* float data type */

char Title[50] = "The Making of the Casino Royale";

 

printf("\t--Data type again--\n");

printf("\t-------------------\n");

 

printf("\n1. \"int\" sample: \t\t %d, the data size: %d bytes", p, sizeof(p));

printf("\n2. \"short\" int sample: \t %d, the data size: %d bytes", q, sizeof(q));

printf("\n3. \"unsigned short int\" sample: %d, the data size: %d bytes", r, sizeof(r));

printf("\n4. \"float\" sample: \t\t %.7f, the data size: %d bytes", s, sizeof(s));

printf("\n5. \"char\" sample: \t\t %c, the data size: %d byte", t, sizeof(t));

printf("\n6. \"long\" sample: \t\t %d, the data size: %d bytes", u, sizeof(u));

printf("\n7. \"unsigned long\" sample: \t %d, the data size: %d bytes", v, sizeof(v));

printf("\n8. negative \"long\" sample: \t %d, the data size: %d bytes", w, sizeof(w));

printf("\n9. negative \"int\" sample: \t %d, the data size: %d bytes", x, sizeof(x));

printf("\n10. negative \"short\" sample: \t %d, the data size: %d bytes", y, sizeof(y));

printf("\n11. unsigned \"short\" sample: \t %d, the data size: %d bytes", z, sizeof(z));

printf("\n12. \"double\" sample: \t\t %.4f, the data size: %d bytes", a, sizeof(a));

printf("\n13. negative \"float\" sample: \t %.5f, the data size: %d bytes", b, sizeof(b));

printf("\n14. The NULL terminated string data size: %d bytes\n", sizeof(Title));

 

return 0;

}

 

Output example:

 

--Data type again--

-------------------

1. "int" sample: 2000, the data size: 4 bytes

2. "short" int sample: -120, the data size: 2 bytes

3. "unsigned short int" sample: 121, the data size: 2 bytes

4. "float" sample: 21.5665779, the data size: 4 bytes

5. "char" sample: r, the data size: 1 byte

6. "long" sample: 5678, the data size: 4 bytes

7. "unsigned long" sample: 5678, the data size: 4 bytes

8. negative "long" sample: -5678, the data size: 4 bytes

9. negative "int" sample: -171, the data size: 4 bytes

10. negative "short" sample: -71, the data size: 2 bytes

11. unsigned "short" sample: 99, the data size: 2 bytes

12. "double" sample: 88.1235, the data size: 8 bytes

13. negative "float" sample: -3.24582, the data size: 4 bytes

14. The NULL terminated string data size: 50 bytes

Press any key to continue . . .

 

 

C and C++ Programming Resources | C & C++ Code Example Index