Skip to main content

Featured

C Program to Check Prime Number Using Efficient Logic

  Introduction A prime number is a number that has exactly two distinct positive divisors: 1 and itself. In this program, we check whether a given number is prime or not using a simple and efficient logic. This type of program is commonly used in mathematics, competitive programming, and basic algorithm learning for beginners in C programming. Problem Statement The task is to write a C program that determines whether a given integer is a prime number or not. The program takes a single integer input from the user and analyzes its divisibility. If the number has no divisors other than 1 and itself, it should be identified as a prime number; otherwise, it is not prime. This problem is important in number theory and has practical relevance in areas such as cryptography, data validation, and algorithm design.  Algorithm / Logic Explanation To check whether a number is prime, we need to verify that it is not divisible by any number other than 1 and itself. The algorithm follows a si...

Typedef in C - Real World Examples

Typedef in C - Real World Examples

✅ Typedef in C - Real World Examples

๐Ÿ”น Typedef with Pointers

#include <stdio.h>

typedef int* IntPtr;

int main() {
    int a = 100;
    IntPtr p = &a;
    printf("Value at pointer: %d\n", *p);
    return 0;
}
  

๐Ÿ”น Typedef with Function Pointer

#include <stdio.h>

typedef void (*FuncPtr)(int);

void greet(int x) {
    printf("Hello %d times!\n", x);
}

int main() {
    FuncPtr fp = greet;
    fp(3);
    return 0;
}
  

๐Ÿ”น Typedef with Enum

#include <stdio.h>

typedef enum {
    RED, GREEN, BLUE
} Color;

int main() {
    Color c = GREEN;
    printf("Color value: %d\n", c);
    return 0;
}
  

๐Ÿ”น Typedef with Struct

#include <stdio.h>

typedef struct {
    int x, y;
} Point;

int main() {
    Point p1 = {10, 20};
    printf("x = %d, y = %d\n", p1.x, p1.y);
    return 0;
}
  

๐Ÿ”น Typedef with Unsigned Int

#include <stdio.h>

typedef unsigned int unit;

int main() {
    unit a = 0;
    printf("The value of a is: %u", a);
}
  

๐Ÿง  Explanation:

  • typedef allows creating aliases for existing data types.
  • Improves readability especially for complex declarations like function pointers or structs.
  • Helpful in embedded systems, large codebases, and API headers.

๐Ÿ–ฅ️ Sample Output:

Value at pointer: 100
Hello 3 times!
Color value: 1
x = 10, y = 20
The value of a is: 0
  

๐Ÿ”‘ Keywords:

typedef in C, typedef with pointers, typedef with struct, typedef with enum, function pointer typedef, C programming tutorials, embedded C typedef

Comments

Popular Posts

๐ŸŒ™