Concepts for C
C is a low-level, procedural programming language known for its efficiency, portability, and versatility. C's structured syntax, powerful standard library, and static typing enable developers to write efficient and portable code for a wide range of applications. However, its manual memory management and pointer arithmetic require careful attention to avoid common pitfalls such as memory leaks and segmentation faults. It is important to note that C does not have many of the built in data structures that come with many of the more higher level programming languages. If a particular feature or data structure is not included, it most likely means that it is not support natively or must be built from the ground up to implement it. For example, C does not have built in sets or classes though you are able to build a similar concept to classes using Structs
Print Text
#include <stdio.h> int main() { printf("Hello World!"); return 0; };Comments
// one line comment /* multi line comment */
Variables
int carCount = 2; char name[] = "Joe"; #include <stdbool.h> bool isPurple = true;
Data Types
int carCount = 2; unsigned int age = 15; signed short int seaLevel = -10000; long long int usBudget = 11000000000; unsigned long long int lightSpeed = 11799312874; float myGPA = 3.45f; double piValue = 3.14159265; char lastLetter = 'Z'; char myName[] = "Joe"; #include <stdbool.h> bool isMale = true;
Math Op
int carCount = 6; int truckCount = 3; int total = carCount + truckCount; printf("%d \n", total); int subtract = carCount - truckCount; printf("%d \n", subtract); int multiple = carCount * truckCount; printf("%d \n", multiple); int divisible = carCount / truckCount; printf("%d \n", divisible); // performs division and gets remainder which is 0 int modulo = carCount % truckCount; printf("%d \n", modulo);Math Functions
#include <stdio.h> #include <math.h> #include <stdlib.h> #include <time.h> float power = pow(3.0, 6.0); printf("%f \n", power); float root = sqrt(16.0); printf("%f \n", root); float floorVal = floor(3.14); printf("%f \n", power); float ceilVal = ceil(4.3); printf("%f \n", power); float roundVal = round(3.4); printf("%f \n", roundVal); // random number from 0 to 100 srand(time(NULL)); int ranNum = rand() % 101; printf("%d \n", ranNum);If/Else
int carCount = 6; if (carCount > 3) { printf("More than three cars"); } else if (carCount == 3) { printf("Equal to three cars"); } else { printf("Less than three cars"); }Ternary
int carCount = 3; char* result = (carCount > 2) ? "Yes" : "No"; printf("%s", result);Switch
int carCount = 3; switch (carCount) { case 1: printf("One car"); break; case 2: printf("Two cars"); break; case 3: printf("Three cars"); break; default: printf("More than three"); }Functions
#include <stdio.h> int handleCars(int cars, int wheels) { total = cars * wheels; return total; } int result = handleCars(3, 4); printf("%d", result);Interpolation
char name[] = "Joe"; char car[] = "Mazda"; char result[50]; sprintf(result, "%s drives a %s", name, car); printf(result);
Type Casting
double squareA(int length) { double area = pow(length, 2); return area; } char* squareL = "4"; int convertedVal = atoi(squareL); double area = squareA(convertedVal); // converted and formatted here char result[50]; snprintf(result, 50, "%f", area); printf("%s", result);Date and Time
#include <stdio.h> #include <time.h> // seconds since epoch time_t seconds = time(NULL); printf("%ld \n", seconds); struct tm tm = *localtime(&seconds); // current year int year = tm.tm_year + 1900; printf("%d \n", year); // current month int month = tm.tm_mon + 1; printf("%d \n", month); // formated date char formatted[50]; sprintf(formatted, "%d/%d/%d", month, tm.tm_mday, year); printf("%s", formatted);Classes
#include <stdio.h> #include <stdlib.h> #include <string.h> #define MAX_CARS 100 struct Cars { char* carList[MAX_CARS]; int count; }; void addCar(struct Cars *cars, const char *car) { if (cars->count < MAX_CARS) { cars->carList[cars->count] = strdup(car); cars->count++; } else { printf("Maximum limit reached.\n"); } } void removeFirst(struct Cars *cars) { if (cars->count > 0) { free(cars->carList[0]); for (int i = 1; i < cars->count; i++) { cars->carList[i - 1] = cars->carList[i]; } cars->count--; } else { printf("No cars to remove.\n"); } } const char* getFirstVal(struct Cars *cars) { if (cars->count > 0) { return cars->carList[0]; } return ""; } int main() { struct Cars newList = {{0}, 0}; addCar(&newList, "Honda"); addCar(&newList, "Mazda"); addCar(&newList, "Toyota"); removeFirst(&newList); const char* firstCar = getFirstVal(&newList); printf("%s\n", firstCar); return 0; }Inheritance
// No Native Support or Implementation
Method Overload
// No Native Support or Implementation
Abstract Class
// No Native Support or Implementation
Static Class
// No Native Support or Implementation
Arrays/Lists
char* names[] = {"Joe", "Alex", "Bob"}; names[1] = "Tom"; char* name = names[1]; int numItems = sizeof(names) / sizeof(names[0]); printf("%s\n", name); printf("%d", numItems);Array Methods
// No Built in methods in C arrays
Concatenation
// No Native Support or Implementation
Sort Method
// No Native Support or Implementation
Objects
#include <stdio.h> #include <stdlib.h> #include <string.h> #define MAX_CAR_BRANDS 10 #define MAX_NAME_LENGTH 50 struct User { char First[MAX_NAME_LENGTH]; char Last[MAX_NAME_LENGTH]; int Age; int Retired; char CarBrands[MAX_CAR_BRANDS][MAX_NAME_LENGTH]; int carBrandsCount; }; void setValues( struct User *user, const char *first, const char *last, int age, int retired, const char *carBrands[], int carBrandsCount ) { strncpy(user->First, first, MAX_NAME_LENGTH - 1); strncpy(user->Last, last, MAX_NAME_LENGTH - 1); user->Age = age; user->Retired = retired; user->carBrandsCount = carBrandsCount; for ( int i = 0; i < carBrandsCount && i < MAX_CAR_BRANDS; i++ ) { strncpy( user->CarBrands[i], carBrands[i], MAX_NAME_LENGTH - 1 ); } } char* fullName(struct User *user) { char *fullName = malloc(MAX_NAME_LENGTH * 2); snprintf( fullName, MAX_NAME_LENGTH * 2, "%s %s", user->First, user->Last ); return fullName; } int main() { struct User user; const char *carBrands[] = {"Mazda", "Toyota"}; setValues(&user, "Joe", "Doe", 23, 0, carBrands, 2); printf("%s\n", user.First); printf("%s\n", user.CarBrands[1]); printf("%s\n", fullName(&user)); return 0; }Maps (Key/Value)
// no built in implementation // must be built from scratch
Sets
// no built in implementation
Stack
// no built in implementation
Queues
// no built in implementation
Linked List
// No Native Support or Implementation
Graphs
// No Native Support or Implementation
For Loops
char* names[] = {"Joe", "Alex", "Bob"}; int numItems = sizeof(names) / sizeof(names[0]); for (int i = 0; i < numItems; i++) { printf("%s\n", names[i]); }While Loops
char* carList[] = {"Mazda", "Toyota", "Honda"}; int numItems = sizeof(carList) / sizeof(carList[0]); int i = 0; while(i < numItems) { printf(carList[i]); printf("\n"); i++; }Loop Breaks
char* cars[] = {"Toyota", "Honda", "Mazda"}; int numItems = sizeof(cars) / sizeof(cars[0]); char result[50]; for (int i = 0; i < numItems; i++) { if (cars[i] == "Honda") { sprintf(result, "Found at index %d", i); printf(result); break; } sprintf(result, "Visited index %d\n", i); printf(result); }Recursion
#include <stdio.h> int getSum( int arr[], int index ) { if (index < 0) { return 0; } return arr[index] + getSum(arr, index-1); } int testArr[] = {1, 2, 4, 5}; int sum = getSum(testArr, 3); printf("%d", sum);Regex Words
// no built in implementation
Regex Numbers
// no built in implementation
Regex Test
#include <stdio.h> #include <string.h> #include <stdbool.h> const char *name = "Bob Lee"; const char *search = "Lee"; char *result = strstr(name, search); bool test = result != NULL; printf("%d", test);Regex Characters
// no built in implementation