Menu
×
   ❮   
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP C C++ C# AWS W3.CSS HOW TO BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR ANGULARJS GIT POSTGRESQL MONGODB ASP AI R GO KOTLIN SWIFT SASS VUE GEN AI SCIPY CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING HTML & CSS BASH RUST TOOLS

C Tutorial

C HOME C Intro C Get Started C Syntax C Output C Comments C Variables C Data Types C Type Conversion C Constants C Operators C Booleans C If...Else C Switch C While Loop C For Loop C Break/Continue C Arrays C Strings C User Input C Memory Address C Pointers

C Functions

C Functions C Function Parameters C Pass By Reference C Scope C Function Declaration C Functions Challenge C Math Functions C Inline Functions C Recursion C Function Pointers

C Files

C Create Files C Write To Files C Read Files

C Structures

C Structures C Structs Challenge C Nested Structures C Arrays of Structs C Structs & Pointers C Unions C typedef C Struct Padding

C Enums

C Enums

C Memory

C Memory Management C Allocate Memory C Access Memory C Reallocate Memory C Deallocate Memory C Structs and Memory C Memory Example

C Errors

C Errors C Error Challenge C Debugging C NULL C Error Handling C Input Validation

C Problem Solving

C Problem Solving C Build the Logic C Edge Cases & Errors C Test Cases

C More

C Date C Random Numbers C Macros C Organize Code C Storage Classes C Bitwise Operators C Fixed-width Integers C Command-Line Args

C DSA

C DSA Intro C Searching C Sorting C Stacks C Queues C Linked Lists C Trees C Hash Tables C Big O

C Projects

C Projects

C Cert

C Certificate

C Reference

C Reference C Keywords C <stdio.h> C <stdlib.h> C <string.h> C <math.h> C <ctype.h> C <time.h>

C Examples

C Examples C Real-Life Examples C Exercises C Quiz C Code Challenges C Practice Problems C Compiler C Syllabus C Study Plan C Interview Q&A

C Command-Line Arguments


Share

What are Command-Line Arguments?

You have learned that a function can take parameters, like add(5, 3).

main() is also a function. Command-line arguments are values that you give to main() when you start the program.

You write them after the program name, when you start the program from the command line (also called the terminal):

./add 5 3

This starts a program called add, and gives it two arguments: 5 and 3.


Why Use Command-Line Arguments?

You use them all the time, without knowing it!

When you double-click a file, like photo.jpg, your computer starts the photo program and gives it the file name as a command-line argument. That is how the program knows which photo to open.

Your own programs can work the same way. With command-line arguments, a program gets what it needs when it starts, so it does not have to stop and ask the user, like it does with scanf().


Read the Arguments in main()

To read the arguments, write main() with two parameters:

int main(int argc, char *argv[])
  • argc (argument count) is the number of arguments. The program name counts too.
  • argv (argument vector, which means a list of arguments) holds the arguments as strings, like an array of strings. argv[0] is the program name.

When you start the program with ./add 5 3, they look like this:


Example: Say Hello

This program says hello to the name you write after the program name:

Example

#include <stdio.h>

int main(int argc, char *argv[]) {
  if (argc < 2) {
    printf("Please write your name after the program name\n");
    return 1;
  }

  printf("Hello %s\n", argv[1]);
  return 0;
}

Compile the program, and start it with your name:

gcc hello.c -o hello
./hello Liam

Result:

Hello Liam

The if statement checks that a name was given. If you only type ./hello, argc is 1 and argv[1] does not exist, so the program prints a message instead.

Note: On Windows, type hello Liam instead of ./hello Liam.



Arguments Are Strings

All arguments are strings, even when you type a number. To use an argument as a number, convert it with the atoi() function from <stdlib.h>.

Here is the add program from the start of this chapter:

Example

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

int main(int argc, char *argv[]) {
  if (argc != 3) {
    printf("Please write two numbers after the program name\n");
    return 1;
  }

  int a = atoi(argv[1]);
  int b = atoi(argv[2]);

  printf("%d + %d = %d\n", a, b, a + b);
  return 0;
}

Compile the program, and start it with two numbers:

gcc add.c -o add
./add 5 3

Result:

5 + 3 = 8

This time, argc must be 3: the program name and two numbers.


Run It in Code::Blocks

You do not need the command line to try this. In Code::Blocks, go to Project > Set programs' arguments, write the arguments (like 5 3), and run the program as usual.

Note: The "Try it Yourself" editor on this website cannot give arguments to a program. That is why the examples on this page do not have a "Try it Yourself" button.


Free account

Track your progress

XP earned 0 Day streak 0 Spaces 0 League —

New

Learn in Adventure Mode W3Schools Adventure App

Coding fundamentals as bite-sized lessons and challenges.

×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookies and privacy policy.

Copyright 1999-2026 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.