Showing posts with label C Programming. Show all posts
Showing posts with label C Programming. Show all posts

Monday, January 30, 2012

Convert Date to MJD format


unsigned short int  VN_ConvertDate_to_MJD(unsigned short int ui16Day, unsigned short int  ui16Month, unsigned short int  ui16Year)
{
if(ui16Month<3)
{
    --ui16Year;
    ui16Month+=12;
}

ui16Year+=4800;
ui16Month-=3;

        return (ui16Year*365) +(ui16Year/4) + (30*ui16Month) +(ui16Month*3+2) / 5 + ui16Day -  
        7265/*32083 - 24817*/;
}

MJD - Modified Julian Date.

Friday, January 27, 2012

Segmentation violation & Bus error


Explain the meaning of "Segmentation violation".
A segmentation violation usually indicates an attempt to access memory which doesn't even exist.

What is "Bus error"?
A bus error indicates an attempt to access memory in an illegal way,perhaps due to an unaligned pointer.

Sunday, January 15, 2012

Little endian to big endian

//2-byte number
int SHORT_little_endian_to_big_endian( int i)
{
    return (( i>>8)&0xff)+((i<<8)&0xff00);
}
//4-byte number
int INT_little_endian_To_big_endian(int i)
{
    return((i&0xff)<<24)+((i&0xff00)<<8)+((i&0xff0000)>>8)+((i>>24)&0xff);
}

Difference between memcpy and memmove

memmove offers guaranteed behavior if the source and destination arguments overlap. memcpy makes no such guarantee, and may therefore be more efficiently implementable. When in doubt, it's safer to use memmove.

Thursday, November 10, 2011

Constant Pointers & Pointer to Constant


1) Constant Pointers : These type of pointers are the one which cannot change address they are pointing to. This means that suppose there is a pointer which points to a variable (or stores the address of that variable). Now if we try to point the pointer to some other variable (or try to make the pointer store address of some other variable), then constant pointers are incapable of this.

A constant pointer is declared as : 'int *const ptr' ( the location of 'const' make the pointer 'ptr' as constant pointer)

2) Pointer to Constant : These type of pointers are the one which cannot change the value they are pointing to. This means they cannot change the value of the variable whose address they are holding.

