This question already has answers here:
What header should I include for memcpy and realloc?
(2 answers)
warning: incompatible implicit declaration of built-in function 'strlen' and 'strcpy' [duplicate]
(1 answer)
Closed 5 years ago.
#include<stdio.h>
struct stud
{
char name[20];
int mark;
int per;
char grad[5];
};
void main(){
int i,n;
printf("Enter number of students");
scanf("%d",&n);
struct stud s[n];
for(i=1;i<=n;i++)
{
printf("Enter name of student");
scanf("%d",s[i].name);
printf("Enter obtained marks");
scanf("%d",&s[i].mark);
s[i].per=s[i].mark/5;
}
for(i=1;i<=n;i++)
{
if(s[i].per>=80)
strcpy(s[i].grad,"A");
else if(s[i].per>=60)
strcpy(s[i].grad,"B");
else if(s[i].per>=50)
strcpy(s[i].grad,"C");
else if(s[i].per>=40)
strcpy(s[i].grad,"D");
else
strcpy(s[i].grad,"F");
}
for(i=1;i<=n;i++)
{
printf("&s",s[i].name);
printf("&d",s[i].mark);
printf("&d",s[i].per);
printf("&s",s[i].grad);
}
}
When executing this code it's showing error :
main.c: In function ‘main’:
main.c:25:10: warning: implicit declaration of function ‘strcpy’ [-Wimplicit-function-declaration]
strcpy(s[i].grad,"A");
^~~~~~
main.c:25:10: warning: incompatible implicit declaration of built-in function ‘strcpy’
main.c:25:10: note: include ‘<string.h>’ or provide a declaration of ‘strcpy’
main.c:27:11: warning: incompatible implicit declaration of built-in function ‘strcpy’
strcpy(s[i].grad,"B");
^~~~~~
main.c:27:11: note: include ‘<string.h>’ or provide a declaration of ‘strcpy’
main.c:29:11: warning: incompatible implicit declaration of built-in function ‘strcpy’
strcpy(s[i].grad,"C");
^~~~~~
main.c:29:11: note: include ‘<string.h>’ or provide a declaration of ‘strcpy’
main.c:31:10: warning: incompatible implicit declaration of built-in function ‘strcpy’
strcpy(s[i].grad,"D");
^~~~~~
main.c:31:10: note: include ‘<string.h>’ or provide a declaration of ‘strcpy’
main.c:33:10: warning: incompatible implicit declaration of built-in function ‘strcpy’
strcpy(s[i].grad,"F");
^~~~~~
main.c:33:10: note: include ‘<string.h>’ or provide a declaration of ‘strcpy’
What's the error here ?
scanf("%d",s[i].name); is undefined behavior. It will be "%s" format specifier.
And printf("%s",s[i].name) not &s.
Include string.h header file.
Also array index out of bound while getting input. Also undefined behavior. Array indices start from 0. All loop would be for(i=0;i<=n-1;i++)
and include header like this:
#include <string.h>
for(i=0;i<=n-1;i++)
{
printf("Enter name of student");
scanf("%s",s[i].name);
printf("Enter obtained marks");
scanf("%d",&s[i].mark);
s[i].per=s[i].mark/5;
}
Another change
for(i=1;i<=n;i++)
{
printf("%s",s[i].name);
printf("%d",s[i].mark);
printf("%d",s[i].per);
printf("%s",s[i].grad);
}
Related
As I'm new to lexer and parser, so I'm trying to read and understand others code.
Here is the code i'm trying to use : https://gist.github.com/justjkk/436828
But it's giving me error. How can I resolve this?
E:\flex_bison_test>gcc lex.yy.c y.tab.c -o json.exe
json.l: In function 'yylex':
json.l:34:11: warning: assignment to 'YYSTYPE' {aka 'int'} from 'char *' makes integer from pointer without a cast [-Wint-conversion]
yylval=strclone(yytext);
^
json.l:38:11: warning: assignment to 'YYSTYPE' {aka 'int'} from 'char *' makes integer from pointer without a cast [-Wint-conversion]
yylval=strclone(yytext);
^
json.l: In function 'strclone':
json.l:82:15: warning: implicit declaration of function 'strlen' [-Wimplicit-function-declaration]
int len = strlen(str);
^~~~~~
json.l:82:15: warning: incompatible implicit declaration of built-in function 'strlen'
json.l:82:15: note: include '<string.h>' or provide a declaration of 'strlen'
json.l:79:1:
+#include <string.h>
%%
json.l:82:15:
int len = strlen(str);
^~~~~~
json.l:84:5: warning: implicit declaration of function 'strcpy' [-Wimplicit-function-declaration]
strcpy(clone,str);
^~~~~~
json.l:84:5: warning: incompatible implicit declaration of built-in function 'strcpy'
json.l:84:5: note: include '<string.h>' or provide a declaration of 'strcpy'
y.tab.c: In function 'yyparse':
y.tab.c:627:16: warning: implicit declaration of function 'yylex' [-Wimplicit-function-declaration]
# define YYLEX yylex ()
^~~~~
y.tab.c:1272:16: note: in expansion of macro 'YYLEX'
yychar = YYLEX;
^~~~~
y.tab.c:1540:7: warning: implicit declaration of function 'yyerror'; did you mean 'yyerrok'? [-Wimplicit-function-declaration]
yyerror (YY_("syntax error"));
^~~~~~~
yyerrok
json.y: At top level:
json.y:80:6: warning: conflicting types for 'yyerror'
void yyerror (char const *s) {
^~~~~~~
y.tab.c:1540:7: note: previous implicit declaration of 'yyerror' was here
yyerror (YY_("syntax error"));
^~~~~~~
E:\flex_bison_test>
Or these should remain as it is.
All the commands, I have given :
flex json.l
bison -dy json.y
gcc lex.yy.c y.tab.c -o json.exe
Simply:
#include <string.h>
in your flex definitions section on top of json.l should fix it for you.
There's also a Makefile in the repository you pointed to. Maybe you should use that. You don't seem to be generating the parser files properly. See comment below.
As for the remaining warnings:
warning: implicit declaration of function 'yyerror';
warning: implicit declaration of function 'yylex';
These can be easily fixed by adding declarations of yylex() and yyerror should be present in the bison prologue section at the top of your json.y:
%{
int yylex();
void yyerror(const char *s);
%}
As for these ones:
json.l:34:11: warning: assignment to 'YYSTYPE' {aka 'int'} from 'char *' makes integer from pointer without a cast [-Wint-conversion]
yylval=strclone(yytext);
^
json.l:38:11: warning: assignment to 'YYSTYPE' {aka 'int'} from 'char *' makes integer from pointer without a cast [-Wint-conversion]
yylval=strclone(yytext);
^
They're a bit more subtle. I would suggest have a look here on how to use yylval for correctly passing on strings from the lex's tokens into the parser's actions. The problem now is that yylval is a bare int but it ends up being assigned char pointers for both NUMBER and STRING tokens.
This is my basic C test program.
After I built it, I just entered negative number like -1, -2, etc. in console.
But the result is "oh", not "another number".
I don't know why this happens, because negative numbers should make the 'if' statement true.
int main(int argc, char* argv[]){
long int num;
scanf("%d", &num);
if(num ==1 || num < 0){
printf("another number\n");
}else{
printf("oh\n");
}
}
When you use the %d format string with scanf, the corresponding argument will be treated as int*. But you have passed a long int*. The value scanf stores will not be the same size as what your if statement reads.
Formally, you get undefined behavior. In practice, on most platforms scanf will write only part of the variable, and the rest will be left with an arbitrary value, with the usual bad effects on future use.
Use %ld for long variables, %d for ints. Change your code to one of these:
int num;
scanf("%d", &num);
or
long int num;
scanf("%ld", &num);
/tmp$ gcc -Wall foo.c
foo.c: In function ‘main’:
foo.c:4:5: warning: implicit declaration of function ‘scanf’ [-Wimplicit-function-declaration]
foo.c:4:5: warning: incompatible implicit declaration of built-in function ‘scanf’ [enabled by default]
foo.c:4:5: warning: format ‘%d’ expects argument of type ‘int *’, but argument 2 has type ‘long int *’ [-Wformat]
foo.c:7:9: warning: implicit declaration of function ‘printf’ [-Wimplicit-function-declaration]
foo.c:7:9: warning: incompatible implicit declaration of built-in function ‘printf’ [enabled by default]
foo.c:9:9: warning: incompatible implicit declaration of built-in function ‘printf’ [enabled by default]
foo.c:11:1: warning: control reaches end of non-void function [-Wreturn-type]
fix the causes of those warnings and all the bugs will go away.
A recurrence relation T is defined on n >= 0 and is given as T(n) = T(n-1) + 2*n with the base case T(0) = 1.
You will be given one integer k and you have to write a program to find out T(k).
The program must implement T( ) recursively.
Here is my Code :
include<stdio.h>
long long T(int input)
{
if(input == 0)
return 1;
return 2*input + T(input-1);
}
int main()
{
/*write your code here*/
int k;
scanf("%d",&k);
printf("%lli",T(k));
return 0;
}
Here is the Error i got
Program:1:9: error: expected '=', ',', ';', 'asm' or '__attribute__' before '<' token
Program:In function 'main':
Program:13:4: warning: implicit declaration of function 'scanf' [-Wimplicit-function-declaration]
Program:13:4: warning: incompatible implicit declaration of built-in function 'scanf' [enabled by default]
Program:14:4: warning: implicit declaration of function 'printf' [-Wimplicit-function-declaration]
Program:14:4: warning: incompatible implicit declaration of built-in function 'printf' [enabled by default]
Program:14:4: warning: implicit declaration of function 'T' [-Wimplicit-function-declaration]
Program:14:4: warning: format '%lli' expects argument of type 'long long int', but argument 2 has type 'int' [-Wformat]
How can i achieve the following Expected Results ?
Sample Test Cases
Input Output
Test Case 1 1 3
Test Case 2 2 7
Test Case 3 3 13
You are missing the square/hash/sharp # symbol behind the include on top.
Warnings indicate that scanf is not defined. Definition for that is given in stdio.h which you have failed to include.
Otherwise, your recursion function is pretty much alright.
You have a typo. Change
include<stdio.h>
to:
#include <stdio.h>
I get this error.
error: warning: incompatible implicit declaration of built-in function ‘memcpy’ [enabled by default]
This is the code:
int arr[ 12] = {1,0,0,0,0,0,0,0,0,0,9370, 0};
void *a = &arr;
memcpy(machine->mem, a,12*4);
What I am doing wrong?
You likely forgot to include <string.h>.
Add #include <string.h> to the top of your file.
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I get the following error with gcc when I declare a struct netif * from a function scope (where the compile doesn't complain) to global scope:
src/main.c:114:3: warning: passing argument 1 of 'low_level_init' from incompatible pointer type [enabled by default]
src/main.c:62:13: note: expected 'struct netif *' but argument is of type 'struct netif *'
Why is does compiler give the following "non-sensical" complaint?
expected 'stuct netif *' but argument is of type 'struct netif *'
This "full program"
void foonetif(struct netif *dst) {
if (dst) return;
return;
}
struct netif {
double bar;
};
int main(void) {
struct netif *netif = 0; /* NULL */
foonetif(netif);
return 0;
}
generates this output from gcc 10398683.c (gcc version 4.6.3), so with all default options
10398683.c:1:22: warning: ‘struct netif’ declared inside parameter list [enabled by default]
10398683.c:1:22: warning: its scope is only this definition or declaration, which is probably not what you want [enabled by default]
10398683.c: In function ‘main’:
10398683.c:12:3: warning: passing argument 1 of ‘newnetif’ from incompatible pointer type [enabled by default]
10398683.c:1:6: note: expected ‘struct netif *’ but argument is of type ‘struct netif *’
Note the last warning (really a note) :)
The problem is scope. The first struct netif is in the scope of the function's argument list, and goes out of scope after that function ends. The second struct netif is defined in a new scope. These are thereby different structure types with different tags which happen to have the same name. Just like int i; and int i; in two different functions are different variables that happen to have the same name.
An interesting question; have fallen foul of this myself; mostly due to horrid hacks involving dubious type casts on innocent memory-blocks.
This answer is really just a corollary to R's answer, which pretty much nails the issue (though i'm not quite sure about the and goes out of scope after that function ends.. bit)
For me, the key to this is perusal of:
(1) ISO C99 6.7.2:
Two types have compatible type if their types are the same. Additional
rules for determining whether two types are compatible are described
in 6.7.2 for type specifiers, in 6.7.3 for type qualifiers, and in
6.7.5 for declarators.46) Moreover, two structure, union, or enumerated types declared in separate translation units are compatible
if their tags and members satisfy the following requirements: If one
is declared with a tag, the other shall be declared with the same tag. ...
(2) C namespaces
Anyway, here's some code (~a couple of translation units) that hopefully demonstrates some possibly surprising behaviour for those that haven't yet hit this issue before:
blah.c:
#include <stdio.h>
struct bar {int a; int b;} stbar;
struct bar_ {int a; int d;} stbar_;
void foo(struct bar* pst);
void foo_(struct bar st);
void callfoo()
{
/*no warnings; possibly surprising results ! */
stbar.a=313;
stbar.b=31313;
foo(&stbar);
printf("called foo() with stbar: %d, %d\n", stbar.a, stbar.b);
/*generates incompatible types warnings:
blah.c:23:5: warning: passing argument 1 of ‘foo’ from incompatible pointer type [enabled by default]
blah.c:6:6: note: expected ‘struct bar *’ but argument is of type ‘struct bar_ *’ */
stbar_.a=313;
stbar_.d=31313;
foo(&stbar_);
printf("called foo() with stbar_: %d, %d\n", stbar_.a, stbar_.d);
/*generates incompatible types warnings:
blah.c:31:5: warning: passing argument 1 of ‘foo’ from incompatible pointer type [enabled by default]
blah.c:6:6: note: expected ‘struct bar *’ but argument is of type ‘struct bar *’ */
struct bar {float s; float t;} stbar;
foo(&stbar);
printf("called foo() with locally defined stbar: %f, %f\n", stbar.s, stbar.t);
}
void callfoo_()
{
stbar.a=313;
stbar.b=31313;
//passing in incompatible type by value ~ no warnings; possibly surprising results !
foo_(stbar);
/*uncomenting next line generates compiler error:
blah.c:47:5: error: incompatible type for argument 1 of ‘foo_’
blah.c:7:6: note: expected ‘struct bar’ but argument is of type ‘struct bar_’ */
//foo_(stbar_);
}
void main()
{
callfoo();
callfoo_();
}
blah_.c:
#include <stdio.h>
struct bar {int x; float z;} stbar;
void foo(struct bar* pst)
{
printf("foo : %d, %f\n", pst->x, pst->z);
pst->x=13;
pst->z=13.13;
}
void foo_(struct bar st)
{
printf("foo_ : %d, %f\n", st.x, st.z);
st.x=13;
st.z=13.13;
}
output:
$ gcc blah.c blah_.c
blah.c: In function ‘callfoo’:
blah.c:23:5: warning: passing argument 1 of ‘foo’ from incompatible pointer type [enabled by default]
blah.c:6:6: note: expected ‘struct bar *’ but argument is of type ‘struct bar_ *’
blah.c:31:5: warning: passing argument 1 of ‘foo’ from incompatible pointer type [enabled by default]
blah.c:6:6: note: expected ‘struct bar *’ but argument is of type ‘struct bar *’
$ ./a.out
foo : 313, 0.000000
called foo() with stbar: 13, 1095898235
foo : 313, 0.000000
called foo() with stbar_: 13, 1095898235
foo : 13274075, 0.000000
called foo() with locally defined stbar: 0.000000, 13.130000
foo_ : 313, 0.000000
$ gcc --version
gcc (Ubuntu/Linaro 4.6.1-9ubuntu3) 4.6.1 ...
Corollary of corollary: praise the gods for C++ namespaces.
Your "full program" wont compile.
struct netif is not declared before foonetif which uses it. Try moving the struct definition before its first use.