The document discusses input and output functions in C for reading, writing, and processing data. It covers the getchar() function for reading a character, the gets() function for reading a string, and the printf() and putchar() functions for writing output. It also discusses dynamic memory allocation functions like malloc(), calloc(), and free() for allocating memory during runtime.
2. INTRODUCTION Reading, writing & processing are the 3 essential function Inputting data 2 types assignment (a==5) Through functions(scanf(),getchar() etc) Output only functions (printf(),putchar()..etc Header file for input & output functions #include<stdio.h>
3. Reading a chara using getchar getchar() read one chara at a time Syntax variable name=getchar(); Wait until the key is pressed,then value inputted will assigned to the variable Example name= getchar();
4. program #include<stdio.h> main() { char answer; Printf(“would you like to know my name?”); Printf(“Type Y for yes & N for no”); answer=getchar(); If(answer==Y !! answer== y) Printf(“my name is busy bee”); else printf(“you are good for nothing”); }
6. gets Format #include<stdio.h> char*gets(char*str); Read character from the input and places in array pointed by the string Read until EOF(End off File) or “” is generated Reading successfully *str return Failure return NULL No limit of no of characters
7. program #include< stdio.h> #include<stdlib.h> Int main void() { FILE *fp; Char fname[128]; Printf(“enter the filename”); gets(filename); If((fp=fopen(fname,”r”))==NULL){ Printf(“cannot open the file”); exit(1); } fclose(fp); Return0; }
8. printf Format #include<stdio.h> intprintf(const char*format,…..); Write stdoutarg Format consist 2 item character the way the arg defined
9. Cont………. Printf(“Hi %c%d%s”,’c’’10’,”there”); Same no of format &arg in order Insufficient arg =output undefined More=remind discarded Printf() return=return the no of character printed If it –ve= error occured
10. Field width Integer between % and format code Indicate min length %5d,%7s..etc %05d=min length 5with 0
11. Precision modifier Places after field width Indicate decimal point(in floating point Integer=min no of digit String=max.length
13. Right & left justified If field width higher than data=data places on the right side of the field (default) Least justify=force data to print on left side,can be done by giving –ve %-10.2=left justified 10 char with 2 decimal point
15. Format modifier 2 type of modifier long & short %ld=long to be displayed %hu=short inttobe displayed
16. %n-command It causes= generation of no. of char written Pointer to be specified in the arg –list Example int; printf(“This is a test%n”,&i); printf(“%d”,i); Output “this is a test”
17. Putchar() Format #include<stdio.h> intputchar(intch); Write into the stdout Example for(; *str;str++) putchar(*str)
18. puts Format #include<stdio.h> int puts(const char *str); write string pointed NULL char=newline Return non-negative=successful writing EOF =failure
20. Scanf() Format #include<stdio.h> intscanf(constchar * format…….) Read stdin & store in variable pointed by arg list can read the datatype & converts into internal function
23. Basic Idea situations where data is dynamic in nature. Amount of data cannot be predicted beforehand. Number of data item keeps changing during program execution. Such situations can be handled more easily and effectively using dynamic memory management techniques.
24. CONT……….. C language requires the number of elements in an array to be specified at compile time. Often leads to wastage or memory space or program failure. Dynamic Memory Allocation Memory space required can be specified at the time of execution. C supports allocating and freeing memory dynamically using library routines.
25. Memory Allocation Process in C Local variables Stack Free memory Heap Global variables Permanent storage area Instructions
26. The program instructions and the global variables are stored in a region known as permanent storage area. The local variables are stored in another area called stack. The memory space between these two areas is available for dynamic allocation during execution of the program. This free region is called the heap. The size of the heap keeps changing
27. Memory Allocation Functions malloc: Allocates requested number of bytes and returns a pointer to the first byte of the allocated space. calloc: Allocates space for an array of elements, initializes them to zero and then returns a pointer to the memory. free : Frees previously allocated space. realloc: Modifies the size of previously allocated space.
28. Dynamic Memory Allocation used to dynamically create space for arrays, structures, etc. int main () { int *a ; int n; .... a = (int *) calloc (n, sizeof(int)); .... } a = malloc (n*sizeof(int));
29. void read_array (int *a, int n) ; int sum_array (int *a, int n) ; void wrt_array (int *a, int n) ; int main () { int *a, n; printf (“Input n: “) ; scanf (“%d”, &n) ; a = calloc (n, sizeof (int)) ; read_array (a, n) ; wrt_array (a, n) ; printf (“Sum = %d”, sum_array(a, n); }
30. void read_array (int *a, int n) { int i; for (i=0; i<n; i++) scanf (“%d”, &a[i]) ; } void sum_array (int *a, int n) { int i, sum=0; for (i=0; i<n; i++) sum += a[i] ; return sum; } void wrt_array (int *a, int n) { int i; ........ }