This question already has answers here:
Compiler error: memset was not declared in this scope
(2 answers)
Closed 7 years ago.
I have the following c code:
#include<stdio.h>
int main(void)
{
char buff[10];
memset(buff,0,sizeof(buff));
gets(buff);
printf("\n The buffer entered is [%s]\n",buff);
return 0;
}
When I run the code, I get the following warning:
warning: implicit declaration of function ‘memset’ [-Wimplicit-function-declaration]
How should I solve the problem?
Thanks
Add
#include <string.h>
at the top of file.
This because is the header file where the memset prototype can be found by compiler.
Avoid using gets function... Use scanf or fgets instead.
Take a look HERE
Add
#include <string.h>
memset is available in string.h
Related
This question already has answers here:
undefined reference to `getline' in c
(3 answers)
Closed last month.
I am trying to use getline() function. I am using correct syntax I guess and also I have included #include<stdio.h> header file also. Still it is showing that [Error] 'getline' was not declared in this scope
here is my code
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
int main()
{
char *str;
int bytes_read;
int size=10;
printf("please enter a string");
str=(char *)malloc(size);
bytes_read=getline(&str,&size,stdin);
puts(str);
}
getline is a POSIX function, and Windows isn't POSIX, so it doesn't have some POSIX C functions available.
You'll need to define the feature macro _GNU_SOURCE in order to make it available for your code.
#define _GNU_SOURCE
This question already has answers here:
Is the strrev() function not available in Linux?
(6 answers)
Closed 1 year ago.
Iam trying to reverse a string using the strrev function in ubuntu. But iam getting two errors which says , "warning: implicit declaration of function ‘strrev’; did you mean ‘strsep’? [-Wimplicit-function-declaration]" and another error " warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘int’ [-Wformat=].Can anyone please help me resolve this problem. I have not learn pointers yet, so is there another method?
#include <stdio.h>
#include <string.h>
int main()
{
char str[10] = "madam";
printf("the given string is %s\n",str);
printf("after reversing :%s",strrev(str));
return 0;
}
strrev() is not a standard C function although it is included in some implementations.
See here for a discussion and a copy-paste strrev() implementation.
If a function is not found, it's assumed to return int by default. That's the reason of your second error. Your code seems fine.
For common/better formatting, please indent with 4 spaces and place spaces after , etc., and add a bit more air between the lines. Here's what I would do:
#include <stdio.h>
#include <string.h>
int main()
{
char str[10] = "madam";
printf("the given string is %s\n", str);
printf("after reversing: %s", strrev(str));
return 0;
}
strrev isn’t a standard c function. Looks like your compiler doesn’t define a function with that name. That leads to the second warning about passing an int to printf. There’s a rule in c that says if a function is encountered undefined it’s assumed to return an int. In that spot in printf you need a string, but strrev isn’t defined, so the compiler assumes it returns int.
To fix this you’ll have to make a strrev function yourself (or copy one from online)
This question already has answers here:
C function calls: Understanding the "implicit int" rule
(3 answers)
Closed 6 years ago.
I recently picked up K&R the second edition to learn C. However on the first program, a simple hello world. It is giving me this compile issue
hello.c:3:1: warning: type specifier missing, defaults to 'int' [-Wimplicit-int]
main()
^
1 warning generated.
My code is
#include <stdio.h>
main()
{
printf("Hello world\n");
}
and I'm compiling it by going into the folder where this program is located and using gcc hello.c -o hello
Ah yes, this is old. Declaring functions or variables without their types is going away.
#include <stdio.h>
int main()
{
printf("Hello world\n");
}
This question already has answers here:
Not including stdlib.h does not produce any compiler error!
(5 answers)
Closed 9 years ago.
I am using the atoi() function which I know is a part of stdlib.h.
Then why is the following code working correctly when I have not included the required header file?
#define _CRT_SECURE_NO_wARNINGS
#include <stdio.h>
int main()
{
char y[10] = "1234";
int z = atoi(y);
printf("%d\n", z);
return 0;
}
If the compiler detects a function in use, not being prototyped, it assumes int as return value. Lucky atoi() returns ìnt, so as the Standard Library is linked by default the symbol atoi() is resolved by the linker successfully.
If you'd had made your compiler log warnings (options -Wall -Wextras -pedantic for gcc) it would have notified you about the missing prototype for atoi().
Btw: It should be
int main(void)
at least.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
What should main() return in C/C++?
Just started coding C about an hour ago, after a few months of basic java coding, and am encountering a problem compiling the basic hello world program.
Here is my code:
#include < stdio.h>
void main()
{
printf("\nHello World\n");
}
and this is what i get back when i try to compile:
Hello.c: In function ‘main’:
Hello.c:13: warning: return type of ‘main’ is not ‘int’
any help would be much apprecated, thanks!
The standard signatures for main are either
int main(void)
or
int main(int argc, char **argv)
Your compiler is simply enforcing the standard.
Note that an implementation may support void main(), but it must be explicitly documented, otherwise the behavior is undefined. Like dandan78 says, a large number of books and online references get this wrong.
it should be
int main() {}
then you should return 0 if the program is terminating correctly or any other number if there was an error. That's an Unix convention, so scripts can check if the program was terminated correctly or an error occurred.
main-function in c has to return an int:
#include < stdio.h>
int main()
{
printf("\nHello World\n");
return 0;
}
Regardless of which prototype you choose for main(), it's return value cannot be void. It has to be int. Many books and tutorials get this wrong and some compilers tend to complain while others do not.