echo '' ;

C Library – stdlib.h

stdlib.h in standard C Library header file that provides various functions to perform general utility tasks. It includes functions for memory allocation, process control, random number generation, string conversion, and other fundamental operations.

Key functions include malloc() for dynamic memory allocation, free() to release allocated memory, exit() for program termination, atoi() for converting strings to integers, and rand() for generating pseudo-random numbers.

Read more: C Library – stdlib.h

Additionally, <stdlib.h> declares the NULL macro for null pointers and defines the EXIT_SUCCESS and EXIT_FAILURE constants for program termination status.

Overall, <stdlib.h> is essential for many C programs, offering foundational functionalities crucial for memory management, program flow control, and data conversion.

Functions List

functions are in below table, Refer this link for more info

FunctionDescription
abort()Stop a Program.
abs()Calculate Integer Absolute Value.
atexit()Record Program Ending Function.
atof()Convert Character String to Float
atoi()Convert Character String to Integer
atol() & atoll()Convert Character String to Long or Long Long Integer
bsearch()Search Array
calloc()Reserve and Initialize Storage
_C_Quickpool_Debug()Modify Quick Pool Memory Manager Characteristics
_C_Quickpool_Init()Initialize Quick Pool Memory Manager.
_C_Quickpool_Report()Generate Quick Pool Memory Manager Report
div()Calculate Quotient and Reminder
exit()End Program
free()Release Storage Blocks
_gcvt1()Convert Floating Point to String
getenv()Search for Enviroment Variable
_itoa1()Convert Integer to String
_ltoa1()Convert Integer to String
labs()Calculate absolute value of long and long long Integer
llabs()Calculate absolute value of long and long long Integer
ldiv()Perform Long and Long Long Division
lldiv()Perform Long and Long Long Division
malloc()Reserve Storage Block
mblen()Determine Length of Multibyte Character
mbstowcs()Convert Multibyte String to a Wide Character String
mbtowc()Convert Multibyte Character to a Wide Character
putenv()Change/Add Environmental Variable
qsort()Sort Array
rand()Generate Random Number
rand_r()Generate Random Number
realloc()Change Reserved Storage Block Size
srand()Set Seed fr rand() Function
strtod()Convert Character String to Double, Float, and Long Double
strtod32()Convert Character String to Decimal Floating-Point
strtod64()Convert Character String to Decimal Floating-Point
strtod128()Convert Character String to Decimal Floating-Point
strtof()Convert Character String to Double, Float, and Long Double
strtol()Convert Character String to Long and Long Long Integer
strtold()Convert Character String to Double, Float, and Long Double
strtoll()Convert Character String to Long and Long Long Integer
strtoul()String to unsigned Long Integer
strtoull()String to Unsigned Long Long Integer
system()Execute a command
_ultoa1()Convert Unsigned Long Integer to String.
wcstombs()Convert a Wide Character to a Multibyte Character String
wctomb()Convert a Wide Character to a Multibyte Character

atoi()

  • atoi function will convert character string to integer.
  • Function format should be like below, and <stdiib.h> required in order to use the atoi()
#include <stdlib.h>
int atoi(const char *string);

Example

#include <stdlib.h>
#include <stdio.h>
 
int main(void)
{
    int i;
    char *s;
 
    s = " -555";
    i = atoi(s);     /* i = -555 */
 
    printf("i = %d\n",i);
}
 
/*******************  Output should be similar to:  ***************
 
i = -555
*/


abs()

  • abs function will calculate integer absolute value from the given value.
  • Given value should from number system.
  • Function format should be like below, and <stdiib.h> required in order to use the abs()
int abs(int n);
long abs(long n);                    /* C++ only */

#include <math.h>

double abs(double n);                /* C++ only */
float abs(float n);                  /* C++ only */
long double abs(long double n);      /* C++ only */
float absf(float n);
long double absl(long double n);

Example

#include <stdio.h>
#include <stdlib.h>
int main()
{
   int GivenValue_1 = abs(200);
   int GivenValue_2 = abs(-400);
 
   printf("Absolute value of GivenValue_1 = %d\n", GivenValue_1);
   printf("Absolute value of GivenValue_2 = %d \n",GivenValue_2);
   return 0;
}

/* Output
Absolute value of m = 200
Absolute value of n = 400
*/


system()

If you have the following questions

  • How to run exe file in c?
  • How to run exe file with another C program?

Then here your answers.

  • In C system() is a execute command is windows platform.
  • You can execute any kind of .exe files using system().

Syntax

Syntax is int system(const char *EXECUTE_COMMAND) 

here EXECUTE_COMMAND is your preferred runnable application (must be a .exe file)

  • Rule-1 (Declaration): Function name should be lowercase example : system(). C program is a case sensitive language. If you by mistakenly entered System() or SYSTEM() or sYSTEM() or anything other than system(). then compiler will generate Error Undefined reference to <Specified_function_name>.
  • Rule-2 (Parameter): EXECUTE_COMMAND should be PATH of execution file name with .exe or directly enter the file name with extension as .exe(Incase your calling in same PATH) . system() argument should be within double code "" as character type. Example: system("ArunEworld"); . Else compiler will generate Error EXECUTE_COMMAD is undeclare.
  • Rule-3 (Ends): The function should be end with semicolon ; , In C language function call should be end with ; Semicolon.
  • Note(Return Value): system() returned is -1 on error, and the return status of the command otherwise.

Example-1: Run another C program(.exe) using C program in same directory

