how can I use getline function in c? [duplicate] - c

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

Related

Warning initialization makes integer from pointer without a cast with fgets [duplicate]

This question already has answers here:
(Why) is using an uninitialized variable undefined behavior?
(7 answers)
Closed 6 years ago.
I want want to read a character string, over the stdin, so I have chosen fgets. But I got this warning: initialization makes integer from pointer without a cast.
Here is my code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stddef.h>
#define MAX_LINIA 301
int main(int argc,char *argv[]){
char *buffer;
printf("Enter the input\n");
if (fgets(buffer,MAX_LINIA-1,stdin)==NULL) printf("Error")
else printf("%s", buffer);
return 0:
}
You are using buffer without initialising it, which causes undefined behaviour.
You don't need to use pointer, and can use char array instead (given the max size already defined):
char buffer[MAX_LINIA];
The fgets then be better written as:
if(fgets(buffer,sizeof(buffer),stdin)==NULL)

implicit declaration of function ‘memset’ [-Wimplicit-function-declaration] [duplicate]

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

How does atoi() function works without including standard library? [duplicate]

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.

strfry(char *__string) returns int?

So I'm new to C and I'm playing around with functions in the GNU C Library when I come across https://www.gnu.org/software/libc/manual/html_node/strfry.html#strfry
Intrigued, I wrote a little tester program:
1 #include <stdio.h>
2 #include <string.h>
3
4 main ()
5 {
6 char *str = "test123abc";
7 char *other;
8
9 other = strfry(str);
10 printf("%s\n", other);
11 return 0;
12 }
gcc test.c outputs test.c:9: warning: assignment makes pointer from integer without a cast
Why?
/usr/include/string.h has the following entry:
extern char *strfry (char *__string) __THROW __nonnull ((1));
How can a char *function(...) return int?
Thanks
Since strfry is a GNU extension, you need to #define _GNU_SOURCE to use it. If you fail to provide that #define, the declaration will not be visible and the compiler will automatically assume that the function returns int.
A related problem, as pointed out by perreal, is that it is undefined behavior to modify a literal string. Once you make the declaration of strfry visible to the compiler, this will be duly reported.
Do note that the strfry function and its cousin memfrob are not entirely serious and are rarely used in production.
To have strfry available, you need
#define _GNU_SOURCE
otherwise the prototype is not exposed and the implicit declaration is assumed to return an int.
The problem is you don't have a prototype in scope for strfry() and the compiler assumes it returns an int. When it wants to assign that int to a char* it complains with the message you specify.
According to my man pages, you need to #define _GNU_SOURCE at the very top of your source code, especially before standard #includes
#define _GNU_SOURCE
/* rest of your program */
You can't modify a literal string:
#define _GNU_SOURCE
#include <stdio.h>
#include <string.h>
int main () {
char *str = "test123abc";
char other[256];
strcpy(other, str);
strfry(other);
printf("%s\n", other);
return 0;
}

c syntax problem w/some changes now

Right now I am only trying to get my getline() function to work. I have the code from the book and it seems to be identical, but I cant get it to compile. This is homework but this part should be just copying from the book.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
//error list
#define ENDOFFILE = -1;
#define TOOMANYNUMS = -2;
#define LIMIT = 256;
//functions declared
int get_line(char line[], int);
//main
main(){
char line[255];
int num[6];
printf("Please input numbers %c: ", line);
get_line(line,LIMIT);
}
//functions
int get_line(char s[],int lim){
int c, i;
for (i=0;i<lim-1 && (c=getchar())!=EOF && c!='\n'; ++i)
s[i] = c;
if(c=='\n'){
s[i]=c;
++i;
}
s[i]='\0';
return i;
}
Now (edited at 10:22) I only get one error:
18 - expected expression before equal
conflicting types for 'getline'
getline might be a function in your standard library, e.g. thisone. If you want to reimplement it, give it a different name.
too few arguments to function 'getline'
You are calling getline() in main() without any arguments, but a few lines above you state that getline takes a char[] and an int. call it like getline(line,sizeof line);
Right now I am only trying to get my getline() function to work.
getline() is a name of Linux function, declared in the stdio.h. C compiler complains that there are two conflicting declarations.
Simply give your getline() function a different name.
Edit1: That:
#define ENDOFFILE = -1;
Should be
#define ENDOFFILE -1
No =, no ; needed for preprocessor directives.
The problem appears to be that the system you are compiling this on appears to have a getline() function already defined, and your definition is conflicting with that. It appears that glibc, the C library used on Linux, has a non-standard getline() function declared in stdio.h. It shouldn't be defined unless you include a line like #define _GNU_SOURCE to opt-in to including non-standard functions, but it may be that this is pre-defined based on how you are compiling your code.
The easiest solution would be to rename your function to something else, but you could also try and find in your compiler options why GNU extensions are being turned on.
Now that you've edited your code, your second problem is that your #define lines are wrong. You don't need an equal or semicolon; these are processed by the preprocessor, which has a different syntax than C, and all you need to do is write #define NAME VALUE.
The proper syntax would be:
#define ENDOFFILE -1
#define TOOMANYNUMS -2
#define LIMIT 256
you need to realize:
it is general an error to write
#define macro like what you wrote,because #define macro is
simply string replacement,so this
statement get_line(line,LIMIT) is
actually get_line(line,=256;)
processed by the compiler,then the
compiling error occurred. Just
change #define LIMIT =256; to
#define LIMIT 256 would be ok.
as is mentioned in the previous
replies,never write codes that
library provided,getline is a function defined in stdio.h.
hope this helps.

Resources