0% found this document useful (0 votes)
2 views

derived data types program

The document provides examples of basic C programming concepts including arrays, structures, unions, pointers, and functions. Each example demonstrates how to declare and use these constructs, along with explanations of their functionality. The examples illustrate how to store and manipulate data in different ways in C.

Uploaded by

yesudossj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

derived data types program

The document provides examples of basic C programming concepts including arrays, structures, unions, pointers, and functions. Each example demonstrates how to declare and use these constructs, along with explanations of their functionality. The examples illustrate how to store and manipulate data in different ways in C.

Uploaded by

yesudossj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

1.

Array Example

#include <stdio.h>

int main() {
int numbers[5] = {10, 20, 30, 40, 50};

printf("Array elements:\n");
for(int i = 0; i < 5; i++) {
printf("%d ", numbers[i]);
}

return 0;
}

🧾 Explanation:
numbers is an array that stores 5 integers. We print them using a loop.

✅ 2. Structure Example

#include <stdio.h>

struct Student {
int id;
char name[20];
};

int main() {
struct Student s1 = {101, "John"};

printf("ID: %d\n", s1.id);


printf("Name: %s\n", s1.name);

return 0;
}

🧾 Explanation:
A Student structure is created with id and name. Then we access and print its values.

✅ 3. Union Example

#include <stdio.h>

union Data {
int i;
float f;
};

int main() {
union Data data;
data.i = 10;
printf("Integer: %d\n", data.i);

data.f = 3.14;
printf("Float: %.2f\n", data.f);

return 0;
}

🧾 Explanation:
union allows only one member to hold a value at a time. data.i is overwritten by data.f.

✅ 4. Pointer Example

#include <stdio.h>

int main() {
int num = 25;
int *ptr = &num;

printf("Value of num: %d\n", num);


printf("Address of num: %p\n", &num);
printf("Value using pointer: %d\n", *ptr);

return 0;
}

5.Function

#include <stdio.h>

// Function declaration
int add(int a, int b);

int main() {
int num1, num2, result;

printf("Enter two numbers: ");


scanf("%d %d", &num1, &num2);

// Function call
result = add(num1, num2);

printf("Sum = %d\n", result);


return 0;
}

// Function definition
int add(int a, int b) {
return a + b;
}

You might also like