ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
now is the latter correct because when I tested this it
had the same result both ways and GCC gave no erros
with -Wall
of course this isent compleate code but its kinda a
simple question. If the first code is incorrect can you give me a reason why it would be bad to do that as well.
Sorry just trying to understand.
Not quite certain what the author was getting at. Maybe it was because of the potential confusion between a character data type '%c' and the variable being a string. This could lead to confusion when revisiting the code.
What was the chapter of the book covering and was there anything else to explain why it was 'bad'?
Not quite certain what the author was getting at. Maybe it was because of the potential confusion between a character data type '%c' and the variable being a string. This could lead to confusion when revisiting the code.
graeme.
This is what i think.
"%c" is a character where as you want a string of characters "%s".
O no you dont lol. your just using one char, so i dont really know.;(
Its a book by H.M. Deitel/P.J. Deitel C how to program second edition.
lol no refund needed book was only $4
its an older text book that i picked up somewhere in lack of being able to find anything else
anway im in the section for Formated input/output and its one of the commen programming errors that they spek of
Quote:
Commen programming error 9.3
Using %c to print the first character of a string. The conversion specification %c expects a char argument. A string is a pointer to char. i.e.. a char *.
in looking at that maybe there talking about wierdness such as doing this.
Code:
char test[] ="this is a test";
printf("%c\n",test);
in that case i would see why it would be wrong but what i was doing earlier as being perfectly ok.
Yes the way you coded it was correct. Omitting the element as in
Code:
printf("%c\n",test);
would have been wrong and I think that is the common error they are talking about. You don't end up printing the first elemnt of the array but part of the address.
The message about being wrong is a warning. That warning is based on human error.
Suppose you had a define
MYTEST = "test string"
and in the code you had
char test[] = MYTEST;
Your printf would work ok, as YOU know that MYTEST is long enough.
But suppose, at 1am in the morning, in a rush to get a fix out, you had changed
char test[] = "test";
Where would your expression point to?
printf("%c\n",test[5]);
To avoid errors, you can do what you want to do, but do lint your program before moving it to production. Lint is a wonderful detecter of pitfalls.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.