Lost Password?


Go Back   CodeCall Programming Forum > Software Development > C and C++

C and C++ C and C++ forum for discussing all forms of C except for C#. These languages are powerful low level languages used for creating Operating Systems, Device Drivers, compilers and much more.

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 09-15-2007, 01:55 AM
littlevamp littlevamp is offline
Newbie
 
Join Date: Sep 2007
Posts: 5
Rep Power: 0
littlevamp is on a distinguished road
Default Explanation on strlen(buffer)

Hi, I'm quite confused by a part of this code, hope someone can explain to me.

C Code:
  1. int main() {
  2.     char buffer[128];
  3.     char *result;
  4.     while (!feof(stdin)) {
  5.         result = fgets( &buffer[0],128,stdin);
  6.         if (result!=NULL) {
  7.             buffer[strlen(buffer)-1] = '\0';
  8.             printf( "String is >%s<\n",&buffer[0]);
  9.         }
  10.     }
  11.     return 0;
  12. }

I would like to ask what does strlen(buffer) function do?
'\0' is supposed to eliminate the last character in the string right? Does it means if my string consist of 128 characters, it will remove the last character which means they will display 127 characters of string?

Can I safely conclude that the size of the largest string I can safety type into the program when it runs is 127?

Hope to see some clarification. Thanks in advance!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #2 (permalink)  
Old 09-15-2007, 09:45 AM
v0id's Avatar   
v0id v0id is offline
Retired
 
Join Date: Apr 2007
Location: Denmark
Posts: 2,654
Last Blog:
CherryPy(thon)
Rep Power: 29
v0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of light
Send a message via MSN to v0id
Default

No, '\0' do not eliminate the last character in the string. It's the terminating NULL byte. It's the end-character for a string.

What strlen does is counting bytes in the string. It loops through the string, while counting, until it reaches a terminating NULL byte. Even if there's characters after the terminating NULL byte it will not continue.

It's easy to make a function yourself which does the job. This is just my version. IMO, simple and clean.
Code:
unsigned int my_strlen(const char *szString)
{
	unsigned int iBytes = 0;
	while(*szString++ != '\0')
		iBytes++;
	return iBytes;
}
And you can test it...
Code:
	char buffer[128];
	
	printf("sizeof:    %u\n", sizeof(buffer));
	printf("strlen:    %u\n", strlen(buffer));
	printf("my_strlen: %u\n", my_strlen(buffer));
	
	buffer[0] = 'a';
	buffer[1] = '\0';
	buffer[2] = 'b';
	
	printf("sizeof:    %u\n", sizeof(buffer));
	printf("strlen:    %u\n", strlen(buffer));
	printf("my_strlen: %u\n", my_strlen(buffer));
__________________
05-03-2007 - 11-13-2008
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


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

Contest Stats

WingedPanther ........ 2753.6
Xav ........ 2704
Brandon W ........ 1702.32
John ........ 1207.73
marwex89 ........ 1175.24
morefood2001 ........ 966.05
dcs ........ 655.75
Steve.L ........ 475.59
orjan ........ 418.58
Aereshaa ........ 383.54

Contest Rules

CodeCall Goal

Goal: 100,000 Posts
Complete: 100%


Complete - Celebrate!

Ads