[go: up one dir, main page]

0% found this document useful (0 votes)
22 views63 pages

Functions 2024 Kla Struc 2024

The document covers various programming concepts in C, including functions, pointers, file management, strings, structures, unions, memory allocation, command-line arguments, and bitwise operators. It provides code examples for each topic, demonstrating how to define and call functions, use pointers, manage files, and allocate memory dynamically. Additionally, it discusses the use of command-line arguments and includes examples of input/output operations.

Uploaded by

Ssenono Francis
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)
22 views63 pages

Functions 2024 Kla Struc 2024

The document covers various programming concepts in C, including functions, pointers, file management, strings, structures, unions, memory allocation, command-line arguments, and bitwise operators. It provides code examples for each topic, demonstrating how to define and call functions, use pointers, manage files, and allocate memory dynamically. Additionally, it discusses the use of command-line arguments and includes examples of input/output operations.

Uploaded by

Ssenono Francis
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/ 63

Functions

Pointers
File Management
Strings
Structures and Unions
Memory Allocation
Commandline Arguments
Bitwise operators
FUNCTIONS
A function is a group of programming
statements given a name and intended to
do a particular task
int main ( ….) {
………
return 0;
}
FUNCTIONS
int Add ( int y ) {
int s = y+100;
return (s);
}
•Call:
Add(6);
FUNCTIONS
. Accepts an int, a float and an int and returns their
average
. Accepts your year of birth, returns your age.
. Accepts two ints, returns their product
. Accepts two ints, returns their Sum

• Call all those functions:


Add(6);
##include<stdio.h>
int Sum ( int x, int y) {
int a;
a = x+y;
return (a);
}

int main ( ) {
printf(“%d” , Sum (16, 100) );
return 0;
}
#include<stdio.h>
int Sum ( int x, int y);

int main ( ) {
int one =7, two =8;
printf(“%d” , Sum (one, two) );
return 0;
}
int Sum ( int x, int y) {
int a;
a = x+y;
return (a);
}
##include<stdio.h>
int Sum ( int x, int y) {
int a;
a = x+y;
return (a);
}
int main ( ) {
int one =7, two =8;
printf(“%d” , Sum (one, two) );
return 0;
}
##include<stdio.h>
int Sum ( int x, int y) {
int a;
a = x+y;
return (a);
}
int main ( ) {
int one, two;
printf(“Enter an integer”);
scanf(“%d” , &one);
printf(“Enter anonther integer”);
scanf(“%d” , &two);
printf(“%d” , Sum (one, two) );
return 0;
}
##include<stdio.h>
int Sum ( int x, int y) {
int a;
a = x+y;
return (a);
}
int main ( ) {
int one, two;
printf(“Enter an integer”);
scanf(“%d” , &one);
printf(“Enter anonther integer”);
scanf(“%d” , &two);
printf(“The sum of %d and %d is: %d ” , one, two, Sum (one, two) );
return 0;
}
##include<stdio.h> ##include<stdio.h>
int Sum ( int x, int y) { int Sum ( int x, int y) ;
int a; int main ( ) {
a = x+y;
printf(“%d” , Sum (16, 100) );
return (a);
} return 0;
int main ( ) { }
printf(“%d” , Sum (16, 100) ); int Sum ( int x, int y) {
return 0; int a;
} a = x+y;
return (a);
}
#include<stdio.h>
int st;
int Sum ( int x, int y) {
int a;
a = x+y;
return (a);
}
int Sum (int s)
{
return (s+100);
}
int main ( ) {
printf(“%d \n” , Sum (16, 100) );
printf(“%d \n” , Sum (160) );
return 0;
}
##include<stdio.h>
int Sum ( int x, int y = 9) {
int a;
a = x+y;
return (a);
}
int main ( ) {
printf(“%d” , Sum (16, 100) );
printf(“%d” , Sum (1000) );
printf(“%d” , a ); //error
return 0;
}
##include<stdio.h>
float Sum2 ( int x, float r, int y = 9) {
float a;
a = x+y +r;
return (a);
}
int main ( ) {
printf(“%d” , Sum2 (16, 2.3, 100) );
printf(“%d” , Sum2 (1000, 2.1) );
return 0;
}
Recursive Functions.
…. A function that calls itself within
its body
POINTERS
007 011

100 007
x ptx

A pointer is a variable that stores the


