Skip to main content

Featured

Mastering Hollow Square Patterns in C: Stars, Numbers, Alphabets & Binary

๐Ÿ”ข C Program to Print Hollow Continuous Number Square ๐Ÿ“„ Source Code: #include <stdio.h> int main() { int num, k = 0; printf("Enter the number:\n"); scanf("%d", &num); for(int i = 1; i <= num; i++) { for(int j = 1; j <= num; j++) { if(i == 1 || i == num || j == 1 || j == num) { // k increments sequentially only along the borders printf("%d ", k++); } else { printf(" "); } } printf("\n"); } return 0; } ๐Ÿ“‹ Copy Code ๐Ÿ’ป Expected Output (Input: 5): Enter the number: 5 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 ๐Ÿ”ข C Program to Print Standard Hollow Binary Row Square ๐Ÿ“„ Source Code (Fixed Specifier): #include <stdio.h> int main() { ...

Top 5 Examples of volatile in C

Top 5 Examples of volatile in C

// Example 1: GPIO Register
#define GPIO_PORT (*(volatile unsigned int*)0x40021000)
while ((GPIO_PORT & 0x01) == 0) { }

// Example 2: Interrupt Flag
volatile int interrupt_flag = 0;
void ISR() { interrupt_flag = 1; }

// Example 3: Watchdog Reset
volatile int system_reset = 0;
while (!system_reset) { }

// Example 4: Multi-thread Flag
volatile int data_ready = 0;
while (!data_ready) { }

// Example 5: Sensor Polling
#define SENSOR_STATUS_REG (*(volatile unsigned char*)0x40024000)
while ((SENSOR_STATUS_REG & 0x01) == 0) { }

Use volatile whenever a variable might change unexpectedly due to hardware, interrupts, or concurrency. This prevents compiler optimizations that can cause bugs in real-time systems.

Comments

Popular Posts

๐ŸŒ™