Subject: Logic Development Programming
Prepared By: Prof. Ankit Patel
BCA- SEM-1
UNIT-3 Array & String
Decision making Statements
The conditional statements (also known as decision control structures) such as if, if
else, switch, etc. are used for decision-making purposes in C programs.
1) IF Statement:
The if statement is the most simple decision-making statement. It is used to decide
whether a certain statement or block of statements will be executed or not i.e if a
certain condition is true then a block of statements is executed otherwise not.
Syntax of if Statement:
if(condition)
{
// Statements to execute if
// condition is true
}
Page 1
Subject: Logic Development Programming
Prepared By: Prof. Ankit Patel
BCA- SEM-1
Example- Example of if Statement in C
#include <stdio.h>
int main()
{
int i = 10;
if (i > 15)
{
printf("10 is greater than 15");
}
printf("I am Not in if");
}
OUTPUT:
I am Not in if
2) IF ELSE Statement:
The if statement alone tells us that if a condition is true it will execute a block of
statements and if the condition is false it won’t. But what if we want to do something
else when the condition is false? Here comes the C else statement. We can use the
else statement with the if statement to execute a block of code when the condition is
false. The if-else statement consists of two blocks, one for false expression and one
for true expression.
Syntax of if Statement:
Page 2
Subject: Logic Development Programming
Prepared By: Prof. Ankit Patel
BCA- SEM-1
if (condition)
{
// Executes this block if
// condition is true
}
else
{
// Executes this block if
// condition is false
}
Example- Example of if else Statement in C
#include <stdio.h>
int main()
{
int i = 20;
if (i < 15)
{
printf("i is smaller than 15");
}
else
{
printf("i is greater than 15");
}
return 0;
}
Page 3
Subject: Logic Development Programming
Prepared By: Prof. Ankit Patel
BCA- SEM-1
OUTPUT:
i is greater than 15
3) Nested if-else Statement:
A nested if in C is an if statement that is the target of another if statement. Nested if
statements mean an if statement inside another if statement. Yes, C allow us to nested
if statements within if statements, i.e, we can place an if statement inside another if
statement.
Syntax of if Statement:
if (condition1)
{
// Executes when condition1 is true
if (condition_2)
{
// statement 1
}
else
{
// Statement 2
}
}
else {
if (condition_3)
{
// statement 3
}
else
{
// Statement 4
}
}
Page 4
Subject: Logic Development Programming
Prepared By: Prof. Ankit Patel
BCA- SEM-1
Example- Example of Nested if Statement in C
#include <stdio.h>
int main()
{
int i = 10;
if (i == 10)
{
if (i < 15)
printf("i is smaller than 15\n");
if (i < 12)
printf("i is smaller than 12 too\n");
else
printf("i is greater than 15");
}
else
{
if (i == 20)
{
if (i < 22)
printf("i is smaller than 22 too\n");
else
printf("i is greater than 25");
} }
return 0;
}
Page 5
Subject: Logic Development Programming
Prepared By: Prof. Ankit Patel
BCA- SEM-1
OUTPUT:
i is smaller than 15
i is smaller than 12 too
4) IF-ELSE-IF Statement:
The if else if statements are used when the user has to decide among multiple options.
The C if statements are executed from the top down. As soon as one of the conditions
controlling the if is true, the statement associated with that if is executed, and the rest
of the C else-if ladder is bypassed. If none of the conditions is true, then the final else
statement will be executed. if-else-if ladder is similar to the switch statement.
Syntax of if Statement:
if (condition)
statement;
else if (condition)
statement;
else
statement;
Example- Example of if else if Statement in C
#include <stdio.h>
int main()
{
Page 6
Subject: Logic Development Programming
Prepared By: Prof. Ankit Patel
BCA- SEM-1
int i = 20;
if (i == 10)
printf("i is 10");
else if (i == 15)
printf("i is 15");
else if (i == 20)
printf("i is 20");
else
printf("i is not present");
}
OUTPUT:
i is 20
5) SWITCH Statement:
The switch case statement is an alternative to the if else if ladder that can be used to
execute the conditional code based on the value of the variable specified in the switch
statement. The switch block consists of cases to be executed based on the value of the
switch variable.
Syntax of if Statement:
switch (expression)
{
case value1:
statements;
case value2:
statements;
....
....
....
default:
statements;
}
Page 7
Subject: Logic Development Programming
Prepared By: Prof. Ankit Patel
BCA- SEM-1
Example- Example of Switch Case in C
#include <stdio.h>
int main()
{
int var = 2;
switch (var)
{
case 1:
printf("Case 1 is executed");
break;
case 2:
printf("Case 2 is executed");
break;
default:
Page 8
Subject: Logic Development Programming
Prepared By: Prof. Ankit Patel
BCA- SEM-1
printf("Default Case is executed");
break;
}
return 0;
}
OUTPUT:
Case 2 is executed
6) Break Statement:
This loop control statement is used to terminate the loop. As soon as the break
statement is encountered from within a loop, the loop iterations stop there, and
control returns from the loop immediately to the first statement after the loop.
Syntax of if Statement:
break;
Page 9
Subject: Logic Development Programming
Prepared By: Prof. Ankit Patel
BCA- SEM-1
Example- Example of Break Statement in C
#include <stdio.h>
void findElement(int arr[], int size, int key)
{
if (arr[i] == key)
{
printf("Element found at position: %d", (i + 1));
break;
}
}
}
int main()
{
int arr[] = { 1, 2, 3, 4, 5, 6 };
int n = 6;
int key = 3;
findElement(arr, n, key);
return 0;
}
OUTPUT:
Element found at position: 3
7) Continue Statement:
This loop control statement is just like the break statement. The continue statement is
opposite to that of the break statement, instead of terminating the loop, it forces to
execute the next iteration of the loop.
As the name suggests the continue statement forces the loop to continue or execute
the next iteration. When the continue statement is executed in the loop, the code
inside the loop following the continue statement will be skipped and the next iteration
of the loop will begin.
Syntax of if Statement:
continue;
Page
10
Subject: Logic Development Programming
Prepared By: Prof. Ankit Patel
BCA- SEM-1
Example- Example of Continue Statement in C
#include <stdio.h>
int main()
{
// loop from 1 to 10
for (int i = 1; i <= 10; i++)
{
if (i == 6)
continue;
else
printf("%d ", i);
}
return 0;
}
OUTPUT:
1 2 3 4 5 7 8 9 10
7) Go to Statement:
The goto statement in C also referred to as the unconditional jump statement can be
used to jump from one point to another within a function.
Syntax of if Statement:
Page
11
Subject: Logic Development Programming
Prepared By: Prof. Ankit Patel
BCA- SEM-1
Syntax1 | Syntax2
----------------------------
goto label; | label:
. | .
. | .
. | .
label: | goto label;
Example- Example of Go to Statement in C
#include <stdio.h>
void printNumbers()
{
int n = 1;
label:
printf("%d ", n);
n++;
if (n <= 10)
goto label;
}
int main()
{
printNumbers();
return 0;
}
Page
12
Subject: Logic Development Programming
Prepared By: Prof. Ankit Patel
BCA- SEM-1
OUTPUT:
1 2 3 4 5 7 8 9 10
Looping Statements:
There are mainly two types of loops in C Programming:
Entry Controlled loops: In Entry controlled loops the test condition is checked
before entering the main body of the loop. For Loop and While Loop is Entry-
controlled loops.
Exit Controlled loops: In Exit controlled loops the test condition is evaluated at the
end of the loop body. The loop body will execute at least once, irrespective of
whether the condition is true or false. do-while Loop is Exit Controlled loop.
1) FOR Loop:
For loop in C programming is a repetition control structure that allows programmers
to write a loop that will be executed a specific number of times. for loop enables
programmers to perform n number of steps together in a single line.
Syntax of if Statement:
for (initialize expression; test expression; update expression)
{
//
// body of for loop
//
}
Page
13
Subject: Logic Development Programming
Prepared By: Prof. Ankit Patel
BCA- SEM-1
Example- Example of For Loop in C
#include <stdio.h>
int main()
{
int i = 0;
for (i = 1; i <= 10; i++)
{
printf( "Ankit\n");
}
return 0;
}
OUTPUT:
Ankit
Ankit
Ankit
Ankit
Ankit
Ankit
Ankit
Ankit
Ankit
Ankit
Page
14
Subject: Logic Development Programming
Prepared By: Prof. Ankit Patel
BCA- SEM-1
2) While Loop:
While loop does not depend upon the number of iterations. In for loop the number of
iterations was previously known to us but in the While loop, the execution is
terminated on the basis of the test condition. If the test condition will become false
then it will break from the while loop else body will be executed.
Syntax of if Statement:
initialization_expression;
while (test_expression)
{
// body of the while loop
update_expression;
}
Example- Example of While Loop in C
#include <stdio.h>
int main()
{
int i = 2;
while(i < 10)
{
printf( "Ankit\n");
i++;
}
return 0;
}
Page
15
Subject: Logic Development Programming
Prepared By: Prof. Ankit Patel
BCA- SEM-1
OUTPUT:
Ankit
Ankit
Ankit
Ankit
Ankit
Ankit
Ankit
Ankit
3) Do While Loop:
The do-while loop is similar to a while loop but the only difference lies in the do-
while loop test condition which is tested at the end of the body. In the do-while loop,
the loop body will execute at least once irrespective of the test condition.
Syntax of if Statement:
initialization_expression;
do
{
// body of do-while loop
update_expression
}
while (test_expression);
Page
16
Subject: Logic Development Programming
Prepared By: Prof. Ankit Patel
BCA- SEM-1
Example- Example of Do While Loop in C
#include <stdio.h>
int main()
{
int i = 2;
do
{
printf( "ANKIT\n");
i++;
} while (i < 1);
return 0;
}
OUTPUT:
ANKIT
Array:
An array in C is a fixed-size collection of similar data items stored in contiguous
memory locations. It can be used to store the collection of primitive data types such
as int, char, float, etc., and also derived and user-defined data types such as pointers,
structures, etc.
Syntax of Array Declaration:
o data_type array_name [size];
o or
o data_type array_name [size1] [size2]...[sizeN];
Page
17
Subject: Logic Development Programming
Prepared By: Prof. Ankit Patel
BCA- SEM-1
Example of Array Declaration:
#include <stdio.h>
int main()
{
// declaring array of integers
int arr_int[5];
// declaring array of characters
char arr_char[5];
return 0;
}
C Array Initialization:
Initialization in C is the process to assign some initial value to the variable. When the
array is declared or allocated memory, the elements of the array contain some garbage
value. So, we need to initialize the array to some meaningful value. There are
multiple ways in which we can initialize an array in C.
Array Initialization with Declaration:
In this method, we initialize the array along with its declaration. We use an initializer
list to initialize multiple elements of the array. An initializer list is the list of values
enclosed within braces { } separated b a comma.
data_type array_name [size] = {value1, value2, ... valueN};
Page
18
Subject: Logic Development Programming
Prepared By: Prof. Ankit Patel
BCA- SEM-1
Array Initialization with Declaration without Size:
If we initialize an array using an initializer list, we can skip declaring the size of the
array as the compiler can automatically deduce the size of the array in these cases.
The size of the array in these cases is equal to the number of elements present in the
initializer list as the compiler can automatically deduce the size of the array.
data_type array_name[] = {1,2,3,4,5};
Array Initialization after Declaration (Using Loops)
We initialize the array after the declaration by assigning the initial value to each
element individually. We can use for loop, while loop, or do-while loop to assign the
value to each element of the array.
for (int i = 0; i < N; i++)
{
array_name[i] = valuei;
}
How to use Array in C?
The following program demonstrates how to use an array in the C programming
language:
#include <stdio.h>
int main()
{
// array declaration and initialization
int arr[5] = { 10, 20, 30, 40, 50 };
Page
19
Subject: Logic Development Programming
Prepared By: Prof. Ankit Patel
BCA- SEM-1
// modifying element at index 2
arr[2] = 100;
// traversing array using for loop
printf("Elements in Array: ");
for (int i = 0; i < 5; i++)
{
printf("%d ", arr[i]);
}
return 0;
}
OUTPUT:
Elements in Array: 10 20 100 40 50
Purpose of Header Files:
1) Code Reusability
2) Modularization
3) Efficient Compilation
Types of Array in C
There are two types of arrays based on the number of dimensions it has. They are as
follows:
One Dimensional Arrays (1D Array)
Multidimensional Arrays
1. One Dimensional Array in C:
The One-dimensional arrays, also known as 1-D arrays in C are those arrays that have
only one dimension.
Page
20
Subject: Logic Development Programming
Prepared By: Prof. Ankit Patel
BCA- SEM-1
Example-1 Array of 1D Array in C:
#include <stdio.h>
int main()
{
// 1d array declaration
int arr[5];
// 1d array initialization using for loop
for (int i = 0; i < 5; i++) {
arr[i] = i * i - 2 * i + 1;
}
printf("Elements of Array: ");
// printing 1d array by traversing using for loop
for (int i = 0; i < 5; i++)
{
printf("%d ", arr[i]);
}
return 0;
}
OUTPUT:
Elements of Array: 1 0 1 4 9
Example-2 Array of Characters
#include <stdio.h>
int main()
{
// creating array of character
char arr[6] = { 'A', 'N', 'K', 'I', 'T', '\0' };
// printing string
int i = 0;
while (arr[i])
{
printf("%c", arr[i++]);
}
return 0;
}
OUTPUT:
ANKIT
Page
21
Subject: Logic Development Programming
Prepared By: Prof. Ankit Patel
BCA- SEM-1
2. Two Dimensional Array in C:
Two-Dimensional array or 2D array in C is an array that has exactly two dimensions.
They can be visualized in the form of rows and columns organized in a two-dimensional
plane.
Syntax of 2D Array in C
array_name [size1] [size2];
Example-1 Array of 2D Array in C:
#include <stdio.h>
int main()
{
// declaring and initializing 2d array
int arr[2][3] = { 10, 20, 30, 40, 50, 60 };
printf("2D Array:\n");
// printing 2d array
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 3; j++)
{
printf("%d ",arr[i][j]);
}
printf("\n");
}
return 0;
}
OUTPUT:
Page
22
Subject: Logic Development Programming
Prepared By: Prof. Ankit Patel
BCA- SEM-1
2D Array:
10 20 30
40 50 60
3. Three Dimensional Array in C:
Another popular form of a multi-dimensional array is Three Dimensional Array or 3D
Array. A 3D array has exactly three dimensions. It can be visualized as a collection of
2D arrays stacked on top of each other to create the third dimension.
Syntax of 3D Array in C
array_name [size1] [size2] [size3];
Example-1 Array of 3D Array in C:
#include <stdio.h>
int main()
{
int arr[2][2][2] = { 10, 20, 30, 40, 50, 60,70,80 };
// printing elements
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 2; j++)
{
for (int k = 0; k < 2; k++)
{
printf("%d ", arr[i][j][k]);
}
printf("\n");
}
printf("\n \n");
}
return 0;
}
Page
23
Subject: Logic Development Programming
Prepared By: Prof. Ankit Patel
BCA- SEM-1
OUTPUT:
3D Array:
10 20
30 40
50 60
70 80
Characteristics of Arrays in C
Fixed Size: The size of an array is defined at compile time and cannot be changed
during runtime.
Homogeneous Collection: Arrays can only store elements of the same data type.
Indexing: Array indexing starts from 0. The first element is at index 0, and the last
element is at index N-1 for an array of size N.
Dimensions: Arrays can be single-dimensional, multi-dimensional, or even higher-
dimensional. The dimension indicates the number of indices required to access elements.
Contiguous Storage: Elements are stored in contiguous memory locations, enabling
efficient access and management.
Random Access: Any element can be accessed in constant time,
No Index Out-of-Bounds Checking: C does not provide runtime checks for accessing
indices beyond the array's declared range, which can lead to undefined behavior.
Advantages of Array in C:
Random and fast access of elements using the array index.
Use of fewer lines of code as it creates a single array of multiple elements.
Traversal through the array becomes easy using a single loop.
Sorting becomes easy as it can be accomplished by writing fewer lines of code.
Disadvantages of Array in C:
Allows a fixed number of elements to be entered which is decided at the time of
declaration. Unlike a linked list, an array in C is not dynamic.
Page
24
Subject: Logic Development Programming
Prepared By: Prof. Ankit Patel
BCA- SEM-1
Insertion and deletion of elements can be costly since the elements are needed to be
rearranged after insertion and deletion.
Few Examples of Array in C:
Example-1 C Program to print the average of the given list of numbers
#include <stdio.h>
float getAverage(float* arr, int size)
{
int sum = 0;
for (int i = 0; i < size; i++)
{
sum += arr[i];
}
return sum / size;
}
int main()
{
float arr[5] = { 10, 20, 30, 40, 50 };
int n = sizeof(arr) / sizeof(float);
printf("Array Elements: ");
for (int i = 0; i < n; i++)
{
printf("%.0f ", arr[i]);
}
printf("\nAverage: %.2f", getAverage(arr, n));
return 0;
}
OUTPUT:
Array Elements: 10 20 30 40 50
Average: 30.00
Example-2 C Program to find the largest number in the array.
#include <stdio.h>
int getMax(int* arr, int size)
{
int max = arr[0];
Page
25
Subject: Logic Development Programming
Prepared By: Prof. Ankit Patel
BCA- SEM-1
for (int i = 1; i < size; i++)
{
if (max < arr[i])
{
max = arr[i];
}
}
return max;
}
int main()
{
int arr[10] = { 135, 165, 1, 16, 511, 65, 654, 654, 169, 4 };
printf("Largest Number in the Array: %d",getMax(arr, 10));
return 0;
}
OUTPUT:
Largest Number in the Array: 654
Example-3 Write a C Program to display sum of all array numbers.
#include <stdio.h>
int main() {
int n, i, sum = 0;
printf("Enter the number of elements: ");
scanf("%d", &n);
int arr[n];
printf("Enter %d elements:\n", n);
for (i = 0; i < n; i++)
{
printf("Element %d: ", i + 1);
scanf("%d", &arr[i]);
}
for (i = 0; i < n; i++)
{
sum += arr[i];
}
printf("The sum of the array elements is: %d\n", sum);
return 0;
}
Page
26
Subject: Logic Development Programming
Prepared By: Prof. Ankit Patel
BCA- SEM-1
OUTPUT:
Enter the number of elements: 5
Enter 5 elements:
Element 1: 10
Element 2: 20
Element 3: 30
Element 4: 40
Element 5: 50
The sum of the array elements is: 150
String:
A String in C programming is a sequence of characters terminated with a null
character ‘\0’.
The null character indicates the end of the string, so it is always one character longer
than the actual sequence of characters.
The C String is stored as an array of characters.
Key Points about Strings:
Strings are arrays of char with a null terminator.
Strings in C are enclosed in double quotes (" ").
Strings are manipulated using functions like strcpy, strlen, strcat, etc., from
<string.h>.
C String Declaration Syntax:
Declaring a string in C is as simple as declaring a one-dimensional array. Below is the
basic syntax for declaring a string.
char string_name[size];
Page
27
Subject: Logic Development Programming
Prepared By: Prof. Ankit Patel
BCA- SEM-1
How to Read a String Separated by Whitespaces in C?
o We can use multiple methods to read a string separated by spaces in C. The
two of the common ones are:
o We can use the fgets() function to read a line of string and gets() to read
characters from the standard input (stdin) and store them as a C string until a
newline character or the End-of-file (EOF) is reached.
o We can also scanset characters inside the scanf() function
Example- Example of String Input using gets()
#include <stdio.h>
#define MAX 50
int main()
{
char str[MAX];
fgets(str, MAX, stdin);
printf("String is: \n");
puts(str);
return 0;
}
OUTPUT:
String is:
Ankit
Example- Example of String Input using scanset
#include <stdio.h>
int main()
{
char str[20];
scanf("%[^\n]s", str);
printf("%s", str);
return 0;
}
OUTPUT:
ANKIT
Page
28
Subject: Logic Development Programming
Prepared By: Prof. Ankit Patel
BCA- SEM-1
Built-in String Functions.
The C string functions are built-in functions that can be used for various operations
and manipulations on strings.
These string functions can be used to perform tasks such as string copy,
concatenation, comparison, length, etc. The <string.h> header file contains these
string functions.
:
1) strcat() Function:
#include <stdio.h>
int main()
{
char dest[50] = "Hello";
char src[50] = " I am Ankit";
printf("String Before: %s\n", dest);
strcat(dest, src);
printf("String After: %s", dest);
return 0;
}
OUTPUT:
String Before: Hello
String After: Hello I am Ankit
2) strlen() Function:
#include <stdio.h>
#include <string.h>
int main()
{
char str[] = "Hi I am Ankit";
size_t length = strlen(str);
printf("String: %s\n", str);
printf("Length: %zu\n", length);
return 0;
}
Page
29
Subject: Logic Development Programming
Prepared By: Prof. Ankit Patel
BCA- SEM-1
OUTPUT:
String: Hi I am Ankit
Length: 13
3) strcmp() Function:
#include <stdio.h>
#include <string.h>
int main()
{
char str1[100], str2[100];
printf("Enter the first string: ");
scanf("%s", str1);
printf("Enter the second string: ");
scanf("%s", str2);
int result = strcmp(str1, str2);
if (result == 0)
{
printf("The strings are equal.\n");
} else if (result < 0)
{
printf("The first string is less than the second string.\n");
} else
{
printf("The first string is greater than the second string.\n");
}
return 0;
}
OUTPUT:
Enter the first string: apple
Enter the second string: orange
The first string is less than the second string.
4) strcpy() Function:
int main()
{
char source[] = "ANKIT";
char dest[20];
Page
30
Subject: Logic Development Programming
Prepared By: Prof. Ankit Patel
BCA- SEM-1
strcpy(dest, source);
printf("Source: %s\n", source);
printf("Destination: %s\n", dest);
return 0;
}
OUTPUT:
Source: ANKIT
Destination: ANKIT
5) strchr() Function:
#include <stdio.h>
#include <string.h>
int main()
{
char str[] = "Ankit";
char ch = 'e';
char* result = strchr(str, ch);
printf("The character '%c' is found at index %ld\n",ch, result - str);
}
else
{
printf("The character '%c' is not found in the " "string\n", ch);
}
return 0;
}
OUTPUT:
The character 'n' is found at index 1
5) strupr() & strlwr() Function:
#include <stdio.h>
#include <string.h>
int main()
{
char str1[] = "Ankit";
char str2[] = "PATEL";
Page
31
Subject: Logic Development Programming
Prepared By: Prof. Ankit Patel
BCA- SEM-1
strupr(str1);
strlwr(str2);
printf("Upper String=%s",str1);
printf("Upper String=%s",str2);
return 0;
}
OUTPUT:
Upper String= ANKIT
Lower String= patel
6) strtok() Function:
#include <stdio.h>
#include <string.h>
int main()
{
char str[] = "Ankit,Patel.Gandhinagar";
// Delimiters: space, comma, dot,
// exclamation mark
const char delimiters[] = ",.";
char* token = strtok(str, delimiters);
while (token != NULL)
{
printf("Token: %s\n", token);
token = strtok(NULL, delimiters);
}
return 0;
}
OUTPUT:
Token: Ankit
Token: Patel
Token: Gandhinagar
7) strstr() Function:
#include <stdio.h>
#include <string.h>
Page
32
Subject: Logic Development Programming
Prepared By: Prof. Ankit Patel
BCA- SEM-1
int main()
{
char s1[] = "ankitpatelgandhinagar";
char s2[] = "patel";
char* result;
result = strstr(s1, s2);
if (result != NULL)
{
printf("Substring found: %s\n", result);
}
else
{
printf("Substring not found.\n");
}
return 0;
}
OUTPUT:
Substring found:patelgandhinagar
String Storage in C:
In C, a string is stored as an array of characters in contiguous memory locations,
terminated by a special character called the null character (\0). This null terminator
marks the end of the string and distinguishes it from other arrays of characters.
1) Character Array:
A string is stored in an array of type char.
Each character occupies one byte of memory.
The null character (\0) requires an additional byte of memory.
Example:
char str[6] = "Hello"; // 'H', 'e', 'l', 'l', 'o', '\0'
2) String Literal:
When a string is directly assigned to a character pointer or array, it is stored in a read-
only section of memory (usually in the text segment).
Page
33
Subject: Logic Development Programming
Prepared By: Prof. Ankit Patel
BCA- SEM-1
Here, the string "Hello" is stored in a read-only section, and str points to its starting
memory address.
Example:
char *str = "Hello";
3) Dynamic Memory Allocation:
Strings can also be allocated dynamically using functions like malloc or calloc.
Example:
char *str = (char *)malloc(6 * sizeof(char));
strcpy(str, "Hello");
String Initialization in C:
Using an Array:
char str1[] = "Hello"; // Array of size 6 (includes '\0')
Using a Pointer:
char *str2 = "Hello"; // Pointer to a string literal
Dynamic Allocation:
char *str3 = (char *)malloc(6);
strcpy(str3, "Hello");
Page
34