Hi,
in the next example code:

#include <iostream>;
using namespace std;
#include <iomanip>;

int main(int argc, char *argv[])
{
  for(int i = 1; i <= 28; i++)
  {
     cout << setw(3)<< i;
    if(i%7 == 0)
    {
        cout << endl;    
    }    
  }
  system("PAUSE");	
  return 0;
}

i compiled and i have no errors, but i want understand two lines:

1- (int argc, char *argv[]) .. what that refer to ..?
2- if (i%7 == 0)

Dani AI

Generated

Quick clarifications that expand on the helpful replies from , and .

The function signature int main(int argc, char *argv[]) gives a program access to command-line arguments. argc is the count (the program name itself is usually argv[0]), and argv is an array of C-strings holding each argument. Always check argc before reading argv[index] to avoid out-of-range access. The standard allows both char *argv[] and char **argv; see the C++ reference on main for platform details and permitted forms ().

The expression i % 7 == 0 uses the remainder operator %. It yields the remainder after integer division, so the test is true exactly when i is a multiple of 7. A subtle point beginners miss: the remainder can be negative when the dividend is negative, because C++ integer division truncates toward zero; see the language reference on the remainder operator for exact rules (cppreference: remainder operator).

A couple of practical notes not in the thread:

  • setw(3) (from <iomanip>) sets the minimum field width for the next output and pads on the left by default (right-aligned). See std::setw.
  • system("PAUSE") is Windows-specific and invokes a shell command; it is non-portable and has security considerations. Prefer running the program from a console or use a portable pause like reading from std::cin if needed (std::system).

If command-line parsing is desired later, convert argv[] strings with safe functions (e.g., std::strtol or std::stoi) and validate inputs before use. This complements the practical points already made by the other posters.

Recommended Answers

All 3 Replies

1- (int argc, char *argv[]) .. what that refer to ..?

You'll find more about this here.

2- if (i%7 == 0)

% is the modulo operator, it returns the remainder of a division, for example 14%7 will return 0, but 15%7 will return 1.

1.argc stands for argument count. It's the number of arguments passed to the program via the command-line. The first agument is always the name of the program it's self (or rather, the path used to execute it).

An easy way to understand this is with the followign program:

#include <stdio.h>

main (int argc, char *argv[])
{
    printf("%d\n", argc);
}
  1. argv is an array that contains the command-line arguments that argc counts.

An example... possibly a bit hard to understand... Notice how **argv works exactly the same as *argv[] does:

main (int argc, char **argv)
{
    int i = 0, j = 0;
    for (; i < argc; i++)
        for (; j < strlen(argv[i]); j++)
            printf("argv[%d][%d] is: %c\n", i, j, argv[i][j]);
}

1. Since you are new to C++, I doubt you will really need to use that argc stuff but like that first post says, you can find information if you really need in that link.

2. "If you divide i by 7 and have the remaider 0 then do something."

if (i) modular (7) is equal to (0) then
{
do this
}


% stands for mod which is basically remainder and == is used to check the right side values against the left side (or vise versa but you get the point...)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.