Why am I getting this message? The compiler is clang. Here is a simple program where it occurs for examples sake:
#include<stdio.h>
int fib(int);
int main()
{
int i;
scanf("%d",&i);
printf("The fibonacci number that is %i'th in the sequence is %i \n", i, fib(i));
return 0;
}
int fib(int n)
{
if (n==1 || n==0) return 1;
else return fib(n-1)+fib(n-2);
}
Assuming C
<stdio.h> is one of the standard C headers. Your compiler complains that it can not find this header. This means that your standard library is broken.
Consider reinstalling your compiler.
Assuming C++
<stdio.h> is the C standard header, with C++ we use <cstdio> instead. Though <stdio.h> is still required to exist in C++, so this probably isn't the problem.
Apart from these assumptions, it seems most likely (by your coding style and tags) that you are using C. Try this as some example code. This is guaranteed (by me) to compile on a working C compiler, if it doesn't then your compiler is horribly broken and you must install another one/reinstall:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char **argv) {
printf("Hello World!\n");
return EXIT_SUCCESS;
}
I got a runtime error similar to this while using C++ in Visual Studio 2017. The solution in my case was to simply clean and rebuild the solution
Related
I am playing with openssl in C and getting some warnings I like to get rid of and am unsure how to tackle it as the examples I had found seem to do what I am emulating.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <openssl/sha.h>
int main(int argc, char** argv)
{
int i;
unsigned char resSHA512[SHA512_DIGEST_LENGTH];
unsigned char unhashed[1024];
printf("Input string to hash: ");
fgets(unhashed, sizeof(unhashed), stdin);
unhashed[strcspn(unhashed, "\n")] = 0;
SHA512(unhashed, strlen(unhashed), resSHA512);
printf("\nSHA512: ");
for ( i = 0; i < SHA512_DIGEST_LENGTH; i++ )
{
printf("%02x", resSHA512[i]);
}
printf("\n");
return 0;
}
I also got an alert that SHA512 has been deprecated by open when I compile on my Mac using openssl I got through it's git repository. I get no warnings in Linux unless I used the -Wall flag with gcc.
Any help is greatly appreciated, as I am trying to learn new things!
Thank you
What you're reading in to unhashed is a string, so you should declare it as an array of char. That will address the warning with the string-related functions.
That leaves you with a warning when calling SHA512 which expects an unsigned char for its first argument. In this case you can safely cast the argument to unsigned char *.
The warning you see on Mac is because the entire OpenSSL library was marked deprecated on that OS in favor of its own crypto library.
I m Writing program for dynamic memory allocation. In this program i getting error of undefined symbol _msize. I have also include . Please help me with this.
/* Example of _msize */
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
void main()
{
long *buffer;
size_t size;
buffer = (long *)malloc(100 * sizeof(long));
size = _msize(buffer);
printf("The size of the buffer is %d\n", size);
}
The _msize is not Standard C.
It is from Microsoft Visual C++(MSVC), and you need to include <malloc.h>, as you already did, and use a Microsoft Compiler (e.g. within the Visual Studio).
PS: Unrelated to your problem: What should main() return in C and C++?
#include <stdio.h>
int read_next_line()
{
int ch;
int flag=0;
ch=getchar();
while(ch!= EOF && ch!='\n') {
ch=getchar();
flag=1;
}
return flag || (ch=='\n');
}
int read_all_lines()
{
int linecount=0;
int isvalid;
while(!feof(stdin)) {
isvalid=read_next_line();
linecount=linecount + isvalid;
}
return linecount;
}
main {
read_all_lines();
}
The above code gives an error saying main does not have a type.
How to solve the problem ?
main is a function that returns a value like other functions. It is up to you if it returns nothing void main(void){ } or an int value, 0 is returned in POSIX systems if no error encountered. More sophisticated but readable is to put exit(EXIT_SUCCESS);.
int main(void){
...
}
The proper prototype in most environments is:
int main(int argc, char *argv[])
This means:
main() is a function that returns an int.
It has one argument called argc that is the number of command-line arguments.
It has one argument called argv that is an array of those arguments.
It should be int main() instead of simply main
Since C98 I think, C disallows to declare a function without using the return type for the result anymore. You have to declare main as
int main ()
{
...
}
This is the minimum requirement to make your code compilable. In previous releases of the C language, it was allowed to declare a function like you have done. Implicitly, the compiler assumed it to be an int function without any complaint. But, as for sure you have your compiler configured to compile post C98 or later code, it complaints with an error. Previous versions of actual compilers issue normally a warning, telling you main will be assumed to be an int function, and this is to be able to compile old pre-ANSI code.
By the way, next time, don't limit yourself to explain what you have coded. Cut and paste your actual code here, and also the exact error you got from the compiler (probably, it was only a warning, but you didn't include it, so we'll never know) Probably the mistake is not where you think of, and that way saves us time and you the need of receiving this complaint.
I have used the #pragma directive inside functions without error or warning(especially #pragma pack()).But the following code shows the warning incompatible implicit declaration of built-in function 'printf'|:
int main(void)
{
printf("Trial");
}
#include<stdio.h>
Further, here's is an extract from a book I have.The author has bad reviews on SO,especially for his generous use of void main(),but still I feel no author can be that bad to claim the following without reason:
Each of these preprocessor directives begin with a #
symbol. The directives can be placed anywhere in a program but
are most often placed at the beginning of a program, before the
first function definition.
So can you tell me whether it's mandatory to use some preprocessor directives like #include at the top of the program while others like #pragma can be used anywhere in the program?
Edit After OUAH's remark I tried the following, but it doesn't give warning,it gives a big pile of errors.LOL.
int main(void)
{
#include<stdio.h>
printf("Trial");
}
Think of it this way. The content of the included file is simply inserted at the point in the file where the #include directive appears. The resulting code needs to be syntactically correct for the language that you are programming in.
Confider the following file:
int a;
int foo();
int main()
#include "myheader.h"
int foo()
{
return 0;
}
And the file myheader.h contains:
{
return foo();
}
The code that the compiler will see after the preprocessor has processed the #include directive is:
int a;
int foo();
int main()
{
return foo();
}
int foo()
{
return 0;
}
This is valid C syntax. Such use of the #include directive is not recommended, but it gives you an idea of what it means. If the myheader.h file had the following content:
this is some garbage
which is not proper C
Then the resulting code will be:
int a;
int foo();
int main()
this is some garbage
which is not proper C
int foo()
{
return 0;
}
You can use #include at any point in the file. It results in literal inclusion of the content of the included file at that point. The reason you get a undeclared message for printf() in your code is that C requires a function be declared before use, and stdio.h has that declaration. Which is why it needs to be before it's use. And why it cannot be included in main() in the latter example is because on inclusion (expansion), it results in syntactically incorrect C code.
An #include directive can be placed anywhere in a source file, but in C an identifier can usually not be used before it has been declared. That's the reason why you put the #include directive at the begining of your source file.
void foo(void)
{
printf("Hello world\n");
}
extern int printf(const char *, ...); // Error, the declaration should be put
// before the actual call
"#pragma" directive will be ignored by C compiler since it considers the lines with "#" tag as comments. Looks like you are using openmp. The OpenMP compiler considers these(#pragma) as parallel directives. Hope this helps.
I have installed the gcc compiler from this sudo apt-get install build-essential command
and my program code is
#include<stdio.h>
main()
{
int *b;
b = (int*)malloc(10*sizeof(int));
printf("b=%u\n\n",b);
printf("b+1=%u\n\n",(b+1));
printf("b+2=%u\n\n",(b+2));
b[2]=4;
printf("*(b+2)=%d\n\n",*(b+2));
}
when i try to compile this program from cc -c program.c command
then i get some error
You're missing #include <stdlib.h> (for malloc), and the format strings are wrong. Use %p to print pointers.
Also, you don't need to (and probably shouldn't) cast the return value of malloc (in C).
And the correct signature for main without parameters is:
int main(void)
Corrected code:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int *b;
b = (int*)malloc(10*sizeof(int));
printf("b=%p\n\n", (void*) b);
printf("b+1=%p\n\n",(void*) (b+1));
printf("b+2=%p\n\n",(void*) (b+2));
b[2]=4;
printf("*(b+2)=%d\n\n",*(b+2));
return 0;
}
I don't know why it worked in the video, it's probably using some strange non-standard compiler.
But your errors are because you are using int instead of unsigned int and you pass pointers to printf when it expects unsigned int.