I am trying to learn C. :) however I am getting this error which I do not understand . Can somebody please explain this to me. I am not able to understand what is causing this issue.
#include<stdio.h>
#include<conio.h>
main()
{
FILE *fp, *ft;
char ch;
fp=fopen("D:\Documents\sample.txt","w");
if (fp ==NULL)
{
puts("cannot open file");
exit();
}
ft=fopen("D:\Documents\sample - Copy.txt","w");
if (ft ==NULL)
{
puts("cannot open file");
exit();
}
while(1)
{
ch=fgetc(fp);
if (ch== EOF)
break;
else
fputc(ch,ft);
}
fclose(fp);
fclose(ft);
}
Error message I am getting :-
C:\Users\LoneRanger\Desktop\FileHandling.c: In function 'main':
C:\Users\LoneRanger\Desktop\FileHandling.c:8:11: warning: unknown escape sequenc
e: '\D' [enabled by default]
fp=fopen("D:\Documents\sample.txt","w");
^
C:\Users\LoneRanger\Desktop\FileHandling.c:8:11: warning: unknown escape sequenc
e: '\s' [enabled by default]
C:\Users\LoneRanger\Desktop\FileHandling.c:12:6: warning: incompatible implicit
declaration of built-in function 'exit' [enabled by default]
exit();
^
C:\Users\LoneRanger\Desktop\FileHandling.c:12:6: error: too few arguments to fun
ction 'exit'
C:\Users\LoneRanger\Desktop\FileHandling.c:15:11: warning: unknown escape sequen
ce: '\D' [enabled by default]
ft=fopen("D:\Documents\sample - Copy.txt","w");
^
C:\Users\LoneRanger\Desktop\FileHandling.c:15:11: warning: unknown escape sequen
ce: '\s' [enabled by default]
C:\Users\LoneRanger\Desktop\FileHandling.c:19:6: warning: incompatible implicit
declaration of built-in function 'exit' [enabled by default]
exit();
^
C:\Users\LoneRanger\Desktop\FileHandling.c:19:6: error: too few arguments to fun
ction 'exit'
C:\Users\LoneRanger\Desktop>gcc C:\Users\LoneRanger\Desktop\FileHandling.c -o Fi
leHandling
C:\Users\LoneRanger\Desktop\FileHandling.c: In function 'main':
C:\Users\LoneRanger\Desktop\FileHandling.c:8:11: warning: unknown escape sequenc
e: '\D' [enabled by default]
fp=fopen("D:/\Documents/\sample.txt","w");
^
C:\Users\LoneRanger\Desktop\FileHandling.c:8:11: warning: unknown escape sequenc
e: '\s' [enabled by default]
C:\Users\LoneRanger\Desktop\FileHandling.c:12:6: warning: incompatible implicit
declaration of built-in function 'exit' [enabled by default]
exit();
^
C:\Users\LoneRanger\Desktop\FileHandling.c:12:6: error: too few arguments to fun
ction 'exit'
C:\Users\LoneRanger\Desktop\FileHandling.c:15:11: warning: unknown escape sequen
ce: '\D' [enabled by default]
ft=fopen("D:/\Documents/\sample - Copy.txt","w");
^
C:\Users\LoneRanger\Desktop\FileHandling.c:15:11: warning: unknown escape sequen
ce: '\s' [enabled by default]
C:\Users\LoneRanger\Desktop\FileHandling.c:19:6: warning: incompatible implicit
declaration of built-in function 'exit' [enabled by default]
exit();
^
C:\Users\LoneRanger\Desktop\FileHandling.c:19:6: error: too few arguments to fun
ction 'exit'
\ is escape symbol in C, you should use \\ in your strings, like:
fp=fopen("D:\\Documents\\sample.txt","w");
exit() is in stdlib.h and you don't include this header so there is a error.
Also return is a better option than exit here
In C, C++, C#, and a bunch of other languages, you can make strings with special characters, such as adding a new line ('\n'), a tab ('\t') and many others.
The convention is to 'escape' them, that is, use a backslash \ and one or more characters to symbolize the character you want. This means that \ is special in a string and you need to use it with care.
In your case, when you want to use a real \ to separate directories, you need to escape it as \\
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.
I was trying to compile a C socket program in CYGWIN gcc but when I compile the client program it gives me the following error
client.h: In function ‘error’:
client.h:11:5: warning: incompatible implicit declaration of built-in function ‘exit’ [enabled by default]
exit(1);
^
client.h: In function ‘main’:
client.h:30:9: warning: incompatible implicit declaration of built-in function ‘exit’ [enabled by default]
exit(0);
^
client.h:36:5: warning: passing argument 2 of ‘connect’ from incompatible pointer type [enabled by default]
if(connect(sockfd,&serv_addr,sizeof(serv_addr)) < 0)
^
In file included from client.h:3:0:
/usr/include/sys/socket.h:28:7: note: expected ‘const struct sockaddr *’ but argument is of type ‘struct sockaddr_in *’
int connect (int, const struct sockaddr *, socklen_t);
^
And when I tries to compile the server program it gives me the following error
server.h: In function ‘error’:
server.h:8:5: warning: incompatible implicit declaration of built-in function ‘exit’ [enabled by default]
exit(1);
^
server.h: In function ‘main’:
server.h:18:9: warning: incompatible implicit declaration of built-in function ‘exit’ [enabled by default]
exit(1);
^
server.h:23:5: warning: incompatible implicit declaration of built-in function ‘bzero’ [enabled by default]
bzero((char *) &serv_addr, sizeof(serv_addr));
^
server.h:32:64: error: ‘client’ undeclared (first use in this function)
newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &client);
^
server.h:32:64: note: each undeclared identifier is reported only once for each function it appears in
So what is the solution to this
The error in your client code is because you pass a pointer to a struct sockaddr_in to a parameter that expects it to be a pointer to a struct sockaddr. The error message basically says it all. The error in your server code is because the variable client is not declared anywhere.
The warnings are caused by not including the appropriate include files containing the declarations of exit (include stdlib.h) and bzero (include strings.h). You thus get an implicit declaration and since the compiler knows these functions as standardly built-in functions it mentions that in the warning as well.
Well the issue was that i was trying to compile the linux program in windows.
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.
What I am really trying to achieve is an array of dynamic byte patterns that I can use as a pattern searcher when I buffer binary files. But I am starting off basic for now. I have the following code that I based off of an example found on StackOverflow.
How to Initialize a Multidimensional Char Array in C?
typedef unsigned char BYTE;
int main()
{
BYTE *p[2][4] = {
{0x44,0x58,0x54,0x31},
{0x44,0x58,0x54,0x00}
};
return 0;
}
I compile it with mingw32 for Windows.
D:\> gcc example.c -o example.exe
I get the following warnings when I try to compile.
example.c: In function 'main':
example.c:6:3: warning: initialization makes pointer from integer without a cast [enabled by default]
example.c:6:3: warning: (near initialization for 'p[0][0]') [enabled by default]
example.c:6:3: warning: initialization makes pointer from integer without a cast [enabled by default]
example.c:6:3: warning: (near initialization for 'p[0][1]') [enabled by default]
example.c:6:3: warning: initialization makes pointer from integer without a cast [enabled by default]
example.c:6:3: warning: (near initialization for 'p[0][2]') [enabled by default]
example.c:6:3: warning: initialization makes pointer from integer without a cast [enabled by default]
example.c:6:3: warning: (near initialization for 'p[0][3]') [enabled by default]
example.c:7:3: warning: initialization makes pointer from integer without a cast [enabled by default]
example.c:7:3: warning: (near initialization for 'p[1][0]') [enabled by default]
example.c:7:3: warning: initialization makes pointer from integer without a cast [enabled by default]
example.c:7:3: warning: (near initialization for 'p[1][1]') [enabled by default]
example.c:7:3: warning: initialization makes pointer from integer without a cast [enabled by default]
example.c:7:3: warning: (near initialization for 'p[1][2]') [enabled by default]
I don't understand the nature of this warning. How do I go about resolving it? Thanks.
Drop the * from BYTE *p[2][4]:
BYTE p[2][4] = {
{0x44,0x58,0x54,0x31},
{0x44,0x58,0x54,0x00}
};
You want a multidimensional array of char: BYTE p[2][4] not a multidimensional array of pointer-to-char.
Parser.h
enum { PLUS, MINUS, DIVIDE, MULTIPLY, NUMBER, END } type;
int token;
/* parsing functions */
void parse_token (void);
Parser.c
void get_token (void)
{
token++;
parse_token(); /* LINE 11 */
}
void parse_token (void) /* LINE 14 */
{
if ( strchr ("1234567890.", token) )
type = NUMBER;
else if ( strchr ("+", token) )
type = PLUS;
else if ( strchr ("-", token) )
type = MINUS;
else if ( strchr ("/", token) )
type = DIVIDE;
else if ( strchr ("*",token) )
type = MULTIPLY;
else if ( token == '\0' )
type = END;
else
show_error(strcat("Couldn't parse token : ", token));
}
The Errors
parser.c:14:6: warning: conflicting types for ‘parse_token’ [enabled by default]
parser.c:11:2: note: previous implicit declaration of ‘parse_token’ was here
parser.c: In function ‘parse_token’:
parser.c:16:2: warning: passing argument 2 of ‘strchr’ makes integer from pointer without a cast [enabled by default]
/usr/include/string.h:235:14: note: expected ‘int’ but argument is of type ‘char *’
parser.c:17:3: error: ‘type’ undeclared (first use in this function)
parser.c:17:3: note: each undeclared identifier is reported only once for each function it appears in
parser.c:17:10: error: ‘NUMBER’ undeclared (first use in this function)
parser.c:19:2: warning: passing argument 2 of ‘strchr’ makes integer from pointer without a cast [enabled by default]
/usr/include/string.h:235:14: note: expected ‘int’ but argument is of type ‘char *’
parser.c:20:10: error: ‘PLUS’ undeclared (first use in this function)
parser.c:22:2: warning: passing argument 2 of ‘strchr’ makes integer from pointer without a cast [enabled by default]
/usr/include/string.h:235:14: note: expected ‘int’ but argument is of type ‘char *’
parser.c:23:10: error: ‘MINUS’ undeclared (first use in this function)
parser.c:25:2: warning: passing argument 2 of ‘strchr’ makes integer from pointer without a cast [enabled by default]
/usr/include/string.h:235:14: note: expected ‘int’ but argument is of type ‘char *’
parser.c:26:10: error: ‘DIVIDE’ undeclared (first use in this function)
parser.c:28:2: warning: passing argument 2 of ‘strchr’ makes integer from pointer without a cast [enabled by default]
/usr/include/string.h:235:14: note: expected ‘int’ but argument is of type ‘char *’
parser.c:29:10: error: ‘MULTIPLY’ undeclared (first use in this function)
parser.c:32:10: error: ‘END’ undeclared (first use in this function)
parser.c: In function ‘show_error’:
parser.c:40:2: warning: incompatible implicit declaration of built-in function ‘exit’ [enabled by default]
I'm utterly bamboozled. :(.
Any help?
One you get it to compile (by including the header, as Luchian Grigore said), you'll find that you can't do strcat() on a constant string.
The constant string is allocated in read-only memory, and can't be modified. And even if you could modify it, you would be overwriting other things in memory.
You're not including your header, so there's no way for the translation unit to know about the declarations of type and token.
You need:
#include "Parser.h"
at the beginning of the implementation file.