C Functions
A function in C is a block of code that performs a specific task. Functions help organize code, making it reusable and easier to read. Here’s a simple structure of a function:
return_type function_name(parameters) {
// function body
return value; // optional
}
Example:
#include <stdio.h>
int add(int a, int b) {
return a + b;
}
int main() {
int result = add(5, 3);
printf("Result: %d\n", result);
return 0;
}
C Data Types
C has several built-in data types, which can be categorized as follows:
-
Basic Data Types:
int: Integer type (e.g.,int a = 5;)float: Floating-point type (e.g.,float b = 5.5;)double: Double precision floating-point type (e.g.,double c = 10.99;)char: Character type (e.g.,char d = 'A';)
-
Derived Data Types:
- Arrays: Collection of elements of the same type (e.g.,
int arr[5];) - Pointers: Variable that stores the address of another variable (e.g.,
int *ptr;)
- Arrays: Collection of elements of the same type (e.g.,
-
User-defined Data Types:
struct: A structure that groups different data types (e.g.,struct Person { char name[50]; int age; };)union: Similar to struct but shares memory for its members.
Summary
Functions in C allow you to encapsulate code for specific tasks, while data types define the kind of data you can work with. Understanding these concepts is fundamental to programming in C.
If you want to explore more, consider checking out LabEx labs on functions and data types!
