Programming with C

⌘K
  1. Home
  2. Docs
  3. Programming with C
  4. Basics of C Programming
  5. Data Types

Data Types

Data type is one most important attributes of an identifier. It determines the possible values that an identifier can have and the valid operations that can be applied on it.

In C language, data types are broadly classified as               

  • Basic data types (primitive data type)
  • Derived data types
  • User-defined data types

Basic / Primitive data types

The five basic data types and their corresponding keywords available in C are:

  • Character (char)
  • Integer (int)
  • Single precision floating point (float)
  • Double precision floating point (double)
  • No value available (void)

Derived data types

These data types are derived from basic data type. Derived data types available in C are

  • Array type (Example: char[ ], int[ ])
  • Pointer type (Example: char*, int*)
  • Funtion type (Example: int(int,int), float(int))

User-defined data types

The C language provides flexibility to the user to create new data types. These newly created data types are called user-defined data types. The user defined data types can be created using:

  • Structure
  • Union
  • Enumeration

Data types and range

Example

#include <stdio.h>
int main()
{
    char data = 'A';
    printf("%c\n", data);
    return 0;
}

Output

A
#include <stdio.h>
int main()
{
    int data = 65;
    printf("%d\n", data);
    printf("%u\n", data);
    printf("%i\n", data);
    return 0;
}

Output

65
65
65
#include <stdio.h>
int main()
{
    float data = 6.27;
    printf("%f\n", data);
    printf("%e\n", data);
    return 0;
}

Output

6.270000
6.270000e+000
#include <stdio.h>
int main()
{
    float data = 6.276240;
    printf("%f\n", data);
    printf("%0.2f\n", data);
    printf("%0.4f\n", data);
    return 0;
}

Output

6.276240
6.28
6.2762
#include <stdio.h>
int main()
{
    int pos = 14;
    float data = 1.2;
    printf("%*f",pos,data);
    return 0;
}

Output

1.200000
#include <stdio.h> 
int main() 
{ 
    int data = 65;
    printf("%o\n", data); 
    return 0; 
}

Output

101
#include <stdio.h>
int main()
{
    int data = 11;
    printf("%x\n", data);
    return 0;
}

Output

b
#include <stdio.h>
int main()
{
    char str1[] = "hello";
    printf("%s\n", str1);
    printf("%24s\n", str1);
    printf("%-24s\n", str1);
    printf("%24.2s\n", str1);
    printf("%-24.4s\n", str1);
    return 0;
}

Output

hello
                   hello
hello
                      he
hell

Views: 0

How can we help?

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments