Home > Back-end >  What does <int> mean in C ?
What does <int> mean in C ?

Time:12-24

How is it called when an int has <> around it and what does it mean? I first stumbled on it through this piece of code when I was reading stack class interface in C .

public:
    stack();
    stack(const stack& stk);
    stack(std::initializer_list<int> ilist);

In addition, how are the <> brackets called in English? I mean {} are curly braces, [] are square brackets, etc. I tried to google it, typing and "" in the search bar, but google ignored the brackets and showed results regarding solely "int". If I knew its name maybe I could have googled it better. So I thought asking it directly here makes sense.

CodePudding user response:

In C angle brackets are used, when you want to call function/method, use structure on anything else, that uses templates. For example

template <typename T>
void print(T arg){
  
  std::cout << arg << '\n';

}

In this example, we are using template as function argument. This means that you can pass anything you want in that function as parameter, and when you will compile your project, compiler will generate print function for any type you have used in your code.

print(5);
print("Hello World");
print('\n');

<int> is used to specify explicitly what type will be used in template. For example

auto ptr = std::make_unique<int>();

I have specified type explicitly, because I don't provide any parameter. If I don't specify type there, compiler will throw error.

  • Related