Error: Declaration of non-variable 'strlen' in 'for' loop initial declaration - C - c

The title is the exact error my compiler(geany on ubuntu) gives me when I try to compile. The part of code in question is the following:
for(int q=strlen(stringA)-1;q>0;q--)
{
//do stuff
}
I've already set the C99 mode and the initialization of variables in the 'for' header is accepted. Is what I did simply illegal in C?

I assume you are missing an include. Try:
#include <string.h>

Related

Apparently erroneous implicit declaration of function warning

I am getting a warning for lstat being implicitly declared, but it should be explicitly declared in the included header.
Compiling the following:
// Standard library
#include <stdio.h>
// Feature test macro
#define _POSIX_C_SOURCE 200809L
// POSIX inclusions
#include <sys/stat.h>
// Function
int main()
{
struct stat x;
char* s = "/data/file";
char* t = "/data/link";
lstat(s,&x);
printf( "%i\n", S_ISLNK(x.st_mode) );
lstat(t,&x);
printf( "%i\n", S_ISLNK(x.st_mode) );
return 0;
}
With the command:
clang -std=c11 test.c
Produces:
test.c:16:4: warning: implicit declaration of function 'lstat' is invalid in C99 [-Wimplicit-function-declaration]
lstat(s,&x);
^
1 warning generated.
Running the output with a regular file at "/data/file" and a link at "/data/link" gives the following output:
0
1
So lstat is obviously being seen by the compiler and used.
If I remove the "-std=c11" option, there is no warning. I get essentially the same thing in gcc, no warning with no option, similar warning with the option.
I am sure I am missing something since both gcc and clang do the same thing, so:
How can I be implicitly defining a function but still accessing the full definition in sys/stat.h?
What can I do to not get this warning?
Feature test macros should be defined before any header is included, including the standard C ones.
So move
#define _POSIX_C_SOURCE 200809L
at the beginning of your file.
If you misplace the feature test macro, the function may not be declared in the header at all. In C89 it is allowed to call an undeclared function. It will be implicitly declared. That is not allowed since C99, but the compilers are choosing to just give a warning about it instead of terminating compilation, and continue to declare the function implicitly.
Without a prototype default argument promotion will be done on the call arguments to the implicitly-declared function, which for the pointers you pass means that they are passed without any conversion. So the arguments you are giving happen to coincide with the actual parameter types of lstat. So there is no problem, but this is obviously dangerous in general, so don't rely on the implicit declaration.

Behavior of extern keyword in C [duplicate]

This question already has an answer here:
Why extern int a ; initialization giving error locally but not globally? [duplicate]
(1 answer)
Closed 5 years ago.
I surprised when compiled following program on GCC compiler. It's successfully worked. Compiler gives only warning.
warning: 'i' initialized and declared 'extern' [enabled by default] extern int i = 10; ^
My code:
#include <stdio.h>
//Compiler version gcc 4.9
extern int i = 10;
int main()
{
printf("%d\n",i);
return 0;
}
Output:
10
Why doesn't give compiler error? Is it undefined behavior?
You should not put your main function body in a header, but rather in a .c-file. Vice versa, you should not put extern in a .c-file but only in a header. That is the difference between declaration and definition.
Extern means, make this variable known, but there is no memory to be reserved for it. The compiler now says: Ok, I know you want this variable to be used, but it is only promised to be there, not actually defined.
The compiler anyway does not know about other objects (other .c-files) that might define this variable. So it keeps it to the linker to actually try to gather all variables.
If the linker now does not find that variable elsewhere, it implicitly makes the variable local, but warns about the broken C-standard.

What kind of error is there in C code snippet?

