Language in C اسئلة واجوبة المقابلات
Question: How can I write a general-purpose swap without using templates?
Answer: Given below is the program which uses the stringizing preprocessor directive ## for building a general purpose swap macro which can swap two integers, two floats, two chars, etc.#define swap( a, b, t ) ( g ## t = ( a ), ( a ) = ( b ), ( b ) = g ## t ) int gint; char gchar; float gfloat ; main( ) { int a = 10, b = 20 ; char ch1 = 'a' , ch2 = 'b' ; float f1 = 1.12, f2 = 3.14 ; swap ( a, b, int ) ; printf ( "na = %d b = %d", a, b ) ; swap ( ch1, ch2, char ) ; printf ( "nch1 = %c ch2 = %c", ch1, ch2 ) ; swap ( f1, f2, float ) ; printf ( "nf1 = %4.2f f2 = %4.2f", f1, f2 ) ; } swap ( a, b, int ) would expand to, ( gint = ( a ), ( a ) = ( b ), ( b ) = gint ) |
احفظ للمراجعة
احفظ هذا العنصر في الإشارات المرجعية، او حدده كصعب، او ضعه في مجموعة مراجعة.
سجل الدخول لحفظ الإشارات المرجعية والاسئلة الصعبة ومجموعات المراجعة.
هل هذا مفيد؟ نعم لا
الاكثر فائدة حسب تقييم المستخدمين:
- What will be the output of the following code?
void main ()
{ int i = 0 , a[3] ;
a[i] = i++;
printf ("%d",a[i]) ;
} - Why doesn't the following code give the desired result?
int x = 3000, y = 2000 ;
long int z = x * y ; - Why doesn't the following statement work?
char str[ ] = "Hello" ;
strcat ( str, '!' ) ; - How do I know how many elements an array can hold?
- How do I compare character data stored at two different memory locations?