kristofer revised this gist . Go to revision
1 file changed, 249 insertions
c_intro.md(file created)
| @@ -0,0 +1,249 @@ | |||
| 1 | + | # Introduction to C for Java and Python Programmers | |
| 2 | + | ||
| 3 | + | As a Java or Python programmer, you'll find C both familiar and different. C is a procedural language that directly influenced both Java and Python, but it's lower-level, giving you more direct control over memory and hardware. Here's an introduction to help you bridge the gap. | |
| 4 | + | ||
| 5 | + | ## Core Characteristics of C | |
| 6 | + | ||
| 7 | + | C is a compiled, statically-typed language with manual memory management. Unlike Java's JVM or Python's interpreter, C code compiles directly to machine code for your specific hardware. There's no garbage collection - you allocate and free memory yourself. | |
| 8 | + | ||
| 9 | + | ## Basic Syntax and Data Types | |
| 10 | + | ||
| 11 | + | C syntax will look familiar with some key differences: | |
| 12 | + | ```c | |
| 13 | + | #include <stdio.h> // Header files use angle brackets | |
| 14 | + | ||
| 15 | + | int main() { // Every C program needs a main function | |
| 16 | + | // Basic data types | |
| 17 | + | int number = 42; // Integer | |
| 18 | + | float decimal = 3.14; // Single-precision floating point | |
| 19 | + | double precise = 3.14159; // Double-precision floating point | |
| 20 | + | char letter = 'A'; // Single character | |
| 21 | + | ||
| 22 | + | // C has no built-in boolean type (before C99) | |
| 23 | + | // Traditionally 0 is false, anything else is true | |
| 24 | + | int is_valid = 1; // true | |
| 25 | + | ||
| 26 | + | // Output | |
| 27 | + | printf("Number: %d, Decimal: %f\n", number, decimal); | |
| 28 | + | ||
| 29 | + | return 0; // Return value from main indicates program status | |
| 30 | + | } | |
| 31 | + | ``` | |
| 32 | + | ||
| 33 | + | ## Control Structures | |
| 34 | + | ||
| 35 | + | Control structures are similar to Java: | |
| 36 | + | ```c | |
| 37 | + | int value = 10; | |
| 38 | + | ||
| 39 | + | // If-else statement | |
| 40 | + | if (value > 5) { | |
| 41 | + | printf("Value is greater than 5\n"); | |
| 42 | + | } else if (value == 5) { | |
| 43 | + | printf("Value is equal to 5\n"); | |
| 44 | + | } else { | |
| 45 | + | printf("Value is less than 5\n"); | |
| 46 | + | } | |
| 47 | + | ||
| 48 | + | // While loop | |
| 49 | + | int counter = 0; | |
| 50 | + | while (counter < 5) { | |
| 51 | + | printf("Counter: %d\n", counter); | |
| 52 | + | counter++; | |
| 53 | + | } | |
| 54 | + | ||
| 55 | + | // For loop | |
| 56 | + | for (int i = 0; i < 5; i++) { | |
| 57 | + | printf("Index: %d\n", i); | |
| 58 | + | } | |
| 59 | + | ||
| 60 | + | // Switch statement | |
| 61 | + | switch (value) { | |
| 62 | + | case 5: | |
| 63 | + | printf("Value is 5\n"); | |
| 64 | + | break; | |
| 65 | + | case 10: | |
| 66 | + | printf("Value is 10\n"); | |
| 67 | + | break; | |
| 68 | + | default: | |
| 69 | + | printf("Value is something else\n"); | |
| 70 | + | } | |
| 71 | + | ``` | |
| 72 | + | ||
| 73 | + | ## Functions | |
| 74 | + | ||
| 75 | + | Functions in C are straightforward but require explicit type declarations: | |
| 76 | + | ```c | |
| 77 | + | // Function declaration (prototype) | |
| 78 | + | int add(int a, int b); | |
| 79 | + | ||
| 80 | + | int main() { | |
| 81 | + | int result = add(5, 3); | |
| 82 | + | printf("5 + 3 = %d\n", result); | |
| 83 | + | return 0; | |
| 84 | + | } | |
| 85 | + | ||
| 86 | + | // Function definition | |
| 87 | + | int add(int a, int b) { | |
| 88 | + | return a + b; | |
| 89 | + | } | |
| 90 | + | ``` | |
| 91 | + | ||
| 92 | + | ## Arrays | |
| 93 | + | ||
| 94 | + | Arrays in C are fixed-size and zero-indexed: | |
| 95 | + | ```c | |
| 96 | + | // Array declaration and initialization | |
| 97 | + | int numbers[5] = {1, 2, 3, 4, 5}; | |
| 98 | + | ||
| 99 | + | // Accessing elements | |
| 100 | + | printf("Third element: %d\n", numbers[2]); // Prints 3 | |
| 101 | + | ||
| 102 | + | // Array without size (compiler calculates) | |
| 103 | + | char name[] = "Hello"; // Creates a 6-element array (including null terminator) | |
| 104 | + | ||
| 105 | + | // Multi-dimensional arrays | |
| 106 | + | int matrix[3][3] = { | |
| 107 | + | {1, 2, 3}, | |
| 108 | + | {4, 5, 6}, | |
| 109 | + | {7, 8, 9} | |
| 110 | + | }; | |
| 111 | + | ||
| 112 | + | printf("matrix[1][2] = %d\n", matrix[1][2]); // Prints 6 | |
| 113 | + | ``` | |
| 114 | + | ||
| 115 | + | ## Strings | |
| 116 | + | ||
| 117 | + | Unlike Java and Python, C has no built-in string type. Strings are arrays of characters terminated by a null character (`\0`): | |
| 118 | + | ```c | |
| 119 | + | // String declaration | |
| 120 | + | char greeting[] = "Hello, World!"; // Compiler adds the null terminator | |
| 121 | + | ||
| 122 | + | // String functions require the string.h header | |
| 123 | + | #include <string.h> | |
| 124 | + | ||
| 125 | + | int main() { | |
| 126 | + | char str1[50] = "Hello"; | |
| 127 | + | char str2[] = ", World!"; | |
| 128 | + | ||
| 129 | + | // String length | |
| 130 | + | printf("Length: %lu\n", strlen(str1)); // 5 | |
| 131 | + | ||
| 132 | + | // String concatenation | |
| 133 | + | strcat(str1, str2); | |
| 134 | + | printf("Concatenated: %s\n", str1); // "Hello, World!" | |
| 135 | + | ||
| 136 | + | // String comparison | |
| 137 | + | if (strcmp(str1, "Hello, World!") == 0) { | |
| 138 | + | printf("Strings are equal\n"); | |
| 139 | + | } | |
| 140 | + | ||
| 141 | + | return 0; | |
| 142 | + | } | |
| 143 | + | ``` | |
| 144 | + | ||
| 145 | + | ## Structs | |
| 146 | + | ||
| 147 | + | Structs let you create custom data types by grouping related variables: | |
| 148 | + | ```c | |
| 149 | + | // Struct definition | |
| 150 | + | struct Person { | |
| 151 | + | char name[50]; | |
| 152 | + | int age; | |
| 153 | + | float height; | |
| 154 | + | }; | |
| 155 | + | ||
| 156 | + | int main() { | |
| 157 | + | // Creating a struct variable | |
| 158 | + | struct Person person1; | |
| 159 | + | ||
| 160 | + | // Assigning values | |
| 161 | + | strcpy(person1.name, "Alice"); | |
| 162 | + | person1.age = 30; | |
| 163 | + | person1.height = 1.75; | |
| 164 | + | ||
| 165 | + | // Alternative initialization syntax | |
| 166 | + | struct Person person2 = {"Bob", 25, 1.82}; | |
| 167 | + | ||
| 168 | + | // Accessing struct members | |
| 169 | + | printf("Name: %s, Age: %d, Height: %.2f\n", | |
| 170 | + | person1.name, person1.age, person1.height); | |
| 171 | + | ||
| 172 | + | return 0; | |
| 173 | + | } | |
| 174 | + | ``` | |
| 175 | + | ||
| 176 | + | ## Pointers | |
| 177 | + | ||
| 178 | + | Pointers are perhaps the most distinctive feature of C. They store memory addresses: | |
| 179 | + | ```c | |
| 180 | + | int main() { | |
| 181 | + | int x = 10; | |
| 182 | + | int *ptr = &x; // ptr stores the address of x | |
| 183 | + | ||
| 184 | + | printf("Value of x: %d\n", x); // 10 | |
| 185 | + | printf("Address of x: %p\n", &x); // Memory address | |
| 186 | + | printf("Value of ptr: %p\n", ptr); // Same memory address | |
| 187 | + | printf("Value at *ptr: %d\n", *ptr); // 10 (dereferencing) | |
| 188 | + | ||
| 189 | + | // Modifying value through pointer | |
| 190 | + | *ptr = 20; | |
| 191 | + | printf("New value of x: %d\n", x); // 20 | |
| 192 | + | ||
| 193 | + | return 0; | |
| 194 | + | } | |
| 195 | + | ``` | |
| 196 | + | ||
| 197 | + | Pointers are essential for: | |
| 198 | + | 1. Dynamic memory allocation | |
| 199 | + | 2. Passing large data structures efficiently | |
| 200 | + | 3. Creating complex data structures like linked lists | |
| 201 | + | ||
| 202 | + | ## Dynamic Memory Allocation | |
| 203 | + | ||
| 204 | + | Unlike Java and Python, C requires manual memory management: | |
| 205 | + | ```c | |
| 206 | + | #include <stdlib.h> // Required for malloc and free | |
| 207 | + | ||
| 208 | + | int main() { | |
| 209 | + | // Allocate memory for an integer | |
| 210 | + | int *ptr = (int *)malloc(sizeof(int)); | |
| 211 | + | ||
| 212 | + | // Check if allocation was successful | |
| 213 | + | if (ptr == NULL) { | |
| 214 | + | printf("Memory allocation failed\n"); | |
| 215 | + | return 1; | |
| 216 | + | } | |
| 217 | + | ||
| 218 | + | // Use the allocated memory | |
| 219 | + | *ptr = 42; | |
| 220 | + | printf("Value: %d\n", *ptr); | |
| 221 | + | ||
| 222 | + | // Free the memory when done | |
| 223 | + | free(ptr); | |
| 224 | + | ||
| 225 | + | // Allocate an array of integers | |
| 226 | + | int *array = (int *)malloc(5 * sizeof(int)); | |
| 227 | + | ||
| 228 | + | if (array != NULL) { | |
| 229 | + | for (int i = 0; i < 5; i++) { | |
| 230 | + | array[i] = i * 10; | |
| 231 | + | printf("array[%d] = %d\n", i, array[i]); | |
| 232 | + | } | |
| 233 | + | free(array); | |
| 234 | + | } | |
| 235 | + | ||
| 236 | + | return 0; | |
| 237 | + | } | |
| 238 | + | ``` | |
| 239 | + | ||
| 240 | + | ## Key Differences to Remember | |
| 241 | + | ||
| 242 | + | 1. **Manual Memory Management**: Unlike Java and Python, C has no garbage collection. You must free any memory you allocate. | |
| 243 | + | 2. **No Objects or Classes**: C is procedural, not object-oriented. | |
| 244 | + | 3. **No Exception Handling**: C uses return values to indicate errors. | |
| 245 | + | 4. **Pointers**: Direct memory manipulation is both C's power and danger. | |
| 246 | + | 5. **No Built-in Data Structures**: No lists, dictionaries, or sets; you build these yourself. | |
| 247 | + | ||
| 248 | + | C is powerful because it's close to the hardware, but this requires more attention to detail. Start with small programs, and always check your code for memory leaks and pointer errors. | |
| 249 | + | ||
Newer
Older