I am examining errors in different C programs and differentiating between them.
Currently, I am confused what type of error is there in this code.
#include <stdio.h>
int main(void) {
in/*hello*/z;
double/*world*/y;
printf("hello world");
return 0;
}
When i run this program, i get compilation error as :
prog.c: In function 'main':
prog.c:4:5: error: unknown type name 'in'
in/*this is an example*/z;
^
prog.c:5:30: warning: unused variable 'y' [-Wunused-variable]
double/*is it an error?*/y;
^
prog.c:4:29: warning: unused variable 'z' [-Wunused-variable]
in/*this is an example*/z;
^
I know that warning will not prevent from compiling, but there is an error
error: unknown type name 'in'
So, Is this syntax or semantic error ?
There is obviously a syntax error.
First of all we have to clarify a thing: you have to understand clearly what's the difference between syntax and semantics.
Syntax are the "grammar rules" of a programming language, when the compiler does not compile, you've done syntax errors.
When you do a semantics error you have done a code that compiles successfully, but, when executed does things you do not want to.
C is a strong-typed language: this means that you have to declare variables before using them.
You have done a couple of errors, but don't worry, let's analyze them together.
First error: you used a type of variable not possible: and the compiler simply showed it up.
For the "in" type i'm assuming that you meant int, but when you code you have constantly to ask yourself: "Is this variable useful now?"
The answer to this question is: "No", because you just want to make an output.
So the correct implementation of this simple procedure is:
http://groups.engin.umd.umich.edu/CIS/course.des/cis400/c/hworld.html
Hope that this helps.
Bye.
Gerald
The code does not compile, so this is a syntax error.
A syntax error is when the code is not valid at all, and cannot be compiled. Such as trying to declare a variable of a type that does not exist (as in your snippet), or performing an invalid assignment, or many other things.
5 = a;
That is a syntax error because you cannot assign a value to a number.
int if;
That is a syntax error because if is a keyword that cannot be a variable name.
A semantic error is code that is valid and compiles but does not do what you would like. Consider the following simple function to square a number:
int square (int value)
{
return value ^ 2;
}
That function does not square the number passed to it but rather performs a bitwise XOR. Yet, in some other languages, ^ 2 would be the syntax to square a number, so this is a semantic error that could conceivably exist in C code.
I think it is typical typo:
in/*hello*/z;
and you wanted int type for your variable z:
int /*hello*/ z;

C constants throwing compile-time errors

Several people have commented on my C code here, saying that I should use constants as loop counters, rather than hard-writing them. I agree with them, since that is my practice when writing Java code, but I'm having compile-time errors thrown when I try to use constants in array declarations and loop conditionals.
To declare a constant in C, the syntax is #define NAME value.
In my code, I have two constants,BUFFER is the file read buffer, and PACKED is the output array size.
I use BUFFER to initialize char inputBuffer[BUFFER]; as a global variable, which works, but when I try to use PACKED
#define PACKED 7; // this line is in the header of file, just below preprocessors
int packedCount;
char inputPack[PACKED]; //compression storage
for (packedCount=0; packedCount<= PACKED; packedCount++){
I get am error: expected ‘]’ before ‘;’ token at char inputPack[PACKED] AND
error: expected expression before ‘;’ token in the loop initialization line. Both errors disappear when I replace PACKED with 7.
You obviously are not posting the code exactly as it appears in your source file.
At the very least, you are missing the semicolon after char inputPack[PACKED].
I strongly suspect that your real source has a semicolon at the end of your macro declaration, which would cause the error. Macro definitions should not be terminated with a semicolon.
there is a ; missing after char inputPack[PACKED]
Try using something other than PACKED, e.g. PACKEDSIZE. It could be that your compiler uses PACKED for something else (e.g. related to struct packing). Also, as other answers mention, you're lacking a ;

"Implicit declaration" warning

For this code:
int i=0; char **mainp;
for(i=0;i<2;++i)
{
mainp[i]=malloc(sizeof(char)*200);
if(!scanf("%[^#],#",mainp[i]))
break;
if(i<2)
scanf("%[^#],#",mainp[i]);
}
GCC emits the warnings:
warning: implicit declaration of function ‘scanf’
warning: incompatible implicit declaration of built-in function ‘scanf’
warning: ‘mainp’ may be used uninitialized in this function
And I get a segmentation fault at runtime
input:(P>Q),(Q>R),-R#-P
output:
(P>Q),(Q>R),-R
(empt slot)
i expected to give me
(P>Q),(Q>R),-R
-P //where should i fix in my code such that it gives me expected
//output
Problem #1:
warning: ‘mainp’ may be used uninitialized in this function
You need to allocate memory for the array of arrays first.
char **mainp = malloc(sizeof(char*)*2);
Problem #2:
warning: implicit declaration of function ‘scanf’
warning: incompatible implicit declaration of built-in function ‘scanf’
You need to include stdio.h at the top of your file:
#include <stdio.h>
Problem #3: (Not included in your compiling warnings)
Remember to free both the allocated array members and also the array of array address.
gcc expects this line at the beginning of your file:
#include <stdio.h>
and a declaration of mainp like this one:
char *mainp[2];
You shouldn't use functions without declaring them; you used scanf, but at no point in your code is scanf declared. Since it's a standard library function it's declared in one of the standard headers, stdio.h, so you just need to include it:
#include <stdio.h>
Brian's answer is good for the other part

Resources