warning: incompatible implicit declaration of built-in function ‘printf’ [enabled by default] - c

I'm using the following C code:
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
int main()
{
int file=0;
if((file=open("testfile.txt",O_RDONLY)) < -1)
return 1;
char buffer[19];
if(read(file,buffer,19) != 19) return 1;
printf("%s\n",buffer);
if(lseek(file,10,SEEK_SET) < 0) return 1;
if(read(file,buffer,19) != 19) return 1;
printf("%s\n",buffer);
return 0;
}
After compiling it produces a warning:
warning: incompatible implicit declaration of built-in
function ‘printf’ [enabled by default]
What does it mean and how do I appease the C compiler to not raise the warning?

You need to add #include <stdio.h> to the top of your file.

Related

C invalid initializer on empty line and invalid arguement on a while loop

I am getting those following errors in this code I am not sure what causes them. Since one of them happens on an empty line where nothing happens. The other is on a while loop that doesn't take an argument I believe.
Error message:
test.c: In function ‘main’:
test.c:28:15: warning: assignment to ‘char *’ from ‘int’ makes pointer from integer without a cast [-Wint-conversion]
28 | checker = decrypt(Ciphertext,key,iv);
| ^
test.c:31:8: warning: format not a string literal and no format arguments [-Wformat-security]
31 | printf(key);
| ^~~~~~
/usr/bin/ld: /tmp/ccGWk1m2.o: in function `main':
test.c:(.text+0x57): undefined reference to `readWord'
/usr/bin/ld: test.c:(.text+0x8c): undefined reference to `decrypt'
collect2: error: ld returned 1 exit status
#include <stdio.h>
#include <stdlib.h>
#include <conf.h>
#include <evp.h>
#include <err.h>
#include <string.h>
#include <ctype.h>
unsigned char *readword(FILE *fp);
int decrypt(unsigned char *Ciphertext, unsigned char *key,unsigned char *iv);
int main (void)
{
char *key = NULL;
char *iv = "aabbccddeeff00998877665544332211";
char *Plaintext = "This is a top secret.";
char *Ciphertext = "764aa26b55a4da654df6b19e4bce00f4ed05e09346fb0e762583cb7da2ac93a2";
char *checker;
// invalid initializer on this line for some reason
FILE words;
words = (fopen("words", "r"));
while(1) { // invalid argument when I am not even using one? it's C standard loop right?
key = readword(words);
if (!key){
printf("the key was not found");
break;
}
else{
checker = decrypt(Ciphertext,key,iv);
if(strcmp(checker,Plaintext)){
printf("the correct key is ");
printf(key);
}
free(key);
free(checker);
}
}
return 0;
}

Encountering compiling issues with implicit declaration using the strlwr function for this code

I am writing code that accepts a command-line argument and determines whether or not the argument is in order based on the ASCII values of the argument. Here is what I have as of now:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int in_order(char *word){
int i = 1;
while(word[i] != '\0'){
if(word[i] < word[i-1]){
return 0;
}
i++;
}
return 1;
}
int main(int argc, char *argv[]) {
if (argc < 2){
exit(0);
}
else{
char *word = argv[1];
if(in_order(strlwr(word)) == 1){
printf("In order\n");
}
else{
printf("Not in order\n");
}
}
return 0;
}
When I try to compile this code with the C99 standard, I receive the following warnings and errors:
warning: implicit declaration of function 'strlwr' [-Wimplicit-function-declaration]
if(in_order(strlwr(word)) == 1){
^
warning: passing argument 1 of 'in_order' makes pointer from integer without a cast [enabled by default]
note: expected 'char *' but argument is of type 'int'
int in_order(char *word){
^
undefined reference to 'strlwr'
How can I make use of the strlwr function without having this error occur, and are there any other mistakes I should be aware of? Thanks.
strlwr is not a standard function; it is only found in some versions of string.h. You can find one such string.h online and copy the function’s code into your program.
You could also implement it yourself:
char* strlwr (char* s) {
for (int i = 0; i < strlen(s); ++i)
if (s[i] >= 'A' && s[i] <= 'Z')
s[i] += 'a' - 'A';
return s;
}
the function strlwr is available on cygwin string.h but it is NOT C99
see in /usr/include/string.h
#if __MISC_VISIBLE
char *strlwr (char *);
char *strupr (char *);
#endif
instead of
$ gcc -Wall -std=c99 prova.c -o prova
prova.c: In function ‘main’:
prova.c:25:21: warning: implicit declaration of function ‘strlwr’; did you mean ‘strstr’? [-Wimplicit-function-declaration]
25 | if(in_order(strlwr(word)) == 1){
just drop the -std=c99.
$ gcc -Wall prova.c -o prova
$ ./prova.exe ARD
Not in order
$ ./prova.exe ADR
In order

undefined reference to 'mbstowcs_s'

This is my code. I want to test function mbstowcs_s.
#include <stdio.h>
#include <wchar.h>
#include <stdlib.h>
#include <locale.h>
#include <assert.h>
int main()
{
char MultiByteStr[20] = "a中";
wchar_t WideByteStr[20];
printf("%s\n", MultiByteStr);
char* LocaleInfo = setlocale(LC_CTYPE, "");
printf("%s\n", LocaleInfo);
// required length of WideByteStr
size_t WideStrLen = 0;
mbstowcs_s(&WideStrLen, NULL, 0, MultiByteStr, 0);
assert(WideStrLen < 20);
mbstowcs_s(NULL, WideByteStr, 20, MultiByteStr, WideStrLen);
WideByteStr[WideStrLen] = L'\0';
wprintf(L"%ls\n", WideByteStr);
return 0;
}
I test this code on windows platform successfully, but failed on linux platform. Below is the error message.
test.c: In function ‘main’:
test.c:20:5: warning: implicit declaration of function ‘mbstowcs_s’ [-Wimplicit-function-declaration]
mbstowcs_s(&WideStrLen, NULL, 0, MultiByteStr, 0);
^
/tmp/ccqzP6HF.o: In function `main':
test.c:(.text+0x77): undefined reference to `mbstowcs_s'
test.c:(.text+0xc3): undefined reference to `mbstowcs_s'
collect2: error: ld returned 1 exit status

