-5

I have a hard time understanding what is the difference between

 string name;

and

 string name();

Could someone explain me the difference?

user2736738
  • 30,591
  • 5
  • 42
  • 56
Maniek
  • 3
  • What is the difference between a cat and a car? The words are spelled almost the same, so they must be similar, right? Note: do not use the identifier `string` in C, it will confuse anyone familiar with C++. Do not use empty parenthesis declarations in C, it is obsolete style. – Lundin Jan 11 '18 at 10:03

2 Answers2

4

Assuming string is a data type you have already declared, string name; declares the variable name of type string.

The declaration string name(); declares the function name that returns a value of type string.

A variable is a place in memory where the program can store some data. A function is a piece of code that can be executed multiple times, when needed; it can receive (zero or more) arguments and it can optionally return one value.

axiac
  • 68,258
  • 9
  • 99
  • 134
  • 1
    As a complement, `string name();` in C declares a function that can accept arbitrary parameters (empty parameter list...) – Serge Ballesta Jan 11 '18 at 09:27
  • 2
    Notably, `string name();` is obsolete function declaration style and should not be used in C. See 6.11.6. – Lundin Jan 11 '18 at 10:01
0

string name; Declare the string data type variable 'name' in a memory where as string name(); shows the function prototype named as 'name' which returns the string value.

Usman
  • 1,983
  • 15
  • 28