1a.String handling functions: Function strcmp(const char *s1, const char *s2) strcpy(char *s1, const char *s2) strlen(const char *) strcat(char *s1, Data type returned int Task Compare two strings lexicographically. Return a negative value if s1<s2; 0 if s1 and s2 are identical; and a positive value if s1>s2. char* Copy string s2 to string s1. int Return the number of characters in string s. char* Concatenation s2 is appended at the end of s1. const char *s2) strlwr(char *s) char* Converts the string s to lowercase. strupr(char *s) char* Converts the string s to uppercase. strrev(char *s) char* Reverses the string s. *strncpy(char *s1, const char *s2, size_t n) strcmpi(const char *s1, const char *s2) char* int The strncpy() function is similar to strcpy(), except that not more than n bytes of s2 are copied. Thus, if there is no null byte among the first n bytes of s2, the result will not be null-terminated. Does a case insensitive comparison between two strings. //Program to demonstrate 1 st four functions char s1[20],s2[20],s3[50]; int len; printf("enter two strings\n"); scanf("%[^\n] %[^\n]",s1,s2); len=strlen(s1); printf("length of %s=%d\n",s1,len); strcpy(s3,s1); printf("s3=%s\n",s3); if(strcmp(s1,s2)==0) printf("s1 and s2 are equal\n"); else if(strcmp(s1,s2)>0) printf("s1 is greater than s2\n"); Prepared by Aravinda Kasukurthi, CSE, RVRJCCE 1
else printf("s2 is greater than s1\n"); strcat(s3,s2); printf("concatenated string=%s\n",s3); 1b. char str[20]; int l,j,flag=1,i; printf("enter any string\n"); scanf("%s",str); for(l=0;str[l]!='\0';l++) ; printf("given string in reverse order\n"); for(i=l-1;i>=0;i--) putchar(str[i]); for(l=l-1,j=0;j<l;l--,j++) if(str[l]!=str[j]) flag=0; break; if(flag==1) printf("\n%s is palindrome\n",str); else printf("\n%s not palindrome\n",str); 2. Storage classes: The storage class determines the part of memory where storage allocated for a variable and how long the storage allocation continues to exist. Storage classes are of 4 types: 1. Automatic variables 2. Static variables 3. Register variables 4. External variables Prepared by Aravinda Kasukurthi, CSE, RVRJCCE 2
Storage specifier auto static register extern Description Storage place: CPU Memory Initial value: garbage Scope: local Lifetime: within the function only Storage place: CPU Memory Initial value: zero Scope: local Lifetime: Retains the value of the variable between different function calls. Storage place: CPU Registers Initial value: garbage Scope: local Lifetime: within the function only Storage place: CPU Memory Initial value: zero Scope: global Lifetime: Till end of the main program. Variable definition might be anywhere in the C program. Automatic variables: int i; void autov(); for(i=1;i<=3;i++) autov(); void autov() auto int x=0; x=x+1; printf("x=%d\n",x); Output: x=1 x=1 Prepared by Aravinda Kasukurthi, CSE, RVRJCCE 3
x=1 When i=1, autov() function is called for 1 st time: Because of the initial value of the automatic variables is garbage, explicitly assigning 0 to x. When x=x+1, x is incremented by 1. Then x=1 is printed. When i=2, autov() function is called for 2 nd time: Because Automatic variable s life time is local and initial value of the automatic variables is garbage, again explicitly assigning 0 to x. When x=x+1, x is incremented by 1. Then x=1 is printed. Static variables: int i; void staticv(); for(i=1;i<=3;i++) staticv(); void staticv() static int x; x=x+1; printf("x=%d\n",x); Output: x=1 x=2 x=3 When i=1, staticv() function is called for 1 st time: Because of the initial value of the static variables is zero (0), there is no need to explicit assignment of x. When x=x+1, x is incremented by 1. Then x=1 is printed. When i=2, staticv() function is called for 2 nd time: Because of static variable s life time is throughout the program, x value is retained as 1. When x=x+1, then x=1+1=2. Then x=2 is printed. Register variables: Prepared by Aravinda Kasukurthi, CSE, RVRJCCE 4
register int i; void regv(); for(i=1;i<=3;i++) regv(); void regv() register int x=0; x=x+1; printf("\nx=%d",x); Output: x=1 x=1 x=1 When i=1, regv() function is called for 1 st time: Because of the initial value of the register variables is garbage, explicitly assigning 0 to x. When x=x+1, x is incremented by 1. Then x=1 is printed. When i=2, regv() function is called for 2 nd time: Because Register variable s life time is local and initial value of the automatic variables is garbage, again explicitly assigning 0 to x. When x=x+1, x is incremented by 1. Then x=1 is printed. Extern variables: extern int y; // external declaration void globe1(); void globe2(); globe1(); y=y+10; printf("in main y=%d\n",y); globe2(); void globe1() y=y+1; printf("in globe1 y=%d\n",y); int y; //definition Prepared by Aravinda Kasukurthi, CSE, RVRJCCE 5
void globe2() y=y*5; printf("in globe2 y=%d\n",y); Output: In globe1 y=1 In main y=11 In globe2 y=55 Here, y is a global variable and by default initial value of global variable is zero (0). But global variable y is declared after the and globe1() functions. So, y is unavailable to those two functions even it is declared as global variable. That variable y is made available to those two functions by declaring that variable as external variable. extern int y; //external declaration (no memory is allocated to this variable). This declaration informs the compiler, that y is declared as external variable and that variable is declared as a global variable somewhere in the program. int y; //definition When this statement is encountered in the program, compiler allocates memory to that variable. 3a. Function calling mechanisms: 1. Call by value 2. Call by reference Call by value: When the function is called by value, then the actual parameters are just copied to the formal parameters. So, any operations performed on formal parameters do not reflect in the actual parameters. Here is an example to swap two numbers using call by value. In the function definition variables, the formal parameters are changed. Nut those changes are not reflected to calling function. After completion of the swap function, the actual values remain same. int a,b; Prepared by Aravinda Kasukurthi, CSE, RVRJCCE 6
void swap(int,int); printf("enter a,b valuea\n"); scanf("%d%d",&a,&b); printf("before swaping a=%d\tb=%d\n",a,b); swap(a,b); printf("after swaping a=%d\tb=%d\n",a,b); void swap(int x,int y) int t=x; x=y; y=t; Output: Enter a,b values 4 5 Before swaping a=4 b=5 After swaping a=4 b=5 Call by reference: When we call the function using call by reference mechanism, the addresses of actual parameters are passed as arguments to the function call. In call by reference, the operation performed on formal parameters, affects the value of actual parameters because all the operations performed on the value stored in the address of actual parameters. Here is an example to swap two numbers using call by reference by passing addresses of variables a and b. The values of the variables have been changed after calling the swap() function because the swap happened on the addresses of the variables a and b. int a,b; void swap(int*,int*); printf("enter a,b values\n"); scanf("%d%d",&a,&b); printf("before swaping a=%d\tb=%d\n",a,b); swap(&a,&b); printf("after swaping a=%d\tb=%d\n",a,b); Prepared by Aravinda Kasukurthi, CSE, RVRJCCE 7
void swap(int *x,int *y) int t=*x; *x=*y; *y=t; Output: Enter a,b values 4 5 Before swaping a=4 b=5 After swaping a=5 b=4 3b. Recursion: When the function called by itself, then that function is called recursion. Recursion function must contain termination condition. int a,b; int gcd(int,int); printf("enter 2 numbers:\n"); scanf("%d%d",&a,&b); printf("\ngreatest Common Divisor is %d\n",gcd(a,b)); int gcd(int a,int b) if(b==0) return a; else return gcd(b,a%b); 4a. Dynamic memory allocation: The process of allocating memory at run time is known as dynamic memory allocation. Dynamic memory allocation functions: 1. malloc() Prepared by Aravinda Kasukurthi, CSE, RVRJCCE 8
malloc(): 2. callc() 3. realloc() 4. free() Allocates requested size of bytes and returns a pointer to the first byte of the allocated space. The malloc function reserves a block of memory of specified size and returns a pointer type of void. This means that we can assign it to any type of pointer. Initial (default) values in the allocated memory are garbage. General form: ptr=(cast type *)malloc(byte size); Example: Int *x; x=(int *)malloc(10*sizeof(int)); The function reserves a block of memory whose size (in bytes) is equivalent to 10 integer quantities. The function returns a pointer to an integer. This pointer indicates the beginning of the memory block. calloc(): Calloc allocates multiple blocks of memory each of same size and sets all bytes to zero. (Initial values in the allocated memory are zero.) - where ptr is a pointer of type cast type. - The malloc returns pointer (of cast type) to an area of memory with size byte size. General form: ptr=(cast type *)calloc(n,byte size); realloc(): Example: Int *x; x=(int *)malloc(10,sizeof(int)); Prepared by Aravinda Kasukurthi, CSE, RVRJCCE 9
If the previously allocated memory is insufficient or more than required, you can change the previously allocated memory size using realloc(). General form: ptr=(cast type *)relloc(ptr, n*byte size); Here, ptr is reallocated with size of byte size (new size). free(): Already allocated memory is deallocated for further use of the memory location by another variables. General form: free(pointer); Example: free(ptr); //Example Program #include <stdio.h> #include <stdlib.h> int *ptr, i, n1, n2,*ptr1; printf("enter size of array: "); scanf("%d", &n1); ptr = (int*) calloc(n, sizeof(int)); ptr1 = (int*) malloc(n1 * sizeof(int)); printf("enter array elements\n"); for(i=0;i<n1;i++) scanf("%d",(ptr+i)); printf("array elements are\n"); for(i=0;i<n1;i++) printf("%d\t",*(ptr+i)); printf("\nenter new size of array: "); scanf("%d", &n2); ptr = (int*)realloc(ptr, n2*sizeof(int)); if(n2>n1) printf("enter %d array elements\n",n2-n1); Prepared by Aravinda Kasukurthi, CSE, RVRJCCE 10
for(i=n1;i<n2;i++) scanf("%d",(ptr+i)); for(i = 0; i < n2; ++i) printf(" %d\t",*(ptr + i)); free(ptr); free(ptr1); 4b. int *a,n,i,pos=-1; void bsort(int *,int); printf("enter n value\n"); scanf("%d",&n); a=(int *)malloc(n*sizeof(int)); printf("\nenter array elements\n"); for(i=0;i<n;i++) scanf("%d",a+i); bsort(a,n); printf("array elements after sorting\n"); for(i=0;i<n;i++) printf("%d\t",*(a+i)); void bsort(int *a,int n) int i,j,t; for(i=0;i<n;i++) for(j=0;j<n-i-1;j++) if(*(a+j) > *(a+j+1)) t=*(a+j); *(a+j)=*(a+j+1); *(a+j+1)=t; Prepared by Aravinda Kasukurthi, CSE, RVRJCCE 11
5a. 1 2 3 Structure Structure is a user defined data type and Heterogeneous. Structure has members with different data types. Syntax: structure <tagname> datatype1 member1; datatype2 member2;... datatypen membern; ; Structure members are accessed through structure variable and dot operator. In structure each member has separate space in memory. Take below example. struct student int rollno; char gender; float marks; s1; The total memory required to store a structure variable is equal to the sum of size of all the members. In above case 9 bytes (4+1+4) will be required to store structure variable s1. We can access any member in any sequence. s1.rollno = 20; Union Union is a user defined data type and Heterogeneous. Union has members with different data types. Syntax: union <tagname> datatype1 member1; datatype2 member2;... datatypen membern; ; Union members are accessed through Union variable and dot operator. In union, all members share the same memory space. This is the biggest difference between structure and union. union student int rollno; char gender; float marks; s1; In above example variable marks is of float type and have largest size (4 bytes). So the total memory required to store union variable s1 is 4 bytes. We can access only that variable whose value is recently stored. s1.rollno = 20; Prepared by Aravinda Kasukurthi, CSE, RVRJCCE 12
4 s1.marks = 90.0; printf( %d,s1.rollno); The above code will work fine but will show erroneous output in the case of union. All the members can be initialized while declaring the variable of structure. s1.marks = 90.0; printf( %d,s1.rollno); The above code will show erroneous output. The value of rollno is lost as most recently we have stored value in marks. This is because all the members share same memory space. Only first member can be initialized while declaring the variable of union. In above example we can initialize only variable rollno at the time of declaration of variable. 5b. Pointer: A pointer is a variable that contains an address which is a location of another variable in memory. A pointer is a variable; its value is also stored in the memory in another location. Declaring and initializing pointers: datatype *ptrname; This tells the compiler 3 things about the variable ptrname. i. The asterisk (*)tells that the vaiable ptr_name is a pointer variable. ii. Ptrname needs a memory location. iii. Ptrname points to a variable of type data type. Example: int *p; declares the variable p as a pointer variable that points to an integer data type. int a,*p; p=&a; a=10; Prepared by Aravinda Kasukurthi, CSE, RVRJCCE 13
6a. We assign the address of 'a' to a variable. The address of a=5000 and p=5048. Since the value of the variable p is the address of the variable a, we may access the value of 'a' by using the value of p. Therefore, the variable p points to the variable a. Program to illustrate pointer arithmetic (expressions): int x,y,*p1,*p2,s1,s2,s3; p1=&x; p2=&y; printf("enter x,y values\n"); scanf("%d%d",&x,&y); s1 = *p1 * *p2; s2 = x + *p2; s3 = *p2 / *p1-5; printf("s1=%d\ns2=%d\ns3=%d\n",s1,s2,s3); Output: Enter x,y values 4 6 s1=24 s2=10 s3=-4 I am giving only table, remaining from material. Function description Prepared by Aravinda Kasukurthi, CSE, RVRJCCE 14
fopen() fclose() fgetc() fputc() fscanf() fprintf() getw() putw() fseek() ftell() rewind() create a new file or open a existing file closes a file reads a character from a file writes a character to a file reads a set of data from a file writes a set of data to a file reads a integer from a file writes a integer to a file set the position to desire point gives current position in the file set the position to the beginning of a file Prepared by Aravinda Kasukurthi, CSE, RVRJCCE 15