LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 12-09-2005, 03:17 PM   #1
exvor
Senior Member
 
Registered: Jul 2004
Location: Phoenix, Arizona
Distribution: LFS-Version SVN-20091202, Arch 2009.08
Posts: 1,488

Rep: Reputation: 67
C string and printf question


Ok in my book here it says doing the following would be bad

Code:
 
 
char test[] =" test string"; 
  
 printf("%c\n",test[5]);
I would suppose you would have to do the following
Code:
 

char test[] =" test string"; 

   char gdb; 
   
   gdb = test[5]; 
   printf("%c\n",gdb);
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.
 
Old 12-09-2005, 04:01 PM   #2
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
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'?

graeme.
 
Old 12-09-2005, 04:20 PM   #3
paulsm4
Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Don't believe everything you read...

If that's really what the author said, I'd consider getting a refund ;-)
 
Old 12-09-2005, 07:51 PM   #4
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
Quote:
Originally Posted by graemef
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.;(

Last edited by dmail; 12-09-2005 at 07:54 PM.
 
Old 12-10-2005, 08:19 AM   #5
exvor
Senior Member
 
Registered: Jul 2004
Location: Phoenix, Arizona
Distribution: LFS-Version SVN-20091202, Arch 2009.08
Posts: 1,488

Original Poster
Rep: Reputation: 67
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.

Last edited by exvor; 12-10-2005 at 08:23 AM.
 
Old 12-10-2005, 08:44 AM   #6
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
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.

graeme.
 
Old 12-10-2005, 09:52 AM   #7
arunvk
Member
 
Registered: Nov 2005
Location: India
Distribution: Fedora 11
Posts: 194

Rep: Reputation: 30
i guess the second is recommended because its easier to debug a program.

and also for the sake of readability i guess. there r many other reasons but i think this is the most logical explaination
 
Old 12-10-2005, 10:53 AM   #8
Lsatenstein
Member
 
Registered: Jul 2005
Location: Montreal Canada
Distribution: Fedora Core 6 XEN
Posts: 196
Blog Entries: 1

Rep: Reputation: 36
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
string question AquamaN Programming 2 10-20-2005 06:17 PM
c printf question. fredgt Programming 5 09-09-2004 05:06 AM
io related question, when printf is called blackzone Programming 2 07-26-2004 05:41 AM
std string class and printf The_Nerd Programming 3 06-28-2004 10:46 PM
Simple printf question AMMullan Programming 3 02-13-2004 10:12 PM


All times are GMT -5. The time now is 02:53 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration