C Notes
C Notes
The <string.h> library has many functions that allow you to perform tasks on strings.
strcat()
strchr()
strcmp()
Compares the ASCII values of characters in two strings to determine which string has a higher value
strcpy()
strerror()
strlen()
strncat()
strncmp()
Compares the ASCII values of a specified number of characters in two strings to determine which string
has a higher value
strncpy()
Copies a number of characters from one string into the memory of another string
Strlen
strcpy
STRINGS
An array of characters is called a string.
Declaration
The syntax for declaring an array is as follows −
POINTERS IN C
A pointer variable points to a data type (like int) of the same type, and is
created with the * operator.
The address of the variable you are working with is assigned to the pointer:
EXAMPLE
Dereference
In the example above, we used the pointer variable to get the memory address of a variable (used
together with the & reference operator).
You can also get the value of the variable the pointer points to, by using the * operator
(the dereference operator):
Example
int myAge = 43; // Variable declaration
int* ptr = &myAge; // Pointer declaration
// Reference: Output the memory address of myAge with the pointer (0x7ffe5367e044)
printf("%p\n", ptr);
In C, call by value and call by reference are two ways to pass arguments to functions. Here’s how they
differ:
1. Call by Value
Changes made inside the function do not affect the original variable.
Example:
#include <stdio.h>
void add(int x)
x = x + 5;
int main() {
return 0;
}
Output:
2. Call by Reference
The function modifies the actual value stored at the memory location.
Example:
#include <stdio.h>
int main()
return 0;
STRUCTURE SYNTAX
Use the struct keyword inside the main() method, followed by the name of the
structure and then the name of the structure variable:
struct mySelf {
int myAge;
char myInitial;
};
int main() {
struct mySelf s1;
return 0;
}
int main() {
// Create a structure variable of mySelf called s1
struct mySelf s1;
// Assign values to members of s1
s1.myAge = 21;
s1.myInitial = 'P';
// Print values
printf("My number: %d\n", s1.myAge);
printf("My letter: %c\n", s1.myInitial);
return 0;
}
struct mySelf {
int myAge;
char myInitial;
char myName[30]; // String
};
int main() {
struct mySelf s1;
// Display
printf("My number: %d\n", s1.myAge);
printf("My letter: %c\n", s1.myInitial);
printf("My name is: %s", s1.myName);
return 0;
}
Simpler Syntax
You can also assign values to members of a structure variable at declaration
time, in a single line.
Just insert the values in a comma-separated list inside curly braces {}. Note
that you don't have to use the strcpy() function for string values with this
technique:
Example
// Create a structure
struct mySelf {
int myAge;
char myInitial;
char myName[30];
};
int main() {
// Create a structure variable and assign values to it
struct mySelf s1 = {21, 'P', "Washie"};
// Print values
printf("%d %c %s", s1.myAge, s1.myInitial, s1.myName);
return 0;
}
Study questions
1. Create a structure named "Employee" to store employee details such as employee ID,
name, and salary. Write a program to input data for three employees, find the highest
salary employee, and display their information.
2. Mrs. Jadah owns a car sale. Design a program that you think she must use in order for her
to sell her cars. The program must include the following:
I. Price of the car
II. Name of the car
III. Version number
IV. Registration number
V. Color of the car
3. Mr. Tees owns a book shop. Design a program that you think he must use in order for him
to sell books. The program must include the following:
VI. Price of book
VII. Title of the book
VIII. Number of pages
IX. Volume number
C Unions
A union is similar to a struct in that it can store members of different data
types.
Example
union mySelf { // Union declaration
int myAge; // Member (int)
char myInitial; // Member (char)
char myName[30]; // Member (char array)
};
int main() {
union mySelf u1; // Create a union variable with the name "u1":
return 0;
}