Monday, 22 June 2015

Using L as suffix for long

There are specific suffixes for long (e.g. 39832L), float (e.g. 2.4f) and double (e.g. -7.832d).
If there is no suffix, and it is an integral type (e.g. 5623), it is assumed to be an int. If it is not an integral type (e.g. 3.14159), it is assumed to be a double.
In all other cases (byteshortchar), you need the cast as there is no specific suffix.
The Java spec allows both upper and lower case suffixes, but the upper case version for longs is preferred, as the upper case L is less easy to confuse with a numeral 1 than the lower case l.

Saturday, 20 June 2015

setvbuf in c

int setvbuf ( FILE * stream, char * buffer, int mode, size_t size );
 
 
stream
Pointer to a FILE object that identifies an open stream.
buffer
User allocated buffer. Shall be at least size bytes long.
If set to a null pointer, the function automatically allocates a buffer.
mode
Specifies a mode for file buffering. Three special macro constants (_IOFBF, _IOLBF and _IONBF) are defined in <cstdio> to be used as the value for this parameter:
_IOFBFFull buffering: On output, data is written once the buffer is full (or flushed). On Input, the buffer is filled when an input operation is requested and the buffer is empty.
_IOLBFLine buffering: On output, data is written when a newline character is inserted into the stream or when the buffer is full (or flushed), whatever happens first. On Input, the buffer is filled up to the next newline character when an input operation is requested and the buffer is empty.
_IONBFNo buffering: No buffer is used. Each I/O operation is written as soon as possible. In this case, the buffer and size parameters are ignored.
size
Buffer size, in bytes.
If the buffer argument is a null pointer, this value may determine the size automatically allocated by the function for the buffer
 

Friday, 19 June 2015

Handling error in c

It is used to display the error no 
 
#include <stdio.h>
#include <errno.h>

extern int errno;

int main () {
 FILE * fp;
 fp = fopen ("filedoesnotexist.txt", "rb");
 if (fp == NULL) {
  fprintf(stderr, "Value of errno: %d\n", errno);
 } else {
  fclose (fp);
 }
 return 0;
} 
 
 To display error message use 
strerror(errno) in printf
or
perror("message to be displayd before error");

Wednesday, 17 June 2015

Difference between c and c++

Difference between C and C++ - C Vs C++


S. No.
C
C++
1
C is a structural or procedural programming language.
C++ is an object oriented programming language.
2
Emphasis is on procedure or steps to solve any problem.
Emphasis is on objects rather than procedure.
3
Functions are the fundamental building blocks.
Objects are the fundamental building blocks.
4
In C, the data is not secured.
Data is hidden and can’t be accessed by external functions.
5
C follows top down approach.
C++ follows bottom up approach
6
C uses scanf() and printf() function for standard input and output.
C++ uses cin>> and cout<< for standard input and output.
7
Variables must be defined at the beginning in the function.
Variables can be defined anywhere in the function.
8
In C, namespace feature is absent.
In C++, namespace feature is present.
9
C is a middle level language.
C++ is a high level language.
10
Programs are divided into modules and functions.
Programs are divided into classes and functions.
11
C doesn't support exception handling directly. Can be done by using some other functions.
C++ supports exception handling. Done by using try and catch block.
12
Features like function overloading and operator overloading is not present.
C++ supports function overloading and operator overloading.
13
C program file is saved with .C extension.
C++ program file is saved with .CPP extension.



VIRTUAL FUNCTION

If there are member functions with same name in base class and derived class, virtual functions gives programmer capability to call member function of different class by a same function call depending upon different context. This feature in C++ programming is known as polymorphism which is one of the important feature of OOP.
If a base class and derived class has same function and if you write code to access that function using pointer of base class then, the function in the base class is executed even if, the object of derived class is referenced with that pointer variable.

Difference Between Procedure Oriented Programming (POP) & Object Oriented Programming (OOP)
Procedure Oriented ProgrammingObject Oriented Programming
Divided IntoIn POP, program is divided into small parts called functions.In OOP, program is divided into parts called objects.
ImportanceIn POP,Importance is not given to data but to functions as well as sequence of actions to be done.In OOP, Importance is given to the data rather than procedures or functions because it works as a real world.
ApproachPOP follows Top Down approach.OOP follows Bottom Up approach.
Access SpecifiersPOP does not have any access specifier.OOP has access specifiers named Public, Private, Protected, etc.
Data MovingIn POP, Data can move freely from function to function in the system.In OOP, objects can move and communicate with each other through member functions.
ExpansionTo add new data and function in POP is not so easy.OOP provides an easy way to add new data and function.
Data AccessIn POP, Most function uses Global data for sharing that can be accessed freely from function to function in the system.In OOP, data can not move easily from function to function,it can be kept public or private so we can control the access of data.
Data HidingPOP does not have any proper way for hiding data so it is less secure.OOP provides Data Hiding so provides more security.
OverloadingIn POP, Overloading is not possible.In OOP, overloading is possible in the form of Function Overloading and Operator Overloading.
ExamplesExample of POP are : C, VB, FORTRAN, Pascal.Example of OOP are : C++, JAVA, VB.NET, C#.NET.