Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Function on a variable declaration

Tags:

c

I'm reading the C Programming Language (chapter 5), and I'm confused by this example:

int n, array[SIZE], getint(int *);

Why is this function call in here like that? Is this just some tricky example and invalid code?

like image 635
Motheus Avatar asked Feb 15 '19 18:02

Motheus


People also ask

What is the declaration of a variable?

Declaration of a variable in a computer programming language is a statement used to specify the variable name and its data type. Declaration tells the compiler about the existence of an entity in the program and its location. When you declare a variable, you should also initialize it.

What is function declaration example?

For example, if the my_function() function, discussed in the previous section, requires two integer parameters, the declaration could be expressed as follows: return_type my_function(int x, y); where int x, y indicates that the function requires two parameters, both of which are integers.

What is variable declaration example?

For example: int age; float weight; char gender; In these examples, age, weight and gender are variables which are declared as integer data type, floating data type and character data type respectively.

What is variable and variable declaration?

Difference b/w variable declaration and definition: Variable declaration refers to the part where a variable is first declared or introduced before its first use. Declaration of variable type is also done in the part. A variable definition is a part where the variable is assigned a memory location and a value.


2 Answers

It's not calling the function; it's declaring its prototype. It's equivalent to:

int n;
int array[SIZE];
int getint(int*);
like image 125
Ray Avatar answered Sep 17 '22 16:09

Ray


Since the statement began with a type specifier, namely int, then it suggests declaration. Thus what follows is a bunch of comma separated list of identifiers.

n being a single int variable.

array being an array of int.

getint being a function that returns an int and has one parameter that is an int pointer. It is unnamed and that is not important because this is a function declaration/prototype.

like image 21
machine_1 Avatar answered Sep 20 '22 16:09

machine_1