A pointer to a constant is declared as : 'const int *ptr' (the location of 'const' makes the pointer 'ptr' as a pointer to constant.

Monday, November 06, 2006

C program to count the number of bits set in an unsigned integer

/*
Program to count no. of bits in an unsigned integer
*/
#include
#include

void main( void )
{
unsigned int a = 15;
int count = 0;

while( a )
{
++count;
a = a & ( a - 1 );
}

clrscr();
printf( "Count is %d\n", count );
getch();
}

Thursday, June 15, 2006

BITWISE OPERATOINS

Refer:
http://graphics.stanford.edu/~seander/bithacks.html

Register Storage Class

register is used to define local variables that should be stored in a register instead of RAM. This means that the variable has a maximum size equal to the register size (usually one word) and cant have the unary '&' operator applied to it (as it does not have a memory location).
{
register int Miles;
}
Register should only be used for variables that require quick access - such as counters. It should also be noted that defining 'register' goes not mean that the variable will be stored in a register. It means that it MIGHT be stored in a register - depending on hardware and implimentation restrictions.

Duplicate Characters deletion - String Operation

String Operation
Duplicate Characters deletion
/*
Name: Duplicate Characters deletion
Copyright: StarSoft
Author: Vijayakumar M
Date: 28/04/06 11:14
Description: To Remove Duplicate
characters in a string.
*/
#include
#include
#include
main()
{
char string[40]="thats goods super star";
char *p= string;
int i, j;
printf( "%s\n", string );
for( i=0;i {
for( j=i+1; j {
if( string[i]==string[j])
*(p+j)='*';
}
}
printf( "%s\n", string );
for( i=0;i {
if( string[i] != '*' )
printf( "%c", string[i] );
}
   getch();
}

how to find give no is 2 power of n

f=( ( no & ( no-1 ) ) == 0 )

if( f == 1)
printf( "The Given Number is Power of %d", no );
else
printf( "The Given Number is not Power of %d", no );

BITWISE OPERATIONS

#define SET_BIT( _X_, _NO_ ) ( 1<<(_X_-1)) _NO_
#define RESET_BIT( _X_, _NO_ ) ~( ( 1<<(_X_-1) ) ) & _NO_
#define SWAP_BIT( _X_, _NO_ ) ( 1<<(_X_-1)) ^ _NO_

Sunday, March 19, 2006

Determine if a Variable is Signed or Not?

I hope you thought the below answer:

if (Var >0 )
signed
else
un-signed.

If so, your answer is wrong. Because, once more check the question carefully. It is not asking variable value is signed/ signed or negative/ postive. It is asking type's sign.

Example, type 'char' it has both 'sighed char' and 'unsigned char'. How do we know? Thanks to the 2's complement, using which we can find it.

Before saying the way, let me explain that, we cant use function for this: why? because if we use function, the type of the variable is defined, but our purpose is to find for any given type. So, we use Macro for it.

If we use macro, we can find the sign type, in two cases:

If the argument is value: (thanks to 2's complement)
#define IS_SIGNED(__VAR__) ( ( __VAR__ > 0 ) && ( ~__VAR__ > 0 ) )

If the argument is type: (thanks to typecasting)
#define IS_SIGNED(__TYPE__) ( (__TYPE__)0 - 1 > 0 )

PS: This question is asked in a interivew for recruiting to Microsoft.

Thursday, December 22, 2005

Binary number !!

We can use "int v = 0xFF;" to use hexadecimal numbers.
We can use still other base numbers, but..., how to use binary numbers in ANSI compiler??

Tuesday, December 20, 2005

solve this

if(X)
{
printf("Hello");
}
else
{
printf("World");
}

Question: What should be the value of "X" so that the output is HelloWorld?

Sunday, December 18, 2005

MACRO

What does the message ``warning: macro replacement within a string literal'' mean?

Some pre-ANSI compilers/preprocessors interpreted macro definitions like

#define TRACE(var, fmt) printf("TRACE: var = fmt\n", var)

such that invocations like

TRACE(i, %d);


were expanded as

printf("TRACE: i = %d\n", i);


In other words, macro parameters were expanded even inside string literals and character constants.

Macro expansion is not defined in this way by K&R or by Standard C. When you do want to turn macro arguments into strings, you can use the new # preprocessing operator, along with string literal concatenation (another new ANSI feature):

#define TRACE(var, fmt) printf("TRACE: " #var " = " #fmt "\n", var)


What's the best way to write a multi-statement macro?

ANSWER:
#define MACRO(arg1, arg2) do { \
/* declarations */ \
stmt1; \
stmt2; \
/* ... */ \
} while(0) /* (no trailing ; ) */


How can I write a macro which takes a variable number of arguments?
One popular trick is to define and invoke the macro with a single, parenthesized ``argument'' which in the macro expansion becomes the entire argument list, parentheses and all, for a function such as printf:

#define DEBUG(args) (printf("DEBUG: "), printf args)

if(n != 0) DEBUG(("n is %d\n", n));

The obvious disadvantage is that the caller must always remember to use the extra parentheses.

gcc has an extension which allows a function-like macro to accept a variable number of arguments, but it's not standard. Other possible solutions are to use different macros (DEBUG1, DEBUG2, etc.) depending on the number of arguments, to play games with commas:

#define DEBUG(args) (printf("DEBUG: "), printf(args))
#define _ ,

DEBUG("i = %d" _ i)

Thursday, December 08, 2005

sizeof() Vs strlen()

In C language Difference between sizeof() and strlen()

Consider the following example:

/* Example for sizeof( ) , strlen( ) */
#include 
main()
{
       char String[]="Hello";

       printf("\n SIZE OF String %d STRING LENGTH %d", 
       sizeof( String ), strlen( String ) );
}

Result:
SIZE OF String 6 STRING LENGTH 5.

Every String contains a NULL character( '\0' ) at the end. The sizeof() function will include that NULL character also for calculating string size but strlen() function not.

Why is sizeof('a') not 1?
Perhaps surprisingly, character constants in C are of type int, so sizeof('a') is sizeof(int) (though it's different in C++).

Result:
In Turbo C output is: 2
In Turbo C++ output is: 1

Sunday, December 04, 2005

Volatile Pointer

Volatile is a variable which may change the valuewithout knowing of code.
A variable should be declared volatile whenever its value could change unexpectedly.

In practice, only three types of variables could change:
Memory-mapped peripheral registers
Global variables modified by an interrupt service routine
Global variables within a multi-threaded application

It is also used to avoid the compiler optimisation.

consider the below code,
volatile int *vp=0x5565;
.
.
.
.

code


while ( *vp!= 0 )
{
..code
}
In the above case, the vp pointer value may change during the run time.
To ensure that the volatile keyword is used.

so that the value of vp will not read often by compiler.
so the optimisation of the compiler will stop.
But in the ordinary pointer it is not done.

Thursday, December 01, 2005

How will get 0x12 from 0x1234?

Just do Right Shift 8 times. Thats it.

But Normally we say that & with 0xFF00 and do right shift 8 times. Please remember to think before answering what is the question and the possibilities to reduce instructions/ execution time/ memory consumptioin.

Simple Example, If we want to multiply a value by 2, we put

"value * 2"

But efficient way would be

"Value << 1"

That is left shift one time will give multiples of two. This will reduce the CPU cycles consumed.

Left Shift is faster than Multiplication

What is the Size of Class having a "int" variable and a inline function?

The size of that class is only the size of that int variable. sizeof dont consider the size of Inline functions.


Example:

Class A
{
int IntVariable;
void PrintContent( void )
{
cout << endl << IntVariable << endl;
};
}


Consider size of the word is 4 bytes. So, sizeof( A ) will return 4 bytes only.