strrev function in C and string input [duplicate] - c

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)

Related

What causes an "incompatible implicit declaration" warning?

I am trying find out the number of characters in a string using the strlen() function. The user inputs a sentence and the code must display the number of characters in the string.
int l;
char string[64];
printf("Enter a statement: \n");
scanf("%s",&string);
l = strlen(string);
printf("%d",l);
The code runs incorrectly with the following warning :
[Warning] incompatible implicit declaration of built-in function 'strlen'
I did find the correct code online, but what exactly is wrong with my logic ? A little help required.
Observation, firstly
The code runs incorrectly with the following warning : [Warning]
incompatible implicit declaration of built-in function 'strlen'
This is because you didn't include the necessary header for strlen.
#include <string.h>
Secondly, here:
scanf("%s",&string);
string is character array and array name itself address, hence while scanning & is not required.
scanf("%s", string); /* remove & */
Also, always check the return value of functions like scanf. Check the manual page of scanf() to know what it returns. For example:
int ret = scanf("%s", string);
if (ret == 1) {
/* success, proceed further */
}
else {
/* proper error handling */
}
The reason for the warning is forgetting to include string.h
What the warning means in detail, is that in older C from the 90s, it was valid to use a function without declaring it. So if you didn't provide the relevant header like string.h where the declaration is found, the compiler would then "guess" what function format you want and try to silently declare the function "between the lines".
The compiler would have implicitly given you this:
int strlen (); // accepts any parameters
But the real and correct strlen declaration is this:
size_t strlen (const char *s);
These aren't compatible types, hence the warning about incompatible declaration. The actual definition of the strlen function matches the latter, but not the former, so this is a bug.
From the year 1999 and beyond, implicit function declarations were removed from the C language, since they were plain dangerous. The gcc compiler still allows obsolete style C though, if you configure it a certain way.
If you are a beginner, you should always compile with a strict and standard compliant setup:
gcc -std=c11 -pedantic-errors -Wall -Wextra
This will swap the mentioned warning for an error, which is nice since this was a program-breaking bug.

C Segmentation Fault within Kali Linux VM

I am trying to create a C program in a Linux VM that uses a char array. I found that every time I try to do anything to the array, e.g. sizeof(), I get a segmentation fault. I wrote a test program that just made an array and got sizeof() as a test, and sure enough I get the same error.
I think this is related to the program running in a VM. Here is the code:
#include <stdio.h>
#include <stdlib.h>
#define ARR_LENGTH(x) sizeof(x) / sizeof(x[0])
int main()
{
printf("Hllo world!\n");
int sent = 10;
char hello[] = {'a','b','c','d','e','f','g','h'};
printf(sizeof(hello));
printf(ARR_LENGTH(hello));
return 0;
}
Am I missing something, or is the VM likely the problem?
You just need to read the printf() documentation carefully. It doesn't take an integer as its first argument.
The problematic lines are:
printf(sizeof(hello));
printf(ARR_LENGTH(hello));
It should be:
printf("%zu\n", sizeof(hello));
printf("%zu\n", ARR_LENGTH(hello));
(%zu is the format specifier to print size_t values).
My gcc compiler produces the warning:
warning: passing argument 1 of ‘printf’ makes pointer from integer
without a cast [-Wint-conversion]
for those two statements. Up the compiler warning levels (in case you don't get them alrready).

Array subscript has type 'char' warning is showed without using arrays

Hi I got this weird warning in this simple code that leaves me completely confused. I searched trough the page and I see that this is a warning to alert users to avoid using chars as index of matrix because they can be signned, but obviously this is not the case.
Here is the code:
#include <stdio.h>
#include <ctype.h>
int main() {
char c='t';
if (isspace(c)==0)
printf ("%c is not a space",c);
return (EXIT_SUCCESS);
}
my question is what is the reason of the warning? is it related with the fact that isspace expects an int as argument?
a warning to alert users to avoid using chars as index of matrix because they can be signned, but obviously this is not the case
Actually, it is the case ... what you see is not what the compiler sees.
is it related with the fact that isspace expects an int as argument?
Yes; isspace is a macro which (in your compiler implementation) accesses an array ... take a look at your ctype.h or ask your compiler to expand macros (e.g., gcc -E) and you will see the array access.
To avoid the warning, use
if (!isspace((unsigned char)c))

Hello.c: In function ‘main’: Hello.c:13: warning: return type of ‘main’ is not ‘int’? [duplicate]

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.

Error: conflicting types for ‘strlen’

I have a program, but I get an error message. Please help me on this.
#include <stdio.h>
#include <string.h>
int print_strlen(char s[]);
main()
{
char s[20];
printf("Enter the string:\n");
scanf("%s\n", s);
}
int print_strlen(char s[])
{
int l;
l = strlen(s);
printf("Length of the string is: %s\n", l);
}
Don't try to prototype strlen yourself. Just include <string.h>, and use the prototype (and function) it already has. Don't try to write a function of your own with the same name (or, at least officially, any other name starting with str).
The conflicting type it's seeing right now is because the standard requires strlen to return a size_t, not an int.
Also note that the function you have named strlen right now is infinitely recursive -- where it (apparently) tries to call the standard strlen, it'll end up calling itself, and since it does that unconditionally, it'll keep recursing forever (or until the system kills it for overflowing the stack, anyway).
strlen is already defined in string.h hence the error. Change your function name to something else.
Call your function something else, there is already a strlen() declared in string.h.
strlen is a standard function declared in <string.h> and you are trying to define it with another prototype (the standard function takes a char const*, not a char*).
What is your error message?
You shouldn't name your function strlen; there is already a strlen function and you know that won't work because you use the real strlen function in your strlen function. Call it something else, like print_strlen. Also, use void instead of int if you're not actually returning anything from print_strlen.
Other problems not directly related include the obvious buffer overflow; what if you entered text more than 20 bytes long?
strlen is a function provided for you in string.h why reinvent the wheel?
#include <stdio.h>
#include <string.h>
main()
{
char s[20];
printf("Enter the string:\n");
scanf("%s\n",s);
printf("Length of the string is: %s\n",strlen(s));
}
would work perfectly fine.

Resources