yaylitzis
yaylitzis

Reputation: 5534

print text via pointer

i am learning now c and i come up with this example, where i can print a text using pointers.

#include <stdio.h>

main ()
{ 
    char *quotes = "One good thing about music, when it hits you, you feel no pain. \"Bob Marley\"\n";
    printf(quotes);     
}

I get a warning from the compiler "format not a string literal and no format arguments" and when I execute the program it runs successfully.

I read some other questions here that they had the same warning from the compiler but I didn't find an answer that fits me. I understood the reason why i get this message:

This warning is gcc's way of telling you that it cannot verify the format string argument to the printf style function (printf, fprintf... etc). This warning is generated when the compiler can't manually peek into the string and ensure that everything will go as you intend during runtime...

Case 3. Now this is somewhat your case. You are taking a string generated at runtime and trying to print it. The warning you are getting is the compiler warning you that there could be a format specifier in the string. Say for eg "bad%sdata". In this case, the runtime will try to access a non-existent argument to match the %s. Even worse, this could be a user trying to exploit your program (causing it to read data that is not safe to read). (See the answer)

but what i have to add in my case to in order to have not warnings from the compiler?

Upvotes: 4

Views: 1242

Answers (6)

Kundu
Kundu

Reputation: 4054

Try this:

#include <stdio.h>

main ()
{ 
char *quotes = "One good thing about music, when it hits you, you feel no pain. \"Bob Marley\"\n";
puts(quotes);      //Either 
printf("%s",quotes);//or
return 0;
}

Upvotes: 0

You have to specify format string - in simplest form:

char *quotes = "One good thing about music(...)\n";
printf("%s", quotes);

or, you can use format string to decorate output:

char *quotes = "One good thing about music(...)"; // no newline
printf("%s\n", quotes); // newline added here

or, if you don't want to mess with format strings:

char *quotes = "One good thing about music(...)"; // no newline
puts(quotes); // puts() adds newline

or

char *quotes = "One good thing about music(...)\n";
fputs(quotes,stdout);

Upvotes: 2

Serve Laurijssen
Serve Laurijssen

Reputation: 9733

You are getting the warning because it is dangerous when the string you are printing contains '%'. In this line it makes no sense for percents but when you want to print this for instance:

int main ()
{ 
    int percent = 10;
    char *s = "%discount: %d\n";
    printf(s, percent);     
    return 0;
}

your program will likely crash when printf encounters the second percent and it tries to pop a value from the stack from printf. When you want to print a percent sign use: "%%discount:"

Upvotes: 0

Saqlain
Saqlain

Reputation: 17918

This warning is gcc's way of telling you that it cannot verify the format string argument to the printf style function (printf, fprintf... etc). This warning is generated when the compiler can't manually peek into the string and ensure that everything will go as you intend during runtime. Lets look at a couple of examples.

So as other suggested explicitly use format specifier to tell the compiler...i.e.

printf("%s",quotes);

Upvotes: 0

Leigh
Leigh

Reputation: 12486

Change it to printf("%s", quotes); which adds the specifier that quotes is a 'string', or array of char.

Upvotes: 4

sukhvir
sukhvir

Reputation: 5555

You need to tell printf what is it that you are printing. %s descriptor will tell printf that you are printing a string.

Format of printf = ("descriptor of what type of data you are printing",variable holding the data);

descriptor for strings is %s, for characters %c, for int %d

Change printf to:

printf("%s",quotes);

Upvotes: 3

Related Questions