MY mENU


Tuesday 14 February 2012

Data Types in C


Data Types In C


 Mainly there are two types of data types:
     1. Simple or primivitive.
     2. Compound or structured or derived.

 An primitive data is fundamental unit of information which cannot be broken down further.


      Simple: integers, floats, characters, pointers.
       Derived data is made up of one or more simple data items.

      Compound : arrays, structures, unions

.Integers in C
      Integers stores numeric value without a decimal point in the system memory.

      Types                                         Bytes required
 
    
Short int                                               2
      Int                                                        4
      Long int                                                4



 Floats in C
     It stores numeric values with decimal point in the system memory.
     Types                                            Bytes required
    
Float                                                         4
     Double                                                      8
     Long double                                              8
   

Characters in C




     Characters is data type which stores an element of machine character set.
     The character set is used is usually ASCII set, it is denoted by char. It takes only one byte. Also singed and unsigned both occupy one byte having different  ranges.

     The primary data type themselves could be of several types. for example Character char could be Unsigned char. or Signed char. The    values  stores in the given integer variables will always be positive. for example we can declare a variables to be unsigned.
   unsigned int num_student,
    The range of integer values is -32768 to +32767 value s for a 16 bit OS to range 0 to 65535.char ch =A; where ASCII value of A is 65.


           Characters Types, Size in Bytes and Range
         
                     Type Name             Bytes           Range
                   ------------- 16 bit system -------------
                      char                          1           -128 to 127
                      signed char               1            -128 to 127
                      unsigned char           1                 0 to 255
                      short                        2             -32,768 to 32,767
                      unsigned short          2              0 to 65,535
                      int                            2              -32,768 to 32,767
                      unsigned int              2              0 to 65,535
                      long                         4              -2,147,483,648 to 2,147,483,647
                      unsigned long           4               0 to 4,294,967,295
                      float                         4              3.4E+/-38 (7 digits)
                      double                     8              1.7E+/-308 (15 digits)
                      long double            10              1.2E+/-4932 (19 digits)




Variable:                     


 A variable is a meaningful name of data storage location in computer memory. When using a variable you refer to memory address of computer.
For ex:

 int a =10;   // initialization
int a                     
// declaration




ContentProvider in Android

Content Provider Component:
       when we created a sqlite database android application. We saw that the database file is stored in the file system directories of that application meaning that the database cannot be accessed from another application. You can apply the same thing on any resources within your application (Files, images, videos). But there is a method to expose your data across multiple applications: Content Providers.

Content Providers makes a specific set of the applications data available to other applications
you can use content providers to store or retrieve data of one application from any other application. Content Provider usually the backend is SQLite. They are loosely linked to clients.  
You can think of content providers as a database server. A content provider's job is to manage access to persisted data, such as a SQLite database. there's no common storage area that all Android packages can access. 
                                                
 If your application is very simple, you might not necessarily create a content provider. If you're building a larger application, or one that makes data available to multiple activities or applications, a content provider is the means of accessing your data. A content provider is implemented as a subclass of Content Provider. We can modify the data with the help of Content Provider.

Android default content providers:There are many built-in content providers supplied by OS. They are defined in the android.provider package, they include:

  •          Browser.
  •          Call log.
  •          Live Folders.
  •          Contacts Contract.
  •          Media Store.
  •          Settings
public abstract class ContentProvider extends Object implements ComponentCallbacks
Note :- Content providers are also useful for reading and writing data that is private to your application and not shared. if you want to share data. You have two options:
     1.  You can create your own content provider and extends the ContentProvider class
     2.  You can add the data to an existing provider and gives permission to write to it.

Creating a Content Provider :- Firstly, you will set up a system for storing the data.You can store your data any way you want in SQLite database SQLiteOpenHelper class is helpful for creation of a database and SQLiteDatabase to manage it. This class extend the ContentProvider class to provide access to the data and must be declare in  AndroidManifest.xml.           
  
Content providers encapsulate data and provide it to applications through the single ContentResolver interface. The client use indirectly the ContentResolver object.You get a ContentResolver by calling getContentResolver() from within the implementation of an Activity or other application component. Any content provider is invoked by a URi in the form of content://provider_name . for example the URi of the Contacts content provider that retrieves all contacts is in the following form content://contacts/people. If you want to retrieve a particular contact (by its ID) then it would be in this form: content://contacts/people/5.

You do not need to write the URis of the content providers manually as they are stored as constant values in their respective content provider classes.

The Uri of the Contacts phones content provider is defined in 


          
 ContactsContract.CommonDataKinds.Phone.CONTENT_URI     (content://com.android.contacts/data/phones)

The Uri of the browser Bookmarks content provider is defined in 

 Browser.BOOKMARKS_URI
 (content://browser/bookmarks)

The Media store (Video) stored in external device (SD Card) is defined in 

        MediaStore.Video.Media.EXTERNAL_CONTENT_URI
          (content://media/external/video/media) and so on.

Content providers allow you to perform basic CRUD operations: Create,Read, Update and Delete on data. To retrieve data from a content provider we run a sql-like query using ManagedQuery object. The ManagedQuery object returns a cursor holding the result set.

Remember to add the permission to the manifest.xml file

<uses-permission android:name="android.permission.READ_CONTACTS"></uses-permission>

Inserting,updating and deleting:  to insert data using a content provider there are two methods

First: Using the Insert() method of your activity’s content resolver object. Like this example insert a new bookmark to the browser:
ContentValues cv=new ContentValues(); 
cv.put(Browser.BookmarkColumns.TITLE, "End Gadget"); 
cv.put(Browser.BookmarkColumns.URL, "http://www.engadget.com/"); 
cv.put(Browser.BookmarkColumns.BOOKMARK,1); 
Uri u= getContentResolver().insert(Browser.BOOKMARKS_URI, cv);
We create a ContentValues object and add to it all the required fields, then we call getContentResolver().insert method which returns the Uri of the newly inserted item. It would be in this example content://browser/bookmarks/17  We can use the generated Uri to update or delete the item later.

Second: We can replace the getcontentresolver().insert() method with bulkInsert method if we want to insert multiple items at a time. The bulkInsert(Uri url,ContentValues[] values) method returns the number of new items created.

Updating info using Content Providers: To update data using content providers, we use getContnetResolver.Update() method: This code updates the title of an existing browser bookmark:


ContentValues cv=new ContentValues(); 
cv.put(Browser.BookmarkColumns.TITLE, "End Gadget mod"); 
//uriBook= getContentResolver().insert(Browser.BOOKMARKS_URI, cv); 
getContentResolver().update(Browser.BOOKMARKS_URI, cv, BookmarkColumns.TITLE+"=?", new String[]{"End Gadget"})
the Update method has the following parameters:
  • Content Providers Uri
  • ContentValues object having the new values
  • Where clause
  • String array of the where clause arguments value
Deleting info using Content Providers: To delete info we use getContentResolver.Delete() method. To delete a bookmark:
getContentResolver().delete(Browser.BOOKMARKS_URI,BookmarkColumns.TITLE+"=?", newString[]{"End Gadget"});
the delete function has the following parameters:
  • Content Providers Uri
  • Where clause
  • String array of the where clause arguments values
Remember to add the following permissions to the manifest.xml to add and read browser bookmarks:
<uses-permission android:name="android.permission.WRITE_CONTACTS"></uses-permission>
<uses-permission android:name="com.android.browser.permission.WRITE_HISTORY_BOOKMARKS"></uses-permission>