address of another variable
int x = 100; int *ptx;
ptx = &x;
*ptx; // value pointed by ptx output x ptx *pty
POINTERS
x ptx
100 007
007 011

int x = 100; int *ptx;


ptx = &x;

printf(“Pointed by ptx : %d \n” , *ptx) ; // 100


printf(“The value of x: %d \n” , x) ; // 100
printf(“Address of ptx: %p \n” , &ptx) ; //
printf(“Address of x: % p \n” , &x) ; // ……..
printf(“Pointer ptx: % p \n” , ptx) ; // ……..
POINTERS
#include<stdio.h> printf(“Ptd by x is: %d \n”, *px);
int main ( ){ printf(“Ptd by y is: %d \n”, *py);
int x = 70, y = 80; return 0; }
int *px, *py;
px = &x;
py = &y;
*px = *py;
*py = 100;
POINTERS
#include<stdio.h> printf(“Ptd by p is: %d \n”, *pp);
int main ( ){ printf(“Ptd by q is: %d \n”, *pq);
int p = 10, q = 20; return 0; }
int *pp, *pq;
pp = &p;
pq = &q;
*pq = *pp;
*pp = 20;
Draw diagrams to represent the code above
POINTERS
#include<stdio.h> #include<stdio.h>
int Incre(int x) int Incre2( int *x)
{ {
x = x+1 ; *x = *x +1;
return ( x); return (*x) ;
} }
int main ( ){ int main ( ){
int a =20; int a =20;
Incre(a); Incre2(&a);
printf(“ a is now%d”, a); printf(“ a is now%d”, a);
return 0; return 0;
} }
POINTERS 002 009
void Swap ( int *a, int *b){
int x; x = *b;
*b = *a;
*a = x; }
Call in main 002 009
int a =5, b =10; 5 10
Swap (&a, &b); a b
POINTERS and TEXT STRINGS
5
H e l l o
char *a; 5
a= “Hello World”; a
printf(“%s”, a);
printf(“%c”, *a);
printf(“%d”, *a); This outputs the numeric equivalent of H
POINTERS and Arrays

The name of the array is a constant pointer to the first


element of the array.
int mark[3] = { 4, 5, 6};

7 7 8 9
mark 4 5 6
0 1 2
POINTERS and Arrays
int mark[3] = { 4, 5, 6};
7 8 9
mark 7 4 5 6
0 1 2

*mark == mark[0]
*(mark+1) == mark[1]
*(mark+2) == mark[2]
(mark) == &mark[0]
(mark+1) == &mark[1]
(mark +2) == &mark[2]
FILE HANDLING
output on screen
input from keyboard
C Program

