LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
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 05-03-2011, 03:01 AM   #1
trist007
Senior Member
 
Registered: May 2008
Distribution: Slackware
Posts: 1,052

Rep: Reputation: 70
A question about strings...


This question is about C. Say I have a string 'test'. By looking at the chars we have { 't', 'e', 's', 't', '\0' }

Say I wanted to add a ': ' after it so that it becomes
{ 't', 'e', 's', 't', ':', ' ' }
I use the following code to implement this.
Code:
char str[16] = "test";
int len;
len = strlen(str);
len = len +2;
str[len - 2] = ':';
str[len - 1] = ' ';
str[len] = '\0';
So that should do it because the string is null-terminated. Yet when I run a
Code:
printf("%s", str);
Nothing gets printed. However, if I do the following instead
Code:
printf("%s\n", str);
It prints just fine. Why is that? I could always just do
Code:
write(STDOUT_FILENO, str, sizeof(str));
which works fine but I'd like to understand why printf without the newline prints nothing.

Last edited by trist007; 05-03-2011 at 03:07 AM.
 
Old 05-03-2011, 03:31 AM   #2
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
Quote:
Originally Posted by trist007 View Post
Yet when I run a
Code:
printf("%s", str);
Nothing gets printed.
Usage of the \n in the printf() function will force the standard-out stream to be flushed. Since you haven't a newline above, the string is not printed. Practice with either of these:
Code:
printf("%s\n", str);
or with:
Code:
printf("%s", str);
printf("\n");
Btw, you could have saved yourself the trouble when augmenting the string using strncat().
Code:
strncat(str, ": ", sizeof(str));
Typically, however, one does not declare a string larger than necessary. Thus memory allocation comes into play. Read up the usage of malloc() and realloc(). You would need to declare a char pointer versus a char array when using these functions.
 
Old 05-03-2011, 03:52 AM   #3
trist007
Senior Member
 
Registered: May 2008
Distribution: Slackware
Posts: 1,052

Original Poster
Rep: Reputation: 70
Thanks that explains it.
 
  


Reply



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
[SOLVED] A question about generating base64 strings... trist007 Linux - Newbie 2 03-16-2011 01:21 PM
C prog, null strings question hk20 Programming 2 08-30-2008 03:57 PM
Using ; in command strings question. Thaidog Linux - General 2 04-21-2007 07:01 PM
strings question sibtay Programming 7 10-01-2004 10:37 AM
Help with a script (question on strings) wswartz Linux - General 3 07-11-2004 08:53 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:37 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration