Showing posts with label Macro. Show all posts
Showing posts with label Macro. Show all posts

C Program To Find Product Of Two No Using MACRO

How to write a C Program To Find Product Of Two No Using MACRO in C Programming Language ?


Solution For C Program :

/*C Program To Find Product Of Two No Using MACRO.*/

#include<stdio.h>
#include<conio.h>
#define prod(t,r) t*r
void main()
{
int a,b,c;
printf("enter two no. for finding product\n");
scanf("%d%d",&a,&b);
c=prod(a,b);
printf("%d",c);
getch();
}

You may also learn these C Program/Code :

C Program To Swap Two Numbers Without Using Third Variable

C Program Bit manipulation macros Example

How to write a C Program Bit manipulation macros in C Programming Language ?


  1. //Bit manipulation macros
  2. #define BIT_SET(value,bitno)    ( (value) |= (1<<(bitno)))
  3. #define BIT_CLEAR(value,bitno)  ( (value) &= ~(1<<(bitno)))
  4.  
  5. #include <avr/io.h>
  6. #include <stdio.h>
  7. #include "USART.h"
  8. #include <util/delay.h>
  9. #include "ADC.h"
  10.  
  11. FILE fpUSART = FDEV_SETUP_STREAM(USARTSendByte, USARTGetByte, _FDEV_SETUP_RW);
  12.  
  13. // Sets up Timer 1 for fast-PWM operation
  14. // DC-Motor Connected to PB0 and OSC1A (PB1)
  15. void init_PWM_OSC1A()
  16. {
  17.         TCCR1A = (1<<COM1A1) | (1<<WGM11);     // Clear on compare, fast PWM, TOP=ICR1 (WGM13/WGM12 in TCCR1B)
  18.         TCCR1B = (1<<WGM12)  | (1<<WGM13) ;                   // CTC-mode width ICR1 defines the TOP'value
  19.         TCCR1B |=(1<< CS11)  | (1<<CS10);                     // Prescaler 64
  20.  
  21.         ICR1 = 5000;    //- Input Capture Register In PWM mode this register defines the TOP period
  22.         OCR1A = 0;      // Start width no pulse
  23. }
  24.  
  25. void rotateClockwise(int Speed)
  26. {
  27.         //Clear OC1A on Compare Match,
  28.         //set OC1A at BOTTOM (non-inverting mode)
  29.         BIT_CLEAR(TCCR1A,COM1A0);
  30.         BIT_SET(TCCR1A,COM1A1);
  31.  
  32.         BIT_CLEAR(PORTB,PB0);  //Clear bit 0 PORTB (PB0)
  33.         OCR1A=Speed;
  34. }
  35.  
  36. void rotateCounterClockwise(int Speed)
  37. {
  38.         //Clear OC1A on Compare Match,
  39.         //set OC1A at BOTTOM (non-inverting mode)
  40.         BIT_CLEAR(TCCR1A,COM1A0);
  41.         BIT_SET(TCCR1A,COM1A1);
  42.  
  43.         BIT_SET(PORTB,PB0);  //Clear bit 0 PORTB (PB0)
  44.         OCR1A=5000-Speed;
  45. }
  46.  
  47. // A DC Motor is connected to PB0 and OSC1A (PB1)
  48. // DIGITAL-8 and DIGITAL-9 on the Arduino Uno Board
  49. // The program rotates the motor slowly clockwise
  50. int main(void)
  51. {
  52.         int ADCverdi;
  53.         int fart;
  54.         init_PWM_OSC1A();
  55.         init_USART();
  56.         InitADC();
  57.         stdin=&fpUSART;
  58.         stdout=&fpUSART;
  59.         DDRB =  (1 << PB1) | (1<<PB0);
  60.  
  61.         DDRC = (1 << PC0) | (1 << PC2);
  62.         PORTC &= ~(1<<PC2);
  63.         PORTC = (1<<PC0);
  64.  
  65.     while(1)
  66.         {
  67.                 ADCverdi = ReadADC(1);
  68.                 _delay_ms(50);
  69.                 if (ADCverdi < 500)
  70.                 {
  71.                         fart = (500 - ADCverdi)*9;
  72.                         rotateClockwise(fart);
  73.                 }
  74.                 if (ADCverdi > 520)
  75.                 {
  76.                         fart = (ADCverdi - 520)*9;
  77.                         rotateCounterClockwise(fart);
  78.                 }
  79.                 if(ADCverdi > 500 && ADCverdi < 520)
  80.                 {
  81.                         rotateClockwise(0);
  82.                 }
  83.                 printf("%d \n\r", fart);
  84.         }
  85. }

Generic stack in C Program

How to Declares a stack structure and functions generically using a C macro in C Programming Language ?


Solution:


#ifndef __ETS_STACK__
#define __ETS_STACK__

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dbg.h>	// Contains function exitWithMessage

/*

DESCRIPTION:
	Declares a stack structure and functions generically using a C macro.


NORMAL TYPE USAGE:	(int, char, float, ...)
	DEFINE_STACK(float) // Put this in the correct scope (global probably).

	Stack_float stack1;
	Stack_float_init( &stack1 , 10 );	// Initialize with maximum capacity being 10.

	Stack_float_push( &stack1, (float)5);  // Pushes the float value into stack1
	float a = Stack_float_pop( &stack1 );

	printf("%f", a); // Will print 5.0000...

	Stack_float_free( &stack1 );


POINTER TYPE USAGE:	(char*, string, ...)
	typedef char* string
	DEFINE_STACK(string)	// TYPE can't contain a '*' token, so typedef to bypass restriction.

	Stack_string this_is_another_stack;
	Stack_string_init( &this_is_another_stack, 1 ); // Initialize with maximum capacity being 1.

	string test = (string) malloc(5 * sizeof(char)); // If not on heap then BAD things can happen, (but whatever i don't judge).
	test[0] = 'h';					 // (Just don't free a variable on the stack)
	test[1] = 'e';
	test[2] = 'y';
	test[3] = '\0';

	Stack_string_push( &this_is_another_stack, test);
	string temp = Stack_string_pop( &this_is_another_stack);

	printf("%s", temp);		// Prints "hey"

	free(temp);		// REMEMBER to free elements after poping to prevent memory leaks.

	Stack_string_free( &this_is_another_stack );


*/

#define DEFINE_STACK(TYPE) \
	\
typedef struct{	\
	TYPE *array; \
	int top; \
	int capacity; \
}Stack_##TYPE##; \
 \
void Stack_##TYPE##_init(Stack_##TYPE *stack, int capacity){ \
	stack->top=-1; \
	stack->capacity = capacity; \
	stack->array = ( TYPE *)calloc(stack->capacity, sizeof( TYPE )); \
} \
 \
void Stack_##TYPE##_push(Stack_##TYPE *stack, TYPE data){ \
	if(stack->top < stack->capacity)	stack->array[++(stack->top)] = data; \
		else	exitWithMessage("ERROR: too many items for stack."); \
} \
 \
TYPE Stack_##TYPE##_pop(Stack_##TYPE *stack){ \
	if(stack->top >= 0)	{	\
		TYPE var = stack->array[stack->top--]; \
		stack->array[stack->top+1] = 0;	\
		return var; \
		} else	exitWithMessage("ERROR: can't pop, no items in stack."); \
} \
	\
void Stack_##TYPE##_free(Stack_##TYPE *stack){	\
	free(stack->array);	\
}	\


#endif // !__ETS_STACK__