function-declaration

behaviour of implicit function declaration

孤街浪徒 提交于 2019-12-08 14:12:13
问题 I know it is wrong to use a function without prototype. But when I was fiddling around, I came across this strange and conflicting behavior. test1 #include <stdio.h> #include <limits.h> void main(){ char c='\0'; float f=0.0; xof(c,f);/* at this point implicit function declaration is generated as int xof(int ,double ); */ } int xof(char c,float f) { printf("%d %f\n", c,f); } Implicit function declaration would be int xof(int ,double ); error is variablename.c:8:5: error: conflicting types for

Weird C function declaration

心已入冬 提交于 2019-12-06 07:05:50
问题 I was going through some code when I encountered this in one of the source files. int st_insert(table, key, value) register st_table *table; register st_data_t key; st_data_t value; { unsigned int hash_val, bin_pos; register st_table_entry *ptr; hash_val = do_hash(key, table); FIND_ENTRY(table, ptr, hash_val, bin_pos); if (ptr == 0) { ADD_DIRECT(table, key, value, hash_val, bin_pos); return 0; } else { ptr->record = value; return 1; } } What is this style? Is it some obscure way to declare

Implicit function declarations and linkage

会有一股神秘感。 提交于 2019-12-05 20:22:55
Recently I've learnt about implicit function declarations in C . The main idea is clear but I have some troubles with understanding of the linkage process in this case. Consider the following code ( file a.c ): #include <stdio.h> int main() { double someValue = f(); printf("%f\n", someValue); return 0; } If I try to compile it: gcc -c a.c -std=c99 I see a warning about implicit declaration of function f() . If I try to compile and link: gcc a.c -std=c99 I have an undefined reference error. So everything is fine. Then I add another file (file b.c ): double f(double x) { return x; } And invoke

Weird C function declaration

痞子三分冷 提交于 2019-12-04 12:34:55
I was going through some code when I encountered this in one of the source files . int st_insert(table, key, value) register st_table *table; register st_data_t key; st_data_t value; { unsigned int hash_val, bin_pos; register st_table_entry *ptr; hash_val = do_hash(key, table); FIND_ENTRY(table, ptr, hash_val, bin_pos); if (ptr == 0) { ADD_DIRECT(table, key, value, hash_val, bin_pos); return 0; } else { ptr->record = value; return 1; } } What is this style? Is it some obscure way to declare functions? Is there some reason one might use this over normal function declarations? Vlad from Moscow

extern declaration and function definition both in the same file

核能气质少年 提交于 2019-12-04 02:51:39
I was just browsing through gcc source files. In gcc.c , I found something like extern int main (int, char **); int main (int argc, char **argv) { Now my doubt is extern is to tell the compiler that the particular function is not in this file but will be found somewhere else in the project. But here, definition of main is immediately after the extern declaration. What purpose is the extern declaration serving then? It seems like, in this specific example, extern seems to be behaving like export that we use in assembly, wherin we export a particular symbol outside of the module Any ideas? You

How to understand this define

匆匆过客 提交于 2019-12-04 00:56:46
Nowadays , i was reading the APUE.and i found the function defined as below: void (*signal(int signo, void (*func)(int)))(int); i was confused, i know signal is pointer to a function and the last (int) is his parameter. i did not know what is (int signo,void (*func)(int)). John Bode The general procedure: find the leftmost identifier and work your way out. Absent an explicit grouping with parentheses, postfix operators such as () and [] bind before unary operators like * ; thus, the following are all true: T *x[N] -- x is an N-element array of pointer to T T (*x)[N] -- x is a pointer to an N

How does ampersand in the return type of a function declaration work? [duplicate]

∥☆過路亽.° 提交于 2019-12-03 09:27:28
问题 This question already has answers here : In C++, what does & mean after a function's return type? (11 answers) Closed 6 years ago . In this piece of code, why f() is declared as "double & f(..."? What does it mean and how does it work? I don't even know what to google to find the answer to my question. Please help. double a = 1, b = 2; double & f (double & d) { d = 4; return b; } I know ampersand sign means the address of a variable or a function, but I don't see why it would make sense to

JavaScript function declaration

喜你入骨 提交于 2019-12-03 02:34:32
问题 Are the JavaScript code snippets given below some sort of function declaration? If not can someone please give an overview of what they are? some_func = function(value) { // some code here } and show:function(value){ // some code here } 回答1: The first one is simply creating an anonymous function and assigning it to a variable some_func . So using some_func() will call the function. The second one should be part of an object notation var obj = { show:function(value){ // some code here } }; So,

How does ampersand in the return type of a function declaration work? [duplicate]

半腔热情 提交于 2019-12-03 01:27:20
This question already has an answer here: In C++, what does & mean after a function's return type? 11 answers In this piece of code, why f() is declared as "double & f(..."? What does it mean and how does it work? I don't even know what to google to find the answer to my question. Please help. double a = 1, b = 2; double & f (double & d) { d = 4; return b; } I know ampersand sign means the address of a variable or a function, but I don't see why it would make sense to write it when you are declaring a function. When the & operator is used in a declaration form, preceded by a type it doesn't

Why is Taking the Address of a Function That is Declared Only Working?

痴心易碎 提交于 2019-12-02 20:06:31
问题 I've asked a question here about whether taking the address of a function forces the compilation of said function specifically with regard to Substitution-Failure-Is-Not-An-Error. The most direct answer to this can be found here: Informally, an object is odr-used if its address is taken, or a reference is bound to it, and a function is odr-used if a function call to it is made or its address is taken. If an object or a function is odr-used, its definition must exist somewhere in the program;