Functions 2024 Kla Struc 2024
Functions 2024 Kla Struc 2024
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
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
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);