quick.gif

space2.gif

space2.gif

space2.gif

space2.gif

space2.gif

space2.gif

space2.gif

   

space.gif

   

space.gif

  ../images/main/bullet_green_ball.gif C Data Types

C has a concept of 'data types' which are used to define a variable before its use.

   

space.gif

The definition of a variable will assign storage for the variable and define the type of data that will be held in the location.

   

space.gif

Following are the data types available in C language.

   

space.gif

  • int : integer variable
  • short : short integer
  • long : long integer
  • float : single precision real (floating point) variable
  • double : double precision real (floating point) variable
  • char : character variable (single byte)
  • void : variable without any type.
  • enum : enumerated data type.
   

space.gif

The printf function can be instructed to print integers, floats and strings properly. The general syntax is

   

space.gif

printf( "format", variables );

   

space.gif

where "format" specifies the converstion specification and variables is a list of quantities to print. Some useful formats are

   

space.gif

  • %.nd integer (optional n = number of columns; if 0, pad with zeroes)
  • %m.nf float or double (optional m = number of columns, n = number of decimal places)
  • %ns string (optional n = number of columns)
  • %c character
  • \n \t to introduce new line or tab
  • \g ring the bell (``beep'') on the terminal
   

space.gif

  ../images/main/bulllet_4dots_orange.gif int - data type

int is used to define integer numbers.

   

space.gif


 1  {
 2     int Count;
 3         Count = 5;
 4   }
You could download file int.c here
   

space.gif

  ../images/main/bulllet_4dots_orange.gif short- data type

short is used to define integer numbers.

   

space.gif


 1  {
 2     short Count;
 3         Count = 5;
 4   }
You could download file short.c here
   

space.gif

  ../images/main/bulllet_4dots_orange.gif long- data type

long is used to define integer numbers.

   

space.gif


 1  {
 2     long Count;
 3         Count = 5;
 4   }
You could download file long.c here
   

space.gif

  ../images/main/bulllet_4dots_orange.gif float - data type

float is used to define floating point numbers.

   

space.gif


 1  {
 2         float Miles;
 3         Miles = 5.6;
 4     }
You could download file float.c here
   

space.gif

  ../images/main/bulllet_4dots_orange.gif double - data type

double is used to define BIG floating point numbers. It reserves twice the storage for the number. On PCs this is likely to be 8 bytes.

   

space.gif


 1  {
 2         double Atoms;
 3         Atoms = 2500000;
 4     }
You could download file double.c here
   

space.gif

   

space.gif

  ../images/main/bulllet_4dots_orange.gif char - data type

char defines characters.

   

space.gif


 1  {
 2         char Letter;
 3         Letter = 'x';
 4     }
You could download file char.c here
   

space.gif

  ../images/main/bulllet_4dots_orange.gif void

The void keyword allows us to create functions that either do not require any parameters or do not return a value.

   

space.gif


  1 #include <stdio.h>
  2 
  3 void Print_Square(int Number);
  4 
  5 void main ()
  6 {
  7    Print_Square(5);
  8    exit(0);
  9 }
 10 
 11 void Print_Square(int Number)
 12 {
 13    printf("%d squared is %d\n",Number, Number*Number);
 14 }
You could download file void.c here
   

space.gif

  ../images/main/bulllet_4dots_orange.gif enum

ENUM allows you to define a list of aliases which represent integer numbers.

   

space.gif


  1 #include <stdio.h>
  2 
  3 enum boolean {FALSE=0, TRUE };
  4 enum months  {Jan=5, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec};
  5  
  6 void main() {
  7   enum months month;
  8   enum boolean mybool;
  9   printf("Month %d\n", month=Aug);
 10   printf("Bool %d\n", mybool=TRUE);
 11 }
You could download file enum.c here
   

space.gif

  ../images/main/bulllet_4dots_orange.gif Global Variables

Local variables are declared within the body of a function, and can only be used within that function. This is usually no problem, since when another function is called, all required data is passed to it as arguments. Alternatively, a variable can be declared globally so it is available to all functions. Modern programming practice recommends against the excessive use of global variables. They can lead to poor program structure, and tend to clog up the available name space.

   

space.gif

A global variable declaration looks normal, but is located outside any of the program's functions. This is usually done at the beginning of the program file, but after preprocessor directives. The variable is not declared again in the body of the functions which access it.

   

space.gif

  ../images/main/bulllet_4dots_orange.gif Static Variables

Another class of local variable is the static type. A static can only be accessed from the function in which it was declared, like a local variable. The static variable is not destroyed on exit from the function, instead its value is preserved, and becomes available again when the function is next called. Static variables are declared as local variables, but the declaration is preceeded by the word static.

   

space.gif

static int counter;

   

space.gif

Static variables can be initialised as normal, the initialisation is performed once only, when the program starts up.

   

space.gif

  ../images/main/bulllet_4dots_orange.gif External Variables

Where a global variable is declared in one file, but used by functions from another, then the variable is called an external variable in these functions, and must be declared as such. The declaration must be preceeded by the word extern. The declaration is required so the compiler can find the type of the variable without having to search through several source files for the declaration.

   

space.gif

Global and external variables can be of any legal type. They can be initialised, but the initialisation takes place when the program starts up, before entry to the main function.

   

space.gif

  ../images/main/bulllet_4dots_orange.gif Arrays

Arrays of any type can be formed in C. The syntax is simple

   

space.gif

type name[dim];

   

space.gif

Arrays can be created from any of the C data types int, float, char. I start with int and float as these are the easiest to understand. Then move onto int and float multi dimentional arrays and finally char arrays

   

space.gif

In C, arrays starts at position 0. The elements of the array occupy adjacent locations in memory. C treats the name of the array as if it were a pointer to the first element--this is important in understanding how to do arithmetic with arrays. Thus, if v is an array, *v is the same thing as v[0], *(v+1) is the same thing as v[1], and so on.

   

space.gif

../images/systemc/pointer-2.gif
   

space.gif


  1 #define SIZE 3
  2 
  3 void main() {
  4   float x[SIZE];
  5   float *fp;
  6   int   i;
  7   // initialize the array x use a "cast" to force i into the equivalent float  
  8   for (i = 0; i < SIZE; i++) {
  9       x[i] = 0.5*(float)i;
 10   }
 11   for (i = 0; i < SIZE; i++) {
 12       printf("  %d  %f \n", i, x[i]);
 13   }
 14   // make fp point to array x     
 15   fp = x;
 16   // print via pointer arithmetic members of x are adjacent to   
 17   // each other in memory *(fp+i) refers to content of	  
 18   // memory location (fp+i) or x[i] 
 19   for (i = 0; i < SIZE; i++) {
 20       printf("  %d  %f \n", i, *(fp+i));
 21   }
 22 }
You could download file array.c here
   

space.gif

   

space.gif

   

space.gif

   

space.gif

space2.gif

space2.gif

space2.gif

space2.gif

space2.gif

  

Copyright 1998-2014

Deepak Kumar Tala - All rights reserved

Do you have any Comment? mail me at:deepak@asic-world.com