- C Programming Tutorial
- C - Home
- Basics of C
- C - Overview
- C - Features
- C - History
- C - Environment Setup
- C - Program Structure
- C - Hello World
- C - Compilation Process
- C - Comments
- C - Tokens
- C - Keywords
- C - Identifiers
- C - User Input
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Integer Promotions
- C - Type Conversion
- C - Type Casting
- C - Booleans
- Constants and Literals in C
- C - Constants
- C - Literals
- C - Escape sequences
- C - Format Specifiers
- Operators in C
- C - Operators
- C - Arithmetic Operators
- C - Relational Operators
- C - Logical Operators
- C - Bitwise Operators
- C - Assignment Operators
- C - Unary Operators
- C - Increment and Decrement Operators
- C - Ternary Operator
- C - sizeof Operator
- C - Operator Precedence
- C - Misc Operators
- Decision Making in C
- C - Decision Making
- C - if statement
- C - if...else statement
- C - nested if statements
- C - switch statement
- C - nested switch statements
- Loops in C
- C - Loops
- C - While loop
- C - For loop
- C - Do...while loop
- C - Nested loop
- C - Infinite loop
- C - Break Statement
- C - Continue Statement
- C - goto Statement
- Functions in C
- C - Functions
- C - Main Function
- C - Function call by Value
- C - Function call by reference
- C - Nested Functions
- C - Variadic Functions
- C - User-Defined Functions
- C - Callback Function
- C - Return Statement
- C - Recursion
- Scope Rules in C
- C - Scope Rules
- C - Static Variables
- C - Global Variables
- Arrays in C
- C - Arrays
- C - Properties of Array
- C - Multi-Dimensional Arrays
- C - Passing Arrays to Function
- C - Return Array from Function
- C - Variable Length Arrays
- Pointers in C
- C - Pointers
- C - Pointers and Arrays
- C - Applications of Pointers
- C - Pointer Arithmetics
- C - Array of Pointers
- C - Pointer to Pointer
- C - Passing Pointers to Functions
- C - Return Pointer from Functions
- C - Function Pointers
- C - Pointer to an Array
- C - Pointers to Structures
- C - Chain of Pointers
- C - Pointer vs Array
- C - Character Pointers and Functions
- C - NULL Pointer
- C - void Pointer
- C - Dangling Pointers
- C - Dereference Pointer
- C - Near, Far and Huge Pointers
- C - Initialization of Pointer Arrays
- C - Pointers vs. Multi-dimensional Arrays
- Strings in C
- C - Strings
- C - Array of Strings
- C - Special Characters
- C Structures and Unions
- C - Structures
- C - Structures and Functions
- C - Arrays of Structures
- C - Self-Referential Structures
- C - Lookup Tables
- C - Dot (.) Operator
- C - Enumeration (or enum)
- C - Structure Padding and Packing
- C - Nested Structures
- C - Anonymous Structure and Union
- C - Unions
- C - Bit Fields
- C - Typedef
- File Handling in C
- C - Input & Output
- C - File I/O (File Handling)
- C Preprocessors
- C - Preprocessors
- C - Pragmas
- C - Preprocessor Operators
- C - Macros
- C - Header Files
- Memory Management in C
- C - Memory Management
- C - Memory Address
- C - Storage Classes
- Miscellaneous Topics
- C - Error Handling
- C - Variable Arguments
- C - Command Execution
- C - Math Functions
- C - Static Keyword
- C - Random Number Generation
- C - Command Line Arguments
- C Programming Resources
- C - Questions & Answers
- C - Quick Guide
- C - Cheat Sheet
- C - Useful Resources
- C - Discussion
Command Execution in C
Sometimes it may be required that you execute a few OS commands through a C program. To execute system commands, you can use the system() function from the "stdlib.h" header file.
With this system() function, you can invoke Linux/Windows terminal commands. The prototype to use this function is as follows −
system(char *command);
Example
The following code shows the execution of ls command using system() function in C language.
#include <stdio.h> #include<stdlib.h> #include<string.h> int main() { char cmd[10]; strcpy(cmd,"dir C:\\users\\user\\*.c"); system(cmd); return 0; }
Output
Run the code and check its output −
C:\Users\user>dir *.c Volume in drive C has no label. Volume Serial Number is 7EE4-E492 Directory of C:\Users\user 04/01/2024 01:30 PM 104 add.c 04/02/2024 01:37 PM 159 add1.c 04/02/2024 01:37 PM 259 array.c 04/02/2024 01:37 PM 149 main.c 04/02/2024 01:37 PM 180 recursion.c 04/02/2024 01:37 PM 241 struct.c 04/02/2024 01:37 PM 172 voidptr.c 7 File(s) 1,264 bytes 0 Dir(s) 139,073,761,280 bytes
The exec Family of Functions
The exec family of functions have been introduced in the "unistd.h" header file. These functions are used to execute a file, and they replace the current process image with a new process image once they are called.
The execl() Function
The execl() function’s first argument is the executable file as its first argument. The next arguments will be available to the file when it’s executed. The last argument has to be NULL.
int execl(const char *pathname, const char *arg, ..., NULL)
Example
Take a look at the following example −
#include <unistd.h> int main(void) { char *file = "/usr/bin/echo"; char *arg1 = "Hello world!"; execl(file, file, arg1, NULL); return 0; }
The echo command in Linux is being invoked through the C code.
Output
Save, compile, and execute the above program −
$ gcc hello.c -o hello $ ./hello Hello world!
The execlp() Function
The execlp() function is similar to the execl() function. It uses the PATH environment variable to locate the file. Hence, the path to the executable file needn’t be given.
int execlp(const char *file, const char *arg, ..., NULL)
Example
Take a look at the following example −
#include <unistd.h> int main(void) { char *file = "echo"; char *arg1 = "Hello world!"; execlp(file, file, arg1, NULL); return 0; }
Output
Here, echo is already located in the PATH environment variable. Save, compile and run from the terminal.
$ gcc hello.c -o hello $ ./hello Hello world!
The execle() Function
In the execle() function, we can pass environment variables to the function, and it’ll use them. Its prototype is like this −
int execle(const char *pathname, const char *arg, ..., NULL, char *const envp[])
Example
Take a look at the following example −
#include <unistd.h> int main(void) { char *file = "/usr/bin/bash"; char *arg1 = "-c"; char *arg2 = "echo $ENV1 $ENV2!"; char *const env[] = {"ENV1 = Hello", "ENV2 = World", NULL}; execle(file, file, arg1, arg2, NULL, env); return 0; }
Output
Save, compile, and run from the terminal −
$ gcc hello.c -o hello $ ./hello Hello world!
The execv() Function
The execv() function receives a vector of arguments that will be available to the executable file. In addition, the last element of the vector has to be NULL:
int execv(const char *pathname, char *const argv[])
Example
Take a look at the following example −
#include <unistd.h> int main(void) { char *file = "/usr/bin/echo"; char *const args[] = {"/usr/bin/echo", "Hello world!", NULL}; execv(file, args); return 0; }
Output
Save, compile, and execute the above program −
$ gcc hello.c -o hello $ ./hello Hello world!
The execvp() Function
The execvp() has the following syntax −
int execvp(const char *file, char *const argv[])
Example
Take a look at the following example −
#include <unistd.h> int main(void) { char *file = "echo"; char *const args[] = {"/usr/bin/echo", "Hello world!", NULL}; execvp(file, args); return 0; }
Output
Save, compile, and execute the above program −
$ gcc hello.c -o hello $ ./hello Hello world!
The execve() Function
In addition to environment variables, we can pass other arguments to execve() function as a NULL-terminated vector −
int execve(const char *pathname, char *const argv[], char *const envp[])
Example
Take a look at the following example −
#include <unistd.h> int main(void) { char *file = "/usr/bin/bash"; char *const args[] = {"/usr/bin/bash", "-c", "echo Hello $ENV!", NULL}; char *const env[] = {"ENV=World", NULL}; execve(file, args, env); return 0; }
Output
Save, compile, and execute the above program −
$ gcc hello.c -o hello $ ./hello Hello world!