×
>
<

Aptitude

C Datatypes | CrackEase

Data types in C

data types intro

What are Data Types?

A data type in C specifies the kind of data a variable can hold (for example integer, character, floating-point) and determines the operations that can be performed on that data. The compiler uses data types to allocate appropriate memory and to enforce correct operations.

Primary Data Types
primary data types
  • Primary (primitive) data types are built into the language and directly supported by the compiler.
  • C provides the following fundamental data types:
    1. char — character type
    2. int — integer type
    3. float — single-precision floating point
    4. double — double-precision floating point
Declaration of Primary Data types
  • Syntax: DATA_TYPE VARIABLE_NAME;

  • Variable names must start with a letter or underscore (_), and may contain letters, digits and underscores.

  • Variable names in C are case-sensitive: count and Count are distinct.

  • Special characters like @, #, $ (except underscore) cannot be used in identifiers.

  • Examples: int a;, float b;, char c;

Initialization of Primary Data types
  • Initialization means assigning a value to a variable at the time of declaration: DATA_TYPE VARIABLE = VALUE;

  • Example: int a = 10;, float pi = 3.14;

  • Initialization can be done at compile time (literal assigned in code) or at run time (value read from user/input).

  • The scope (visibility) of a variable depends on whether it is declared global or local (inside a function/block).

Compile Time example
#include <stdio.h>

int main(void)
{
   int a = 10;  
   float b = 10.56f;
   printf("a = %d, b = %f\n", a, b);
   return 0;
}
Run Time example
#include <stdio.h>

int main(void)
{
   int a; 
   float b;  
   printf("Enter a : ");
   if (scanf("%d", &a) != 1) return 1;
   printf("Enter b : ");
   if (scanf("%f", &b) != 1) return 1;
   printf("You entered: a = %d, b = %f\n", a, b);
   return 0;
}
Char

char Data Type

  • char stores a single character (typically 1 byte).

  • Can hold letters, digits or special characters, for example 'A', '7', '#'.

  • Use %c for input/output with scanf/printf.

Example of char
#include <stdio.h>

int main(void)
{
    char c = 'a';
    char d;
    d = 'b';
    printf("%c\t", c);
    printf("%c\t\n", d);
    return 0;
}
Taking character input at run time
#include <stdio.h>

int main(void)
{
    char x;
    printf("Enter x : ");
    if (scanf(" %c", &x) == 1)   /* note space before %c to skip whitespace */
        printf("You entered: %c\n", x);
    return 0;
}
Float

float Data Type

  • float stores single-precision real numbers (numbers with fractional part).

  • Used for decimal values, e.g. float a = 12.75f;

  • Use %f with printf/scanf. For formatted output use precision specifiers like %.2f.

Float Example
#include <stdio.h>

int main(void)
{
    float x = 10.3897f;
    float y = 20.4556f;
    printf("x = %.4f\t", x);   /* print with 4 digits after decimal */
    printf("y = %.4f\n", y);
    return 0;
}
Use of some common Data Types
#include <stdio.h>

int main(void)
{
    float x, y;
    printf("Enter x : ");
    if (scanf("%f", &x) != 1) return 1;
    y = 20.4556f;
    printf("You entered x = %.4f\t y = %.4f\n", x, y);
    return 0;
}
Range of the datatypes (examples)
#include <stdio.h>
#include <string.h>

int main(void)
{
    int a = -123456;               /* integers */
    unsigned int b = 123456u;      /* only non-negative */

    short c = -123;                /* small integers */
    unsigned short d = 123u;       

    long e = -1234567890L;         /* large integers */
    unsigned long f = 1234567890UL;

    long long g = -1234567890123LL;       /* very large integers */
    unsigned long long h = 1234567890123ULL;

    char i = 'Z';                  /* character */
    char j[] = "CrackEx";

    float k = 1.0001f;             /* single precision */
    double l = 123456.0000000001;  /* double precision */
    long double m = 1234567890.00000000000001L; /* long double */

    printf("%d\n", a);
    printf("%u\n", b);

    printf("%hd\n", c);
    printf("%hu\n", d);

    printf("%ld\n", e);
    printf("%lu\n", f);

    printf("%lld\n", g);
    printf("%llu\n", h);

    printf("%c\n", i);
    printf("%s\n", j);

    printf("%f\n", k);
    printf("%lf\n", l);
    printf("%Lf\n", m);

    return 0;
}
Storage size of some common Data Types
#include <stdio.h>

int main(void)
{
    printf("Storage size for char is: %zu byte(s)\n", sizeof(char));

    printf("Storage size for int is: %zu byte(s)\n", sizeof(int));
    printf("Storage size for unsigned int is: %zu byte(s)\n", sizeof(unsigned int));

    printf("Storage size for short is: %zu byte(s)\n", sizeof(short));
    printf("Storage size for unsigned short is: %zu byte(s)\n", sizeof(unsigned short));

    printf("Storage size for long  is: %zu byte(s)\n", sizeof(long));
    printf("Storage size for unsigned long is: %zu byte(s)\n", sizeof(unsigned long));    

    printf("Storage size for long long is: %zu byte(s)\n", sizeof(long long));
    printf("Storage size for unsigned long long is: %zu byte(s)\n", sizeof(unsigned long long));

    printf("Storage size for float is: %zu byte(s)\n", sizeof(float));
    printf("Storage size for double is: %zu byte(s)\n", sizeof(double));
    printf("Storage size for long double is: %zu byte(s)\n", sizeof(long double));

    return 0; 
}
Typical output (platform dependent)

Storage size for char is: 1 byte(s)
Storage size for int is: 4 byte(s)
Storage size for unsigned int is: 4 byte(s)
Storage size for short is: 2 byte(s)
Storage size for unsigned short is: 2 byte(s)
Storage size for long  is: 8 byte(s)
Storage size for unsigned long is: 8 byte(s)
Storage size for long long is: 8 byte(s)
Storage size for unsigned long long is: 8 byte(s)
Storage size for float is: 4 byte(s)
Storage size for double is: 8 byte(s)
Storage size for long double is: 16 byte(s)
Footer Content | CrackEase