C/Data Types and Keywords

From Wikiversity
< C
Jump to: navigation, search

C Data Types[edit]

In C, there is a concept called "datatypes". Data types indicate the type of data a variable can hold. When a variable is defined, a memory location will be assigned to the newly defined variable and it will also define the type of data that memory location will hold. C has following data types

  • int - an integer; reflects size of integers on host machine
  • float - single-precision floating point
  • double - double-precision floating point
  • char - character, a single byte

In addition to basic data types C also defines certain qualifiers to these data types. Qualifiers are used to make variable declaration more specific to variable uses. Qualifiers available in the C language are:

  • short (applied to integers)
  • long (applied to integers)
  • signed (applied to char, or any integer)
  • unsigned (applied to char, or any integer)

With application of these qualifiers basic data types can be flavoured in many ways as given in the table below. Note that the values given are acceptable minimum magnitudes defined by the C Standard - each implementation will define values greater or equal in magnitude.

Data Type Bits Range Begin Range End
char 8 -127 127
unsigned char 8 0 255
short int 16 -32767 +32767
unsigned short 16 0 65,535
int 16 -32,767 32,767
unsigned int 16 0 65,535
long int 32 -2,147,483,647 2,147,483,647
unsigned long int 32 0 4,294,967,295
float 32 1e-37 1e+37
double 32 1e-37 1e+37
long double 32 1e-37 1e+37

Example: the arithmetic mean of two numbers[edit]

Borland Turbo-C (Linux/Unix users may need Linux implementation of conio.h):

#include <stdio.h>
#include <conio.h>

void main()
{
  int a,b;
  float avg;                   // data type

  printf("Enter the a:");
  scanf("%d",&a);

  printf("Enter the b:");
  scanf("%d",&b);

  avg=(a+b)/2;                 // expression

  printf("%f",avg);
  getch();                     // getchar() may work instead

  return 0;
}


GCC version (may have to be compiled with "-lcurses" under MacOS X and "-lncurses" under Linux):

#include <stdio.h>

int main()
{
  int a,b;
  float avg;                   // data type

  initscr();
  cbreak(); 
  echo();

  printw("Enter the a:");
  refresh();
  scanw("%d",&a);
  clear();
 
  printw("Enter the b:");
  refresh();
  scanw("%d",&b);
  clear();
  avg=(a+b)/2;                 // expression
  
  printw("%f",avg);
  refresh();
  getchar();                     
  endwin();
 
  return 0;
}

Exercises[edit]

  • What data type(s) could you use if you were creating a program that stored monetary sums (Dollars, Euros, etc.)?
  • What data type could you use to store if you wanted someone to type their name into your program?
  • What data type(s) could you need if you wanted to count from 0 to 100?
  • Compiler dependent: Modify the "arithmetic mean" example program in order to get the right result even if a and b differ by an odd number (e.g. a=1 and b=2).



Project: Topic:C
Previous: Basic Output — C/Data Types and Keywords — Next: Type Qualifiers