C
Official website
The C programming language does not have an official website maintained by any central authority. Instead, its specifications are maintained and published by standards organizations such as ISO (the International Organization for Standardization) and IEC (the International Electrotechnical Commission).
For example:
-
The current international standard for the C language is ISO/IEC 9899:2018 (often referred to as C18). You can view information about this standard through the ISO website:
ISO/IEC 9899:2018 -
Historically, the ANSI C standard (from the American National Standards Institute) also played a key role in the language’s standardization.
If you’re looking for resources, tutorials, and community information about C, there are many reputable sites (e.g., https://en.cppreference.com/w/c) and communities, but none of these serve as an “official” website for the language itself.
So in summary, while you won’t find a singular “official website of C,” the standards maintained by ISO/IEC can be considered the closest thing to an official reference, and you can find more details by visiting the ISO link provided above.
Static typing
Yes, C is a statically typed language.
This means that the type of every variable is known at compile time, and types must be explicitly declared (with some exceptions via type inference in more modern languages, but not in C). The compiler uses the type information to perform type checking and ensure type safety before the program runs. As a result, many type errors are caught during compilation rather than at runtime, reducing a whole class of potential runtime errors.
Example code
Below is an example of a C algorithm that searches for a value in a binary search tree (BST). This example defines a basic BST structure, a function to search for a value recursively, and a main function that demonstrates how to use the search function.
#include <stdio.h>
#include <stdlib.h>
// Define the structure for a tree node.
typedef struct Node {
int data;
struct Node* left;
struct Node* right;
} Node;
// Helper function to create a new node.
Node* createNode(int data) {
Node* newNode = (Node*)malloc(sizeof(Node));
if (newNode == NULL) {
fprintf(stderr, "Memory allocation error!\n");
exit(EXIT_FAILURE);
}
newNode->data = data;
newNode->left = newNode->right = NULL;
return newNode;
}
// Function to insert a value into the BST.
Node* insert(Node* root, int data) {
// If tree is empty, return a new node.
if (root == NULL) {
return createNode(data);
}
// Otherwise, recur down the tree.
if (data < root->data) {
root->left = insert(root->left, data);
} else if (data > root->data) {
root->right = insert(root->right, data);
}
// Return the unchanged node pointer.
return root;
}
// Recursive function to find a value in the BST.
Node* search(Node* root, int key) {
// Base Cases: root is null or key is present at root.
if (root == NULL) {
return NULL;
}
if (root->data == key) {
return root;
}
// Key is greater than root's data.
if (key < root->data) {
return search(root->left, key);
} else {
return search(root->right, key);
}
}
// Function to free the BST nodes.
void freeTree(Node* root) {
if (root != NULL) {
freeTree(root->left);
freeTree(root->right);
free(root);
}
}
// Main function to demonstrate usage.
int main(void) {
Node* root = NULL;
int values[] = {50, 30, 20, 40, 70, 60, 80};
int n = sizeof(values) / sizeof(values[0]);
// Insert values into the BST.
for (int i = 0; i < n; i++) {
root = insert(root, values[i]);
}
int key = 40;
Node* found = search(root, key);
if (found != NULL) {
printf("Key %d found in the BST.\n", key);
} else {
printf("Key %d not found in the BST.\n", key);
}
// Free memory allocated for the tree.
freeTree(root);
return 0;
}
Explanation
-
Node Structure:
TheNodestructure represents nodes in the BST with an integerdataand pointers to left and right children. -
createNode Function:
This helper function creates a new node with the given data and initializes its children toNULL. -
insert Function:
This function inserts a new value into the BST while maintaining the BST property. It recursively finds the appropriate position for the new node. - search Function:
This is the core function that searches for a given key in the BST:- If the current node is
NULL, it means the key isn’t in the BST. - If the current node’s data matches the key, it returns a pointer to that node.
- Otherwise, it recursively searches in the left or right subtree based on the comparison between the key and the node’s data.
- If the current node is
-
freeTree Function:
This utility function frees the allocated memory for the BST nodes to prevent memory leaks. - Main Function:
Themainfunction demonstrates constructing a BST by inserting a list of values, searching for a particular key, and then printing whether the key was found. Finally, it frees the memory used by the BST.
This complete and self-contained example should help you understand how to search for a value in a binary search tree using C.