C Prog
C Prog
• // printing string
• int i = 0;
• while (arr[i]) {
• printf("%c", arr[i++]);
• }
• return 0;
• }
• 2. Multidimensional Array in C
• Multi-dimensional Arrays in C are those arrays
that have more than one dimension. Some of
the popular multidimensional arrays are 2D
arrays and 3D arrays. We can declare arrays
with more dimensions than 3d arrays but they
are avoided as they get very complex and
occupy a large amount of space.
• A. Two-Dimensional Array in C
• A 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];
• Here,
• size1: Size of the first dimension.
• size2: Size of the second dimension.
•
• // C Program to illustrate 2d array
• #include <stdio.h>
• int main()
• {
• 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
2D Array: 10 20 30
40 50 60
• 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];
• // C Program to illustrate the 3d array
• #include <stdio.h>
• int main()
• {
• // 3D array declaration
• int arr[2][2][2] = { 10, 20, 30, 40, 50, 60 };
• // 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;
• }
Output
10 20
30 40
50 60
0 0 0
• Strings in C
• A String in C programming is a sequence
of characters terminated with a null
character ‘\0’. The C String is stored as an
array of characters. The difference
between a character array and a C string
is that the string in C is terminated with a
unique character ‘\0’.
•
• 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];
• In the above syntax string_name is any name given
to the string variable and size is used to define the
length of the string, i.e the number of characters
strings will store.
• There is an extra terminating character which is
the Null character (‘\0’) used to indicate the
termination of a string that differs strings from normal
character arrays.
Initialization of string
There are multiple ways we can initialize a string.
Direct initialization
If we assign string directly with in double quotes, no need to bother about null
character.
Because compiler will automatically assign the null character at the end of string.
char country[6]=”india”;
Note
while giving initial size, we should always give extra one size to store null
character.
To store "india", string size 5 is enough but we should give extra one (+1) size 6 to
store null character.
ii)Assigning direct string without size
Example
char country[]=”india”;
Syntax
int strcmp(const char *str1, const char *str2);
Parameters
•str1: This is the first string to be compared.
•str2: This is the second string to be compared.
yntax
har* strcpy(char* dest, const char* src);
• 4. strcpy
• The strcpy() is a standard library
function in C and is used to copy one
string to another. In C, it is present
in <string.h> header file.
Syntax
char* strcpy(char* dest, const char*
src);
• Parameters
• dest: Pointer to the destination array
where the content is to be copied.
• src: string which will be copied.
5. strchr()
The strchr() function in C is a predefined
function used for string handling. This
function is used to find the first occurrence
of a character in a string.
Syntax
char *strchr(const char *str, int c);
strrchr() Function
In C, strrchr() function is similar to strchr() function. This function is
used
to find the last occurrence of a character in a string.
Syntax
char* strchr(const char *str, int ch);
7. strtok()
The strtok() function is used to split the st
tokens
based on a set of delimiter characters.
Syntax
char * strtok(char* str, const char
• Parameters
• str: It is the string to be tokenized.
• delims: It is the set of delimiters
Functions in C
• Definition
• A function is a self contained block
of code that performs a certain
task/job. For example, we can write a
function for reading an integer or we
can write a function to add numbers
from 1 to 100 etc.
• Generally a function can be imagined like
a Black Box, which accepts data input and
transforms the data into output results. The
user knows only about the inputs and
outputs. User has no knowledge of the
process going on inside the box which
converts the given input into output.
Types of Functions
return_type function_name
1 Function declaration
(argument list);
return_type function_name
3 Function definition
(argument list) {function body;}
• Return Value
• A C function may or may not return a
value from the function. If you don't have
to return any value from the function,
use void for the return type.
• Example without return value:
1.void hello(){
2.printf("hello c");
3.}
• Example with return value:
1.int get(){
2.return 10;
3.}
• In the above example, we have to
return 10 as a value, so the return type
is int. If you want to return floating-
point value (e.g., 10.2, 3.1, 54.5, etc),
you need to use float as the return type
of the method.
1.float get(){
2.return 10.2;
3.}
Different aspects of function
calling
• A function may or may not accept any argument. It
may or may not return any value. Based on these
facts, There are four different aspects of function
calls.
The keyword "struct" is used to define a structure. The keyword "union" is used to define a structure.
Modifying the value of one item won't affect the other items Modifying one single data item affects other members of
or the structure at all. the union thus affecting the whole unit.
We initialize several members at once. We can initialize only the first member of union.
The total size of the structure is the sum of the size of The total size of the union is the size of the largest data
every data member member
It is mainly used for storing one of the many data types that
It is mainly used for storing various data types
are available.
It occupies space for each and every member written in It occupies space for a member having the highest size
inner parameters written in inner parameters
We can retrieve any member at any time We can only access one member at a time in the union.