Apply for Zend Framework Certification Training

c

< Top 20 Questions of Predict output on file handling in c Last >




Standard C Libraries with Examples
C provides many standard libraries that contain ready-made functions. Instead of writing everything from scratch, we include the required header file using #include.
The six important standard C libraries are:
Header File                    Main Purpose                              Common Functions
stdio.h                          Input and output                          printf(), scanf(), getchar(), putchar()
stdlib.h                         General utilities                            malloc(), free(), rand(), atoi(), exit()
string.h                         String handling                            strlen(), strcpy(), strcmp(), strcat()
math.h                         Mathematical operations                sqrt(), pow(), ceil(), floor()
ctype.h                         Character checking/conversion      isalpha(), isdigit(), toupper(), tolower()
time.h                          Date and time                              time(), localtime(), clock()
1. stdio.h — Standard Input/Output
stdio.h stands for Standard Input Output. It is used for taking input from the user and displaying output.
Important functions
printf()            – displays output
scanf()            – takes formatted input
getchar()         – reads one character
putchar()         – displays one character
puts()              – displays a string
fgets()             – reads a string/line
Example
#include <stdio.h>
int main(){
    int age;
    printf("Enter your age: ");
    scanf("%d", &age);
    printf("Your age is %d", age);
    return 0;
}
Sample Input
20
Sample Output
Enter your age: 20
Your age is 20
Character example
#include <stdio.h>
int main(){
    char ch;
    printf("Enter a character: ");
    ch = getchar();
    printf("You entered: ");
    putchar(ch);
    return 0;
}
2. stdlib.h — Standard Utility Library
stdlib.h provides functions for memory allocation, random numbers, number conversion, program control, etc.
Important functions
Function              Purpose
malloc()              Allocates memory
calloc()               Allocates and initializes memory
realloc()             Changes allocated memory size
free()                 Releases allocated memory
rand()                Generates random number
srand()              Sets random number seed
atoi()                Converts string to integer
atof()                Converts string to floating point
exit()                Terminates program
abs()                Returns absolute value
Example: abs()
#include <stdio.h>
#include <stdlib.h>
int main(){
    int n = -25;
    printf("Absolute value = %d", abs(n));
    return 0;
}
Output
Absolute value = 25
Example: atoi()
#include <stdio.h>
#include <stdlib.h>
int main(){
    char str[] = "123";
    int n = atoi(str);
    printf("Number = %d", n);
    return 0;
}
Output
Number = 123
Example: Dynamic Memory
#include <stdio.h>
#include <stdlib.h>
int main(){
    int *p;
    p = (int *)malloc(5 * sizeof(int));
    if (p == NULL)    {
        printf("Memory allocation failed");
        return 1;
    }
    for (int i = 0; i < 5; i++)
        p[i] = (i + 1) * 10;
    for (int i = 0; i < 5; i++)
        printf("%d ", p[i]);
    free(p);
 
    return 0;
}
Output
10 20 30 40 50
3. string.h — String Handling
string.h provides functions for working with strings and memory blocks.
Important functions
Function Purpose
strlen() Finds string length
strcpy() Copies a string
strncpy() Copies limited characters
strcat() Joins two strings
strcmp() Compares two strings
strchr() Searches for a character
strstr() Searches for a substring
Example: strlen()
#include <stdio.h>
#include <string.h>
int main(){
    char name[] = "Aman";
    printf("Length = %zu", strlen(name));
    return 0;
}
Output
Length = 4
Example: strcpy()
#include <stdio.h>
#include <string.h>
int main(){
    char source[] = "Hello";
    char destination[20];
    strcpy(destination, source);
    printf("%s", destination);
    return 0;
}
Output
Hello
Example: strcat()
#include <stdio.h>
#include <string.h>
int main(){
    char first[30] = "Good ";
    char second[] = "Morning";
    strcat(first, second);
    printf("%s", first);
    return 0;
}
Output
Good Morning
Example: strcmp()
#include <stdio.h>
#include <string.h>
int main(){
    char str1[] = "apple";
    char str2[] = "apple";
    if (strcmp(str1, str2) == 0)
        printf("Strings are equal");
    else
        printf("Strings are different");
    return 0;
}
Output
Strings are equal
4. math.h — Mathematical Functions
math.h provides functions for performing mathematical calculations.
Important functions
Function Purpose Example
sqrt() Square root sqrt(25) → 5
pow() Power pow(2,3) → 8
ceil() Rounds upward ceil(4.2) → 5
floor() Rounds downward floor(4.8) → 4
fabs() Absolute value of floating point fabs(-4.5) → 4.5
sin() Sine sin(x)
cos() Cosine cos(x)
tan() Tangent tan(x)
log() Natural logarithm log(x)
log10() Base-10 logarithm log10(x)
Example: sqrt()
#include <stdio.h>
#include <math.h>
int main(){
    double n = 25;
    printf("Square root = %.2f", sqrt(n));
    return 0;
}
Output
Square root = 5.00
Example: pow()
#include <stdio.h>
#include <math.h>
int main(){
    double result;
    result = pow(2, 5);
    printf("Result = %.0f", result);
    return 0;
}
Output
Result = 32
Example: ceil() and floor()
#include <stdio.h>
#include <math.h>
int main(){
    double n = 5.7;
    printf("Ceil = %.0f\n", ceil(n));
    printf("Floor = %.0f", floor(n));
    return 0;
}
Output
Ceil = 6
Floor = 5
Note: On some compilers, especially GCC, mathematical programs may need to be linked with the math library using -lm, for example: gcc program.c -lm.
5. ctype.h — Character Handling
ctype.h provides functions for testing and converting characters.
Important functions
Function Purpose
isalpha() Checks whether character is alphabet
isdigit() Checks whether character is digit
isalnum() Checks alphabet or digit
isspace() Checks whitespace
islower() Checks lowercase
isupper() Checks uppercase
tolower() Converts to lowercase
toupper() Converts to uppercase
Example: isalpha()
#include <stdio.h>
#include <ctype.h>
int main(){
    char ch = 'A';
    if (isalpha(ch))
        printf("It is an alphabet");
    else
        printf("It is not an alphabet");
    return 0;
}
Output
It is an alphabet
Example: isdigit()
#include <stdio.h>
#include <ctype.h>
int main(){
    char ch = '7';
    if (isdigit(ch))
        printf("It is a digit");
    else
        printf("It is not a digit");
    return 0;
}
Output
It is a digit
Example: toupper() and tolower()
#include <stdio.h>
#include <ctype.h>
int main(){
    char ch = 'a';
    printf("Uppercase = %c\n", toupper(ch));
    printf("Lowercase = %c", tolower('B'));
    return 0;
}
Output
Uppercase = A
Lowercase = b
6. time.h — Date and Time
time.h provides functions for working with date and time.
Important functions
Function Purpose
time() Gets current calendar time
localtime() Converts time to local time
gmtime() Converts time to UTC
clock() Measures processor time
strftime() Formats date/time
difftime() Calculates difference between two times
Example: Current Date and Time
#include <stdio.h>
#include <time.h>
int main(){
    time_t currentTime;
    time(&currentTime);
    printf("Current date and time: %s", ctime(&currentTime));
    return 0;
}
Sample Output
Current date and time: Thu Sep 10 14:30:25 2026
The exact output will depend on the computer's current date and time.
Example: Getting individual date/time components
#include <stdio.h>
#include <time.h>
int main(){
    time_t t;
    struct tm *now;
    time(&t);
    now = localtime(&t);
    printf("Year  : %d\n", now->tm_year + 1900);
    printf("Month : %d\n", now->tm_mon + 1);
    printf("Day   : %d\n", now->tm_mday);
    printf("Hour  : %d\n", now->tm_hour);
    printf("Minute: %d\n", now->tm_min);
    printf("Second: %d\n", now->tm_sec);
    return 0;
}
Quick Revision
stdio.h
printf();
scanf();
getchar();
putchar();
puts();
fgets();
 