File A File B
C Program
Reads from File A
Outputs to File B
#include<stdio.h>
int main ( ){
FILE *fp;
fp = fopen(“Other.txt" , “w");
putc(‘F’ , fp);
putc(‘O’ , fp);
putc(‘O’ , fp);
putc(‘D’ , fp);
fclose(fp);
return 0;
}
#include<stdio.h>
int main ( ){
FILE *fp; char y;
fp = fopen(“me.txt" , "r");
y=getc(fp);
printf(“%c”, y);
y=getc(fp);
printf(“%c”, y);
y=getc(fp);
printf(“%c”, y);
fclose(fp);
return 0;
}
#include<stdio.h>
int main ( ){
FILE *fp; char y;
fp = fopen("The file.txt" , "r");
y=getc(fp);
while( y!=EOF) {
printf(“%c”, y);
y=getc(fp);
}
fclose(fp);
return 0;
}
#include<stdio.h>
int main ( ){
FILE *fp;
fp = fopen(“Other.txt" , “w");
if(fp==NULL)
{
printf(“file does not exist”);
}
else {
putc(‘F’ , fp);
putc(‘O’ , fp);
putc(‘O’ , fp);
putc(‘D’ , fp);
}
fclose(fp);
return 0;
}
• #include<stdio.h>
• int main ( ){
• FILE *fp;
• fp = fopen(“Other.txt" , “w");
• if(fp!=NULL)
• {
• putc(‘F’ , fp);
• putc(‘O’ , fp);
• putc(‘O’ , fp);
• putc(‘D’ , fp);
• }
• else {
• printf(“file does not exist”);
• }
• fclose(fp);
• return 0;
• }
#include<stdio.h>
int main ( ){
FILE *fp; char buffer[10]; char *res;
fp = fopen(“Other.txt" , “r");
res = fgets(buffer, 10, fp);
printf(“%s” , res);
res = fgets(buffer, 10, fp);
printf(“%s” , res);
res = fgets(buffer, 10, fp);
printf(“%s” , res);
fclose(fp);
return 0;
} see the loop on page 48
.
•fputs (string, fp);

•fputs (“Namaganda”, fp);

•char *one = “Namaganda”;

•fputs (one, fp);


.

•fprintf (file pointer, “%s”, string);


•fprintf(fp, “%s”, “Namaganda”);
•char *one = “Namaganda”;
•fprintf(fp, “%s”, one );
Command-line Arguments
.
.
.
.
.
#include<stdio.h>
int main (int argc, char *argv[]) {
printf("Hello Uganda \n\n");
printf("%s \n", argv[0]);
printf("%s \n", argv[1]);
printf("%s \n", argv[2]);
printf("%s \n", argv[3]);
return 0; }
To run, first go to the execute menu, select parameters, then a
dialog box comes…enter a sentence (the arguments) and click
ok. Then run your program normally.
.
.
#include<stdio.h>
.
int main (int argc, char *argv[]) {
printf("Hello Uganda \n\n"); //using a loop

for (int i = 1; i < argc; i++) {


printf("%s \n", argv[i]);
}
return 0; }
To run, first go to the execute menu, select parameters,
then a dialog box comes…enter a sentence and click on
Then run your program normally.
.
.

- Uses the header file stdlib.h


.
#include<stdio.h>
#include<stdlib.h>
int main ( int argc, char *argv[]) {
int a = atoi(argv[1]);
int b = atoi(argv[2]);
int sum = a+b;
printf("\n\n %d", sum);
return 0;
} // allow the user to capture 8 arguments and convert them to float and output them on the screen
MEMORY ALLOCATION
Version Simple
mall
.
malloc
void *malloc(size_t size);

Allocates memory and returns a pointer to that


memory
•If the memory can’t be found, NULL is returned.
#include <stdio.h>
#include <stdlib.h>
int main() { // declare 2 pointers
int *A, *B; // allocate memory for the pointers
A = (int *)malloc(sizeof(int));
B = (int *)malloc(sizeof(int));
// Store the int 5 where A is pointing to
*A = 5; // *A = 5 is not similar to A = 5
// Store the int 17 where B is pointing to
*B = 17;
}
• Extra notes on pointers and dynamic arrays
• The return type of malloc is void*
• This means that the return type for malloc MUST be casted
–To what?
»To the type of pointer that will be pointing to the allocated memory.
• What is the reason?
–malloc is used to allocate memory for all types of structures
–If malloc only returned an int *, for example, then we couldn’t
use it to allocate space for a character array
• So malloc simply returns a memory location
–it doesn’t specify what will be stored there.
#include <stdio.h> #include <stdlib.h>
int main() {
int *p, size;
printf(“Enter size”); scanf(“%d” , &size); // if size is 3
p = (int *) malloc(size * sizeof(int));
printf(“Enter the first element”);
Scanf(“%d” , &p[0]) //coz pointer name and array name are related, remember
printf(“Enter the second element”);
Scanf(“%d” , &p[1])
printf(“Enter the third element”);
Scanf(“%d” , &p[2])
//now print the elements
printf(“First %d\n”, p[0]);
printf(“Second %d\n”, p[1]);
printf(“Third %d\n”, p[2]);
// Close the file and free memory.
free(p);
}
int *p, size;
printf(“Enter size”); scanf(“%d” , &size); // if
size is 3
p = (int *) malloc(size * sizeof(int));
printf(“Enter the first element”);
scanf(“%d” , &p[0]) //coz pointer name and
array name are related, remember
printf(“Enter the second element”);
Scanf(“%d” , &p[1])
printf("How many numbers do you want to pick?\n");
scanf("%d", &numVals);
int* values = (int*)malloc(numVals*sizeof(int)); // assume numVals is 3
values[0] = 4; values[1] = 50; values[2] = 40;
int extra = 2;
values = (int*)realloc(values,(numVals+extra)*sizeof(int)); // extra is 2
values[0+numVals] = 60; values[1+numVals] = 65;
numVals = numVals + extra; // 3+ 2

printf("%d \n ", values[0]); // 0 to numVals-1


printf("%d \n ", values[1]);
printf("%d \n ", values[2]);
printf("%d \n ", values[3]);
printf("%d \n ", values[4]);
ma

You might also like