Wednesday, October 13, 2010

Data Types in C Language With Size

,

C has a concept of 'data types' which are used to define a variable before its use. C compilers support four fundamental data types, namely integer (int), character (char), floating point (float), and double-precision floating point (double). Like integer data type, other data types also offer extended data types such as long double and signed char.

Data Type

Data Type of a variable tell to the compiler
  • Name of variable
  • Which type of value that variable can hold
  • How much memory space it reserve
1. Primary data type or predefined data type or Fundamental data type - are those data type that are defined in c already.
Data Type
Used for
Size(in Byte)
Range
Format
Int Integer 2 -32768 to 32767
%d
Char Character 1 -128 to 127
%c
Float Single precision floating point number 4 -3.4e38 to +3.4e38
%f
Double double precision floating point number 8 -1.7e308 to +1.7e308
%lf
Void - - -
0


Data Type Modifiers –There are four modifiers available.
  • Signed
  • Unsigned
  • Short
  • Long
Data Type
Size(in Byte)
Range
Format
signed char  1 -128 to + 127
%c
unsigned char  1 0 to 255
%c
signed int  2 -32768 to +32767
%d
unsigned int  2 0 to 65535
%u
short signed int  2 -32768 to +32767
%d
short unsigned int  2 0 to 65535
%u
long double  10 -1.7e4932 to +1.7e4932
%Lf

2. Derived data type - are those data type that are derived by predefined data type.

Example-Array, pointer


3. User defined data type - are those data type that are defined by the user itself.

Example-struct, union

Read more →

File Handling in C Language

,

File handling means one can perform operations like create, modify, delete etc on system files. You want to read from or write to one of the files you're working with, you identify that file by using its file pointer.

File Handling Funtions :

fopen() : The fopen() function is used to open a file and associates an I/O stream with it.

The mode string "r" indicates reading. Mode "w" indicates writing, so we
could open. The third major mode is "a" for append. mode is "+" for read and write. Mode "b" character to indicate that you want to do “binary''

fread() and fwrite() : The functions fread/fwrite are used for reading/writing data from/to the file opened by fopen function.

fseek() : The fseek() function is used to set the file position indicator for the stream to a new position.

fclose() : The fclose() function first flushes the stream opened by fopen() and then closes the underlying descriptor.

You declare a variable to store a file pointer like this:

FILE *fp;

If you were reading from one file and writing to another you might declare an input file pointer and an output file pointer:

FILE *ifp, *ofp;

One thing to beware of when opening files is that it's an operation which may fail, file might not exist, or it might be protected against reading or writing. Fopen returns a null pointer if it can't open the requested file, and it's important to check for this case before going off and using fopen's return value as a file pointer. eg:

ifp = fopen("input.dat", "r");
if(ifp == NULL)
{
 printf("can't open file\n");
 exit or return
}


Example : Writing a data to the file

#include <stdio.h>
main( )
{
  FILE *fp;
  char stuff[25];
  int index;
  fp = fopen("TENLINES.TXT","w");
  strcpy(stuff,"This is an example line.");
  for (index = 1; index <= 10; index++)
  fprintf(fp,"%s Line number %d\n", stuff, index);
  fclose(fp);
}

Read more →

String In C Language

,


The string in C programming language are represented by arrays of characters. The end of the string is marked with a special character, the null character, which is simply the character with the value 0. Whenever we write a string, enclosed in double quotes, C automatically creates an array of characters for us, containing that string, terminated by the \0 character.

For example, we can declare and define an array of characters, and initialize it with a string constant:

char string[ ] = "Hellopg";



Print With Example :

#include <stdio.h>
int main ()
{
   char Value[8] = {'H', 'e', 'l', 'l', 'o','p','g', '\0'};
   printf("Greeting message: %s\n", Value );
   return 0;
}

 
 Function  Purpose
 strcpy(s1, s2); Copies string s2 into string s1.
 strcat(s1, s2); Concatenates string s2 onto the end of string s1.
 strlen(s1); Returns the length of string s1.
 strcmp(s1, s2); Returns 0 if s1 and s2 are the same; less than 0 if s1<s2; greater than 0 if s1>s2.
 strchr(s1, ch); Returns a pointer to the first occurrence of character ch in string s1.
 strstr(s1, s2); Returns a pointer to the first occurrence of string s2 in string s1.

Copy Function :

#include <string.h>

char string1[ ] = "Hello, world!";
char string2[20];
strcpy(string2, string1);

Try all function and observe result.
Read more →

How structures are to be used in c

,
Read more →

Thursday, October 7, 2010

What is Inheritance?

,
What is Inheritance?

Inheritance is one of the important feature of the OOP.Inheritance is the method by which objects of one class gets the properties of another class.

Advantages of Inheritance?

Sometimes we need to repeat the code or we need repeat the whole class properties. So It helps in various ways.

1.) It saves memory space.

2.) It saves time.

3.) It will remove frustration.

4.) It increases reliability of the code

5.) It saves the developing and testing efforts.

Why we use Inheritance?

To increase the reusability of the code and to make further usable for another classes. We use the concept of inheritance.

How Inheritance can be achieved?

In the inheritance there will be a parent child relationship .There is a class called in which all the properties are defined and generally,it is refered as a base class.There is also an another class in which is derived from the existing one are known as a derived class.

Types of Inheritance

There are Five types of Inhertance found in Object Oriented Programming.

1.)Single Inheritance

2.)Multiple Inheritance

3.)Hierarchical Inheritance

4.)Multiple Inheritance

5.)Hybrid inheritance
Read more →