Show List
Standard Library Functions
The C Standard Library is a collection of functions and macros that provide standard and generic functions for tasks such as input/output, string manipulation, memory allocation, and more. The functions in the C Standard Library are declared in various header files, such as stdio.h
, string.h
, and stdlib.h
, and are available for use in your C programs.
Here are some examples of commonly used C Standard Library functions:
printf
- Theprintf
function is used to print formatted text to the standard output (usually the screen). For example:
cCopy code
#include <stdio.h>
int main() {
printf("Hello, world!\n");
return 0;
}
scanf
- Thescanf
function is used to read formatted input from the standard input (usually the keyboard). For example:
cCopy code
#include <stdio.h>
int main() {
int a;
printf("Enter an integer: ");
scanf("%d", &a);
printf("You entered: %d\n", a);
return 0;
}
strlen
- Thestrlen
function returns the length of a null-terminated string. For example:
cCopy code
#include <string.h>
#include <stdio.h>
int main() {
char str[] = "Hello, world!";
int len = strlen(str);
printf("The length of the string is: %d\n", len);
return 0;
}
malloc
- Themalloc
function is used to dynamically allocate memory at runtime. For example:
cCopy code
#include <stdlib.h>
#include <stdio.h>
int main() {
int n = 5;
int *ptr = (int *) malloc(n * sizeof(int));
if (ptr == NULL) {
printf("Memory allocation failed.\n");
return 1;
}
int i;
for (i = 0; i < n; i++) {
ptr[i] = i * i;
}
printf("The squares of the first %d integers are: ", n);
for (i = 0; i < n; i++) {
printf("%d ", ptr[i]);
}
printf("\n");
free(ptr);
return 0;
}
These are just a few examples of the many functions available in the C Standard Library. By using these functions, you can write powerful and efficient C programs with less code and effort.
Leave a Comment