Code being tried below:
#include <stdio.h>
#include <conio.h>
#include <ctype.h>
int main(void)
{
char ch;
do{
ch=getch();
cprintf("%c", toupper(ch));
} while(ch !='q');
return 0;
}
error below:
C:\Users\Towsif\Desktop\C\sd\main.c||In function 'main':|
C:\Users\Towsif\Desktop\C\sd\main.c|11|warning: implicit declaration of function 'cprintf' [-Wimplicit-function-declaration]|
obj\Debug\main.o||In function main':|
C:\Users\Towsif\Desktop\C\sd\main.c|11|undefined reference tocprintf'|
||=== Build finished: 1 errors, 1 warnings (0 minutes, 0 seconds) ===|
<conio.h> header file is not available in GCC(MinGW/Cygwin) compiler.
The error is not informative and is misleading.Try without using that header file...
EDIT :-
You can't use getch() and cprintf()!
So, instead of them you try getchar() and printf(). Also, there is no need to change the compiler as GCC is considered the best compiler for C language. Actually, you should either skim that portion of book which requires these <conio.h> header files OR alternately just install another C-compiler alongside GCC. Don't remove GCC! Please use GCC only...
Try this code :-
do{
ch=getchar(); // changed getch() to getchar();
printf("%c", toupper(ch)); //changed cprintf() to printf();
}
while(ch !='q');
Related
I was working on a c program with code blocks on windows when I realized i needed the pdcurses library so downloaded it and build it but after importing it into code blocks, when I ran a test code, I got these errors :
the code is:
#include <stdio.h>
#include <stdlib.h>
#include <pdcurses.a>
int main(){
initsrc();
printw("Hello world!\n");
refresh();
getch();
endwin();
return 0;
}
can someone help me resolve this please?
A file with an extension of ".a" is a library file, which is binary. You don't include that in your code with #include.
What you should do instead is #include the header file(s) associated with this library, then link in the library file in the project configuration.
This code was tested to work with PDCurses. Notice you don't need other #include:
#include <curses.h>
int main(void)
{
initscr();
addstr("Hello World!\n");
refresh();
getch();
endwin();
return 0;
}
I am getting an error in my compiler:
Warning: implicit declaration of function 'system'
I added:
system("cls");
To be able to clear the screen, and now I get the error. I am using this code to test:
#include <stdio.h>
int nothing; //random name
int main()
{
printf("this is a msg");
scanf("%d",¬hing);
system("cls");
printf("hello");
getchar();
return 0;
}
This is just a test code, so it's very sloppy. I am new to coding so any help would be appreciated.
For C++: #include <cstdlib>, for C: #include <stdlib.h>.
Or, you can do as follows:
#ifdef __cplusplus__
#include <cstdlib>
#else
#include <stdlib.h>
#endif
if (system("CLS")) system("clear");
You can also see a full article w.r.t Clear the screen.
This warning is reported when a function is called before its declaration. In your case, you haven't included the library stdlib.h at the start of your code. So the compiler sees the call to function before its prototype.
I am learning C.
In this program
I use sleep function to slowdown a count down.
My text book doesn't specify a library I should include to use the sleep function.
So I use it without including any special library for it and it works.
But it gives me this warning message in codeblocks.
I tried to include <windows.h> but still the same warning message appears.
warning D:\Project\C language\trial8\trial8.c|19|warning: implicit
declaration of function `sleep'|
And here is my code.
#include <stdio.h>
int main()
{
int start;
do
{
printf("Please enter the number to start\n");
printf("the countdown (1 to 100):");
scanf("%d",&start);
}
while(start<1 || start>100);
do
{
printf("T-minus %d\n",start);
start--;
sleep(3000);
}
while(start>0);
printf("Zero!\n Go!\n");
return(0);
}
I want to know what does the warning message mean? How important is it? Is there anything that I should do about it? Note that the program works anyway.
The issue is in the libraries (header files):
on Windows:
#include <windows.h> and Sleep(1000); => 1000 milliseconds
on Linux:
#include <unistd.h> and sleep(1); => 1 second
The function sleep is not part of C programming language. So, C compiler needs a declaration/prototype of it so that it can get to know about about number of arguments and their data types and return data type of the function. When it doesn't find it, it creates an Implicit Declaration of that function.
In Linux, sleep has a prototype in <unistd.h> and in windows, there is another function Sleep which has a prototype in <windows.h> or <synchapi.h>.
You can always get away with including header, if you explicitly supply the prototype of the function before using it. It is useful when you need only few functions from a header file.
The prototype of Sleep function in C on windows is:
VOID WINAPI Sleep(_In_ DWORD dwMilliseconds);
Remember, it is always a good practice to supply the prototype of the function being used either by including the appropriate header file or by explicitly writing it. Even, if you don't supply it, compiler will just throw a warning most of the time and it will make an assumption which in most cases will be something that you don't want. It is better to include the header file as API might change in future versions of the Library.
Windows doesn't have the sleep function. Instead, it has Sleep, which takes the number of milliseconds to sleep:
VOID WINAPI Sleep(
_In_ DWORD dwMilliseconds
);
You'll need to either #include <windows.h> or #include <synchapi.h>, depending on the version of Windows you're running. See MSDN for more details.
Update in 2022:
As it is stated on the Linux man page here we need to include unistd.h and should do fine for all OS.
#include <stdio.h>
#include <unistd.h>
int main()
{
sleep(1); /* sleep for 1 second*/
printf("END\n");
return 0;
}
To make it more cross-platform, try this:
#ifdef _WIN32
#include <Windows.h>
#else
#include <unistd.h>
#endif
I am learning to use getline in C programming and tried the codes from http://crasseux.com/books/ctutorial/getline.html
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int atgc, char *argv[])
{
int bytes_read = 1;
int nbytes = 10;
char *my_string;
my_string = (char *)malloc(nbytes+1);
puts("Please enter a line of text");
bytes_read = getline(&my_string, &nbytes, stdin);
if (bytes_read == -1)
{
puts ("ERROR!");
}
else
{
puts ("You typed:");
puts (my_string);
}
return 0;
}
However, the problem is that the compiler keeps returning errors of this: undefined reference to 'getline'.
Could you please tell me what the problem is? Thank you!
I am using Win7 64bit + Eclipse Indigo + MinGW
The other answers have covered most of this, but there are several problems. First, getline() is not in the C standard library, but is a POSIX 2008 extension. Normally, it will be available with a POSIX-compatible compiler, as the macros _POSIX_C_SOURCE will be defined with the appropriate values. You possibly have an older compiler from before getline() was standardized, in which case this is a GNU extension, and you must #define _GNU_SOURCE before #include <stdio.h> to enable it, and must be using a GNU-compatible compiler, such as gcc.
Additionally, nbytes should have type size_t, not int. On my system, at least, these are of different size, with size_t being longer, and using an int* instead of a size_t* can have grave consequences (and also doesn't compile with default gcc settings). See the getline manual page (http://linux.die.net/man/3/getline) for details.
With that change made, your program compiles and runs fine on my system.
I am also using MinGW. I checked MinGW headers and getline() does not appear in any C header, it appears only in C++ headers. This means the C function getline() does not exist in MinGW.
getline isn't a standard function, you need to set a feature test macro to use it, according to my man page,
_POSIX_C_SOURCE >= 200809L || _XOPEN_SOURCE >= 700
for glibc 2.10 or later,
_GNU_SOURCE
before that.
I have this simple code
#include <stdio.h>
#include <OpenGL/glext.h>
#include <OpenGL/gl.h>
int main (int argc, const char * argv[])
{
printf("Hello, World!\n");
return 0;
}
If I comment out the line with "glext.h" it compiles and runs fine in xcode 4 if I uncomment that line I get 345 errors most of them 'expected * before *' ...
What is going on?! both gl.h and glext.h are inside the OpenGL framework but no matter if I incluhe it or not I get the same error. I tried GCC 4.2 as well as LLVM GCC 4.2 and LLVM (in this case 21 semantic and parse errors).
I am sure my lack of experience with C is causing this but I am surprised gl.h has no problem but glext.h has.
Even if I try to compile in from the command line by gcc I get lots of
/System/Library/Frameworks/OpenGL.framework/Headers/glext.h:3137: error: expected ‘)’ before ‘const’
Any ideas?
It's a bug with glext.h. If you look at that file, you'll see that it has a bunch of definitions that use GLenum, but GLenum isn't defined anywhere in that file. So, before you include glext.h, you need to include a file that defines GLenum. The easiest thing to do is to include gl.h first instead of second:
#include <stdio.h>
#include <OpenGL/gl.h>
#include <OpenGL/glext.h>
Switch these two lines around:
#include <OpenGL/glext.h>
#include <OpenGL/gl.h>
And it should work.