Fundemental Data Types - chapter 3


#include <stdio.h>

int main(void)
{
   int   a, b, c;                     /* declaration */
   float x, y=3.3, z=-7.7;            /* declaration with initialization */ 

   printf("Input two integers: ");    /* function call */
   scanf("%d%d", &b, &c);             /* function call */
   a = b + c;                         /* assignment stmt */
   x = y + z;                         /* assignment stmt */
}
statements.c

Fundamental data types: long form

char signed char unsigned char
signed short int signed int signed long int
usigned short int unsigned int unsigned long int
float double long double

Fundamental data types

char signed char unsigned char
short int long
usigned short unsigned unsigned long
float double long double

Fundamental data types grouped by funcionality

Integral types
char signed char unsigned char
short int long
usigned short unsigned unsigned long
Floating types
float double long double
Arithmetic types Integral types + Floating types

Some character constants and their integer values

character:
integer value:
'a' 'b' 'c'...'z'
9798 99...112
character:
integer value:
'A' 'B' 'C'...'Z'
65 66 67...90
character:
integer value:
'0' '1' '2'...'9'
48 49 50...57
character:
integer value:
'&' '*' '+'
38 42 43

Special Characters

Name of characterWritten in CInteger value
alert \a 7
backslash \\ 92
horizontal tab \t 9
newline \n 10
null character \0 0

what is ASCII?


Printing

char   c = 'a';

printf("%c", c);    /*  a is printed */
printf("%d", c);    /* 97 is printed */

printf("%c%c%c", c, c+1, c+2);    /* abc is printed */
print1.c
char c;
int  i;

for (i='a'; i<='z'; ++i)
  printf("%c", i);         /* abc...z is printed */

for (c=65; c<=90; ++c)
  printf("%c", c);         /* ABC...Z is printed */

for (c='0'; c<='9'; ++c)
  printf("%d", c);         /* 48 49 50 ... 57 is printed */
print2.c

The sizeof operator

/* Compute the size of some fundamental types. */

#include <stdio.h>

int main(void)
{
  printf("The size of some fundamental types is computed.\n\n");
  printf("       char:%3d byte \n", sizeof(char));
  printf("      short:%3d bytes\n", sizeof(short));
  printf("        int:%3d bytes\n", sizeof(int));
  printf("       long:%3d bytes\n", sizeof(long));
  printf("   unsigned:%3d bytes\n", sizeof(unsigned));
  printf("      float:%3d bytes\n", sizeof(float));
  printf("     double:%3d bytes\n", sizeof(double));
  printf("long double:%3d bytes\n", sizeof(long double));
  return 0;
}
sizeof.c

Output

run on "scorpio":

The size of some fundamental types is computed.

       char:  1 byte 
      short:  2 bytes
        int:  4 bytes
       long:  4 bytes
   unsigned:  4 bytes
      float:  4 bytes
     double:  8 bytes
long double:  8 bytes

Sizes guaranteed

sizeof(char) = 1
sizeof(short) <= sizeof(int) <= sizeof(long)
sizeof(signed) = sizeof(unsigned) = sizeof(int)
sizeof(float) <= sizeof(double) <= sizeof(long double)

Sizes and overflow

#include <stdio.h>

int main(void)
{
   int        i;
   unsigned   u = UINT_MAX;   /* typically 4294967295 */

   for (i = 0; i < 5; ++i)
      printf("%u + %d = %u\n", u, i, u + i);
   for (i = 0; i < 5; ++i)
      printf("%u * %d = %u\n", u, i, u * i);
   return 0;
}
overflow.c

Output

run on "scorpio":

4294967295 + 0 = 4294967295
4294967295 + 1 = 0
4294967295 + 2 = 1
4294967295 + 3 = 2
4294967295 + 4 = 3
4294967295 * 0 = 0
4294967295 * 1 = 4294967295
4294967295 * 2 = 4294967294
4294967295 * 3 = 4294967293
4294967295 * 4 = 4294967292

The use of getchar() and putchar()

#include <stdio.h>

int main(void)
{
  int   c;

  while ((c = getchar()) != EOF) {
    putchar(c);
    putchar(c);
  }
  return 0;
}
double_out.c

Square root and power math functions

#include <math.h>
#include <stdio.h>

int main(void)
{
  double   x;

  printf("\n%s\n%s\n%s\n\n",
    "The square root of x and x raised",
    "to the x power will be computed.",
    "---");
  while (1) {                  /* do it forever */
    printf("Input x:  ");
    scanf("%lf", &x);
    if (x >= 0.0)
      printf("\n%15s%22.15e\n%15s%22.15e\n%15s%22.15e\n\n",
        "x = ", x,
        "sqrt(x) = ", sqrt(x),
        "pow(x, x) = ", pow(x, x));
    else
      printf("\nSorry, your number must be nonnegative.\n\n");
  }
  return 0;
}
power_square.c

Output

The square root of x and x raised to the x power will be computed.
Input x:  2

           x = 2.000000000000000e+000
     sqrt(x) = 1.414213562373095e+000
   pow(x, x) = 4.000000000000000e+000

Input x:  

The usual arithmetic conversions


Automatic Conversion

char c;   short s;    int i;
long l;   unsigned u; unsigned long ul;
float  f; double d;   long double ld;
Expression Type Expression Type
c - s / i int u * 7 - i unsigned
u * 2.0 - i double f * 7 - i float
c + 3 int 7 * s * ul unsigned long
c + 5.0 double ld + c long double
d + s double u - ul unsigned long
2 * i / l long u - l system dependent

Decimal, hexadecimal, octal conversions

/* Decimal, hexadecimal, octal conversions. */

#include <stdio.h>

int main(void)
{
   printf("%d  %x  %o\n", 19, 19, 19);        /* 19  13  23 */
   printf("%d  %x  %o\n", 0x1c, 0x1c, 0x1c);  /* 28  1c  34 */
   printf("%d  %x  %o\n", 017, 017, 017);     /* 15   f  17 */
   printf("%d\n", 11 + 0x11 + 011);           /* 37         */
   printf("%x\n", 2097151);                   /* 1fffff     */
   printf("%d\n", 0x1FfFFf);                  /* 2097151    */
   return 0;
}
conversions.c
Number: Radix: Number:

prev top


Last modified: July 23 2004.