Used for: Input and Output
 
stdlib.h
malloc();
calloc();
realloc();
free();
rand();
atoi();
abs();
exit();
 
Used for: Memory, conversion, random numbers, program control
 
string.h
strlen();
strcpy();
strcat();
strcmp();
strchr();
strstr();
 
Used for: String manipulation
 
math.h
sqrt();
pow();
ceil();
floor();
fabs();
sin();
cos();
tan();
log();
 
Used for: Mathematical calculations
 
ctype.h
isalpha();
isdigit();
isalnum();
isspace();
islower();
isupper();
toupper();
tolower();
 
Used for: Character testing and conversion
 
time.h
time();
ctime();
localtime();
gmtime();
clock();
strftime();
difftime();
 
Used for: Date and time operations
 
One program using multiple libraries
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <ctype.h>
#include <time.h>
int main(){
    char name[] = "aman";
    double number = 25;
    printf("Name length: %zu\n", strlen(name));
    printf("Square root: %.2f\n", sqrt(number));
    printf("Uppercase: %c\n", toupper(name[0]));
    printf("Absolute value: %d\n", abs(-50));
    time_t t;
    time(&t);
    printf("Current time: %s", ctime(&t));
    return 0;
}
This single program demonstrates the use of all six standard C libraries.

< Top 20 Questions of Predict output on file handling in c Last >



Ask a question



  • Question:
    {{questionlistdata.blog_question_description}}
    • Answer:
      {{answer.blog_answer_description  }}
    Replay to Question


Back to Top