Here first i’m going to create a new C program as ArunEworld.c, then i will generate the ArunEworld.exe file for that c program using compiler(you can use any compiler, But here used Dev C++ GCC compiler). Generated ArunEworld.exe file going to call in another C program as AEW_SystemFun_Example-1.c .

Directory

FilesDirectory (Folder PATH)
ArunEworld.c
ArunEworld.exe
D:\C_Example\stdlib\SystemFun\Example-1\
AEW_SystemFun_Example-1.cD:\C_Example\stdlib\SystemFun\Example-1\

Below are the codes,

ArunEworld.c

#include <stdio.h>
int main()
{
	return printf("www.ArunEworld.com");
}

Output (ArunEworld.c): www.ArunEworld.com

Generated (ArunEworld.c): ArunEworld.exe

AEW_SystemFun_Example-1.c

#include <stdlib.h>
int main()
{
    return system("ArunEworld.exe");
}

Output(AEW_SystemFun_Example-1.c):

www.ArunEworld.com
--------------------------------
Process exited after 1.77 seconds with return value 18
Press any key to continue . . .

Explanation

  • If you observe the output, return value 18. What it means? our exe file “ArunEworld.exe” file return the 18 value, because printf() returns number of char printed (here "www.ArunEworld.com" is 18 characters). So that will be passed to system() function return value, when successfully run the ArunEworld.exe file.

Example-2: Run another C program(.exe) using C program in different directory

Consider the Example-1 : ArunEworld.c file and Newly going to create a C program as AEW_SystemFun_Example-2.c files are in different directory. In this case you need to mention the PATH as system() parameter.

Directory

FilesDirectory (Folder PATH)
ArunEworld.c
ArunEworld.exe
D:\C_Example\stdlib\SystemFun\Example-1\
AEW_SystemFun_Example-2.cD:\C_Example\stdlib\SystemFun\Example-2\

AEW_SystemFun_Example-2.c

#include <stdlib.h>
int main()
{
    return system("D:\C_Example\stdlib\SystemFun\Example-1\ArunEworld.exe");
}

Output(AEW_SystemFun_Example-1.c):

www.ArunEworld.com
--------------------------------
Process exited after 1.77 seconds with return value 18
Press any key to continue . . .

Example-3: Run any installed windows application using C.

If you have the questions like How to run notepad file using c program? Here your solution. (You can run any installed application)

#include <stdlib.h>
int main()
{
    return system("notepad.exe");;
}

Output: blank Notepad will be open.

Note: If you want to open other applications like Microsoft office product or other product, you need to mention the actual .exe file directory PATH.

Example-4: List down all the files and directories in the current directory Using C Program(Windows Platform).

Code

#include <stdio.h>
#include <string.h>

int main () {
   char AEW_Directory_List[50];

   strcpy(AEW_Directory_List, "dir");
   return system(AEW_Directory_List);
} 

Output

Volume in drive D is DATA

Directory of D:\C_Example\stdlin\SystemFun\Exampel-4

25-04-2021  23:55    <DIR>          .
25-04-2021  23:55    <DIR>          ..
25-04-2021  23:55               145 AEW_SystemFun_Example-4.c
25-04-2021  23:55           127,698 AEW_SystemFun_Example-4.exe
25-04-2021  23:52                83 ArunEworld.c
25-04-2021  23:53           126,684 ArunEworld.exe
               4 File(s)        254,610 bytes
               2 Dir(s)  418,645,946,368 bytes free

--------------------------------
Process exited after 2.877 seconds with return value 0
Press any key to continue . . .

Example-5: List down all the files and directories in the current directory Using C Program(Linux Platform).

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main () {
   char AEW_Directory_List[50];

   strcpy( AEW_Directory_List, "ls -l" );
   
   return system(AEW_Directory_List);;
} 

Example-6: Run Chrome Browser in Windows platform using C Program

Wrong Method

Directly provide the actual application path some time wont work.

Wrong Method: Code

The following code wont be run the chrome application.

#include <string.h>

int main(void) 
{
   return(system("C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"));
} 

Wrong Method: Output:

'C:Program' is not recognized as an internal or external command,
operable program or batch file.

--------------------------------
Process exited after 1.555 seconds with return value 1
Press any key to continue . . .

Wrong Method: Explanation

The given directory PATH : "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" contains Space in Program File directory. In C Space will be considered as termination. So system() will consider only “C:\Program” as a PATH and search for runnable application. So it will generate the Error.

Correct Method

In order to run the chrome.exe application using actual path :

Its wont work, you can clear with above code. So here I’m doing some small trick. Copying the chrome.exe shortcut into D:\ Directory as well as change the file name from "Google Chrome.lnk" to "GoogleChome.lnk". So that we can easily open the chrome.

Correct Method: Code

The following code will run the chrome application

#include <stdio.h>
#include <string.h>

int main(void) 
{
   return(system("D:\GoogleChrome.lnk"));
} 

Correct Method: Output

  • Chrome Application will open newly.

Example-7: Run Windows Command Prompt using C Program

If you want to access the windows commands using the C program? it’s very easy to do. For that, you need to include predecessor (stdlib.h) . See the Below code

Run HELP in cmd prompt

#include<stdlib.h> 
void main()
{
	system("C:\\Windows\\System32\\help");
}

Run IPCONFIG in cmd prompt

#include<stdlib.h> 
void main()
{
	system("C:\\Windows\\System32\\ipconfig");	
}


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Discover more from ArunEworld

Subscribe now to keep reading and get access to the full archive.

Continue reading