Output of semaphore values

Here's my code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
#include <semaphore.h>
sem_t s1, s2;
pthread_t foo_tid, bar_tid;
void *foo(void*) {
while(1) {
sem_wait(&s1);
printf("HI ");
sem_post(&s2);
}
}
void *bar(void*) {
while(1) {
sem_wait(&s2);
printf("HO ");
sem_post(&s1);
}
}
int main() {
sem_init(&s1, 0, 0);
sem_getvalue(&s1, &foo_tid);
sem_init(&s2, 0, 0);
sem_getvalue(&s2, &bar_tid);
pthread_create(&foo_tid, NULL, foo, NULL);
pthread_create(&bar_tid, NULL, bar, NULL);
return 0;
}
I'm trying to get the output of the semaphores s1 and s2. But I keep getting these errors:
sema.c: In function 'main':
sema.c:29:3: warning: passing argument 2 of 'sem_getvalue' from incompatible pointer type [enabled by default]
In file included from sema.c:6:0:
/usr/include/semaphore.h:72:12: note: expected 'int * __restrict__' but argument is of type 'pthread_t *'
sema.c:31:3: warning: passing argument 2 of 'sem_getvalue' from incompatible pointer type [enabled by default]
In file included from sema.c:6:0:
/usr/include/semaphore.h:72:12: note: expected 'int * __restrict__' but argument is of type 'pthread_t *'`
I just haven not been able to get rid of this error. If anyone could help me out with this, that'd be greatly appreciated!
From sem_getvalue, values should be int :
Declare int s1_val, s2_val;
sem_init(&s1, 0, 0);
sem_getvalue(&s1, &s1_val);
sem_init(&s2, 0, 0);
sem_getvalue(&s2, &s2_val);
The second parameter is expected to be a pointer to int, where as you are passing in the address of a pthread_t. A pthread_t is not an int on your system.

Errors with gcc when compiling C program

I have a custom shell program in which I have included signal.h, unistd.h, and stdio.h. I was originally working on this in RedHat Enterprise (not sure exactly what version, but not too old) and I was able to use gcc on my program and it compiled fine and ran fine. Now I moved it over to Ubuntu and gcc is giving me some errors, the first of which is conflicting types for 'getline()'. Some other errors say incompatible implicit declaration of built-in function strlen. I have overridden the functions in question, why was this working in RedHat but not in Ubuntu? Linux is not my thing so please speak plainly. Let me know if you need more error details.
/* define a global input buffer */
#include <signal.h>
#include <unistd.h>
#include <stdio.h>
#define MAXARG 512
#define MAXBUF 512
#define BUFFER_SIZE 50
#define MAX_COMMANDS 10
char buffer [BUFFER_SIZE];
static char *prompt = "MYSHELL>";
static char inpbuf[MAXBUF];
static char *arg[MAXARG+1];
static char tokbuf[2*MAXBUF];
static char *tok = tokbuf;
char history[MAX_COMMANDS][MAXBUF];
int cmd_num;
void getline(void);
void getline() {
int length;
length = read(0, inpbuf, MAXBUF);
if (length == 0) {
printf("\n");
exit(0);
}
inpbuf[length] = '\0';
}
void processline() {
char *ptr = inpbuf;
int narg;
for (narg=0;;) {
arg[narg] = tok;
for (; *ptr == ' ' || *ptr == '\t'; ptr++)
;
while(*ptr != ' ' && *ptr != '\t' && *ptr != '\n' &&
*ptr != '\0' && *ptr != ';' && *ptr != '&')
*tok++ = *ptr++;
*tok++ = '\0';
if (narg < MAXARG)
narg++;
if (*ptr == '\n')
break;
}
// clear the input buffer
for (ptr = inpbuf; *ptr != '\n'; ptr++)
*ptr = ' ';
if (narg != 0) {
arg[narg] = NULL;
}
}
void handle_SIGINT()
{
write(STDOUT_FILENO, buffer, strlen(buffer));
}
int main()
{
int pid, exitstat, ret;
struct sigaction handler;
handler.sa_handler = handle_SIGINT;
handler.sa_flags = 0;
sigemptyset(&handler.sa_mask);
sigaction(SIGINT, &handler, NULL);
strcpy(buffer, "Caught Control C\n");
while (1) {
printf("%s ", prompt);
fflush(stdout);
getline();
processline();
if ((pid = fork()) < 0){
fprintf(stderr, "myshell: error\n");
return (-1);
}
if (pid == 0) {
execvp(*arg, arg);
fprintf(stderr, "%s\n", *arg);
exit(127);
}
waitpid(pid, &exitstat, 0);
}
return 0;
}
Simplest solution would be to rename your getline() function, e.g. to my_getline()
incompatible implicit declaration of built-in function strlen
Include <string.h>
conflicting types for 'getline()
<stdio.h> already contains a declaration of getline, so make sure that nowhere in your code you have redeclared/redefined getline()[with a different prototype].
gcc -Wall typoknig.c
typoknig.c:19: error: conflicting types for ‘getline’
//usr/include/stdio.h:671: note: previous declaration of ‘getline’ was here
typoknig.c:21: error: conflicting types for ‘getline’
//usr/include/stdio.h:671: note: previous declaration of ‘getline’ was here
Two separate declarations of getline which Andy had recommended that you use my_getline() since the former is already part of stdio.h.
typoknig.c: In function ‘getline’:
typoknig.c:27: warning: implicit declaration of function ‘exit’
typoknig.c:27: warning: incompatible implicit declaration of built-in function ‘exit’
That can't be good, man exit says right at the top:
#include <stdlib.h>
void exit(int status);
perhaps you need to include stdlib.h? What does gcc assume is the signature of an undeclared function?
typoknig.c: In function ‘handle_SIGINT’:
typoknig.c:59: warning: implicit declaration of function ‘strlen’
typoknig.c:59: warning: incompatible implicit declaration of built-in function ‘strlen’
Ouch, man strlen to the rescue:
#include <string.h>
Fortunately, string.h will help out with the next one and we already nailed exit:
typoknig.c: In function ‘main’:
typoknig.c:70: warning: implicit declaration of function ‘strcpy’
typoknig.c:70: warning: incompatible implicit declaration of built-in function ‘strcpy’
typoknig.c:85: warning: incompatible implicit declaration of built-in function ‘exit’
Ain't that pre-processor nifty?
typoknig.c:87: warning: implicit declaration of function ‘waitpid’
typoknig.c:64: warning: unused variable ‘ret’
Sayeth man waitpid:
#include <sys/types.h>
#include <sys/wait.h>
Line 64 is left as an exercise for the reader.
Getline() is not part of C standard. It is a GNU extension. In c++ Getline() is standard.
So adding to your code right before your #includes
#define _GNU_SOURCE
should fix the warning. Also see "man getline" on linux.

Resources