Strings in C are arrays of characters that end with a special null character ('\0'). You can determine the length of a string using the strlen() function.
#include <string.h> char str[] = "Hello"; int length = strlen(str); /* length is 5 */
You can access individual characters in a string using indices, similar to how you access elements in an array.
char str[] = "Hello"; char firstChar = str[0]; /* firstChar is 'H' */
Strings are created by initializing an array of characters with the desired text.
char str[] = "Hello, World!";
Every string in C ends with a null character ('\0') to mark the end of the string.
char str[] = "Hello"; /* Internally stored as 'H', 'e', 'l', 'l', 'o', '\0' */
The strlen() function calculates the number of characters in a string, not including the null character.
char str[] = "Hello"; int length = strlen(str); /* length is 5 */
To join two strings together, you can use the strcat() function.
char str1[50] = "Hello, "; char str2[] = "World!"; strcat(str1, str2); /* str1 is now "Hello, World!" */
To copy one string into another, use the strcpy() function. The destination must have enough space to hold the copied string.
char src[] = "Hello"; char dest[50]; strcpy(dest, src); /* dest is "Hello" */
An array is a collection of elements of the same type stored in contiguous memory locations. For example, an array of integers stores multiple integers.
int numbers[5] = {1, 2, 3, 4, 5}; /* Access first element: numbers[0] = 1 */
You can create an array without initializing it. The size must be specified, but the elements are not assigned initial values.
int uninitializedArray[10]; /* Array with 10 uninitialized elements */
You can create an array with specific values from the start.
int initializedArray[3] = {1, 2, 3}; /* Array with elements 1, 2, and 3 */
To access an element in an array, use its index, starting from 0 for the first element.
int numbers[3] = {10, 20, 30}; int firstNumber = numbers[0]; /* firstNumber is 10 */
The first element of an array is at index 0, and the last element is at index size-1.
int numbers[3] = {10, 20, 30}; int firstNumber = numbers[0]; /* 10 */ int lastNumber = numbers[2]; /* 30 */
To determine the size of an array, use the sizeof() function. This gives you the total number of bytes, so divide by the size of an individual element to get the number of elements.
int numbers[5]; int size = sizeof(numbers) / sizeof(numbers[0]); /* size is 5 */
You can loop through an array using while loops or for loops to process each element.
int numbers[3] = {10, 20, 30}; for (int i = 0; i < 3; i++) { printf("%d\n", numbers[i]); }
Accessing an element outside the array's bounds can cause unexpected behavior or crashes. Always ensure your indices are within the valid range.
int numbers[3] = {1, 2, 3}; int outOfBounds = numbers[5]; /* Invalid access */
Multidimensional arrays can be thought of as arrays of arrays. You can create them with specified sizes and initial values.
int matrix[2][3] = { {1, 2, 3}, {4, 5, 6} }; /* 2x3 matrix */
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!