A function is a block of reusable code that performs a specific task. It may accept inputs and return an output.
#include <stdio.h> int main() { // printf is a standard function provided by C printf("Hello from a function!"); return 0; }
C provides many built-in functions like `printf` for printing text. You can also create your own functions.
int addNumbers(int num1, int num2) { return num1 + num2; } int main() { // Calls the function addNumbers with 5 and 2 as inputs int result = addNumbers(5, 2); printf("Sum: %d", result); return 0; }
To use a function, simply write its name followed by parentheses. If the function needs inputs, include them inside the parentheses.
int multiply(int a, int b) { return a * b; } int main() { // Calls the function multiply with 4 and 3 as inputs int product = multiply(4, 3); printf("Product: %d", product); return 0; }
The result of a function can be stored in a variable for later use. This allows you to use the result in further calculations or output.
int subtract(int a, int b) { return a - b; } int main() { int result = subtract(10, 3); printf("Difference: %d", result); return 0; }
A function signature includes the function's return type, its name, and the types of any parameters it takes. It defines how the function should be used.
void printMessage() { printf("This function has no return value."); } int main() { printMessage(); return 0; }
When a function does not return any value, it uses the keyword `void` as its return type. This means the function performs an action but does not send any result back.
void displayNumber(int number) { printf("Number: %d", number); } int main() { displayNumber(5); return 0; }
Functions can return a value to the caller using the `return` keyword. The type of the returned value must match the function's return type.
int getMaximum(int a, int b) { if (a > b) { return a; } else { return b; } } int main() { int max = getMaximum(10, 20); printf("Maximum: %d", max); return 0; }
Parameters are used to pass data into functions. They are listed inside the parentheses in the function's signature and are used within the function.
int square(int number) { return number * number; } int main() { int result = square(4); printf("Square: %d", result); return 0; }
A function prototype provides a declaration of a function before its actual definition. It tells the compiler what the function's return type and parameters are.
// Prototype int add(int, int); // Definition int add(int a, int b) { return a + b; } int main() { int sum = add(3, 4); printf("Sum: %d", sum); return 0; }
Structures in C allow you to group different types of data together under one name. You use the `struct` keyword to define a structure.
// Define a structure named Person struct Person { char* name; int age; };
You can initialize a structure by listing values for its members in the order they are declared. There are two ways to initialize a structure: with named members or in order.
// Define and initialize a structure named Person struct Person { char* name; int age; }; // Initialize with named members struct Person person1 = {.name = "Alice", .age = 30}; // Initialize in order struct Person person2 = {"Bob", 25};
Structures allow you to create custom data types that group various related data together. This makes it easier to manage complex data.
// Define a structure with different types of data struct Person { char* name; int age; char middleInitial; };
Structures can combine multiple data types into a single unit, unlike arrays that only handle one type of data. This helps to keep related data organized.
// Define a structure with various types struct Person { char* name; int age; char middleInitial; }; // Create and initialize a Person struct Person person1 = {"John", 40, 'D'}; // Access data printf("Name: %s", person1.name);
You can access members of a structure using the dot (.) operator if you have a structure variable, or the arrow (->) operator if you have a pointer to a structure.
// Define a structure struct Person { char* name; int age; }; // Initialize a structure variable struct Person person1 = {"Jane", 28}; // Access and print member variable printf("Name: %s", person1.name);
Pointers to structures let you work with structures indirectly. You can use these pointers to access and modify structure data without directly copying the structure.
// Define a structure struct Person { char* name; int age; }; // Initialize a structure and pointer struct Person person1 = {"Mike", 22}; struct Person* personPointer = &person1 // Access data via pointer printf("Name: %s", personPointer->name);
When working with pointers to structures, use the arrow (->) operator to access structure members. This simplifies accessing data through pointers.
// Define a structure struct Person { char* name; int age; }; // Initialize a structure and pointer struct Person person1 = {"Sara", 35}; struct Person* personPointer = &person1 // Access member via pointer printf("Age: %d", personPointer->age);
You can pass structures to functions as parameters. This allows functions to receive and work with structured data directly.
// Define a structure struct Person { char* name; int age; }; // Function that takes a Person structure void printPerson(struct Person p) { printf("Name: %s, Age: %d", p.name, p.age); } int main() { struct Person person1 = {"Ella", 29}; printPerson(person1); return 0; }
You can also pass pointers to structures to functions. This is often more efficient than passing the entire structure, especially for large structures.
// Define a structure struct Person { char* name; int age; }; // Function that takes a pointer to a Person structure void updateAge(struct Person* p, int newAge) { p->age = newAge; } int main() { struct Person person1 = {"Tom", 40}; updateAge(&person1, 41); printf("Updated Age: %d", person1.age); return 0; }
Welcome to our comprehensive collection of programming language cheatsheets! Whether you're a seasoned developer or a beginner, these quick reference guides provide essential tips and key information for all major languages. They focus on core concepts, commands, and functions—designed to enhance your efficiency and productivity.
ManageEngine Site24x7, a leading IT monitoring and observability platform, is committed to equipping developers and IT professionals with the tools and insights needed to excel in their fields.
Monitor your IT infrastructure effortlessly with Site24x7 and get comprehensive insights and ensure smooth operations with 24/7 monitoring.
Sign up now!