C Programming Notes
Pointers in C
Definition:
A pointer is a variable that stores the memory address of another variable.
Syntax:
data_type *pointer_name;
Example:
#include <stdio.h>
int main() {
int a = 10;
int *p = &a;
printf("Value of a: %d\n", a);
printf("Address of a: %p\n", p);
printf("Value pointed to by p: %d\n", *p);
return 0;
Structure in C
Definition:
A structure is a user-defined data type that allows grouping variables of different types.
C Programming Notes
Syntax:
struct StructureName {
data_type member1;
data_type member2;
};
Example:
#include <stdio.h>
struct Student {
int id;
char name[20];
float marks;
};
int main() {
struct Student s1 = {101, "Alice", 89.5};
printf("ID: %d\nName: %s\nMarks: %.2f\n", s1.id, s1.name, s1.marks);
return 0;
Union in C
Definition:
C Programming Notes
A union is a user-defined data type where all members share the same memory location.
Syntax:
union UnionName {
data_type member1;
data_type member2;
};
Example:
#include <stdio.h>
union Data {
int i;
float f;
};
int main() {
union Data d;
d.i = 10;
printf("d.i = %d\n", d.i);
d.f = 3.14;
printf("d.f = %.2f\n", d.f);
printf("d.i after assigning f = %d\n", d.i);
return 0;
}
C Programming Notes
Self-referential Structure
Definition:
A self-referential structure is a structure that contains a pointer to a structure of the same type.
Syntax:
struct Node {
int data;
struct Node *next;
};
Example:
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node *next;
};
int main() {
struct Node *head = NULL;
head = (struct Node*) malloc(sizeof(struct Node));
head->data = 10;
head->next = NULL;
C Programming Notes
printf("Data = %d\n", head->data);
return 0;
typedef in C
Definition:
The typedef keyword gives a new name (alias) to existing data types.
Syntax:
typedef existing_type new_name;
Example:
#include <stdio.h>
typedef unsigned int u_int;
int main() {
u_int a = 10;
printf("a = %u\n", a);
return 0;
enum in C
Definition:
C Programming Notes
An enumeration is a user-defined type consisting of named integer constants.
Syntax:
enum EnumName {CONST1, CONST2};
Example:
#include <stdio.h>
enum Week {Mon, Tue, Wed, Thu, Fri, Sat, Sun};
int main() {
enum Week today = Wed;
printf("Day number = %d\n", today);
return 0;
Structure vs Union
Structure vs Union:
Structure:
- Allocates separate memory for each member
- All members can be accessed at the same time
- Size = sum of sizes of all members (with padding)
Union:
C Programming Notes
- All members share the same memory location
- Only one member can be accessed at a time
- Size = size of the largest member
Example:
#include <stdio.h>
struct MyStruct {
int i;
float f;
};
union MyUnion {
int i;
float f;
};
int main() {
struct MyStruct s = {10, 3.14};
union MyUnion u;
u.i = 10;
printf("Union i: %d\n", u.i);
u.f = 3.14;
printf("Union f: %.2f\n", u.f);
C Programming Notes
printf("Union i after f assignment: %d\n", u.i);
printf("\nStruct i: %d\nStruct f: %.2f\n", s.i, s.f);
printf("\nSize of Struct: %lu bytes\n", sizeof(s));
printf("Size of Union: %lu bytes\n", sizeof(u));
return 0;