0% found this document useful (0 votes)
9 views10 pages

C Notes

Uploaded by

magaisatavonga33
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views10 pages

C Notes

Uploaded by

magaisatavonga33
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

C string Functions

The <string.h> library has many functions that allow you to perform tasks on strings.

A list of all string functions can be found in the table below:

strcat()

Appends one string to the end of another

strchr()

Returns a pointer to the first occurrence of a character in a string

strcmp()

Compares the ASCII values of characters in two strings to determine which string has a higher value

strcpy()

Copies the characters of a string into the memory of another string

strerror()

Returns a string describing the meaning of an error code

strlen()

Return the length of a string

strncat()

Appends a number of characters from a string to the end of another string

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 −

char stringname [size];

For example − char string[50]; string of length 50 characters

POINTERS IN C

A pointer is a variable that stores the memory address of another variable


as its value.

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

int myAge = 21; // An int variable


int* ptr = &myAge; // A pointer variable, with the name ptr, that stores the address of myAge

// Output the value of myAge (21)


printf("%d\n", myAge);

// Output the memory address of myAge (0x7ffe5367e044)


printf("%p\n", &myAge);

// Output the memory address of myAge with the pointer (0x7ffe5367e044)


printf("%p\n", ptr);

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);

// Dereference: Output the value of myAge with the pointer (43)


printf("%d\n", *ptr);

When used in declaration (int* ptr), it creates a pointer variable.

When not used in declaration, it act as a dereference operator.

CALL BY VALUE AND CALL BY REFERENCE

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

A copy of the actual value is passed to the function.

Changes made inside the function do not affect the original variable.

Memory for the argument is separate from the original variable.

Example:

#include <stdio.h>

void add(int x)

x = x + 5;

int main() {

int num = 10;

add(num); // Pass by value

printf("After call by value: %d\n", num); // Original value remains unchanged

return 0;
}

Output:

After call by value the output is 10 after “call by value”

The original value remains unchanged.

2. Call by Reference

A memory address (pointer) is passed to the function.

Changes made inside the function do affect the original variable.

The function modifies the actual value stored at the memory location.

Example:

#include <stdio.h>

void add(int *x)

*x = *x + 15; // this modifies the original variable using the pointer

int main()

int num = 20;

add(&num); // Pass by reference

printf("After calling by reference: %d\n", num); // the original value is modified

return 0;

Here the output is 35 after “call by reference”


STRUCTURE AND UNIONS IN C

STRUCTURE SYNTAX

struct MySelf { // Structure declaration


int myAge; // Member (int variable)
char myInitial; // Member (char variable)
}; // End

ACCESSING STRUCTURE MEMBERS

To access the structure, you must create a variable of it.

Use the struct keyword inside the main() method, followed by the name of the
structure and then the name of the structure variable:

Create a struct variable with the name "s1":

struct mySelf {
int myAge;
char myInitial;
};

int main() {
struct mySelf s1;
return 0;
}

ACCESSING STRUCTURE MEMBERS EXAMPLE 2

// Create a structure called mySelf


struct mySelf {
int myAge;
char myInitial;
};

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;
}

ACCESSING STRUCTURE MEMBERS EXAMPLE 3

struct mySelf {
int myAge;
char myInitial;
char myName[30]; // String
};

int main() {
struct mySelf s1;

// Assign a value to the string using the strcpy function


strcpy(s1.myName, "Prim Washie");
s1.myAge = 21;
s1.myInitial = 'P';

// 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.

However, there are some differences:

 In a struct, each member has its own memory.


 In a union, all members share the same memory, which means you
can only use one of the values at a time.

Declare a Union Syntax


To create a union, use the union keyword, and then create a variable from it
(just like with structs):

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;
}

When to Use Unions


Use unions when:

 You need to store different types in the same location


 You only use one type at a time
 Saving memory is important

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy