473,698 Members | 2,635 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Please explain the behaviour of strlen() in the program

34 New Member
Hi All

Please explain why strlen returns() "16" as output here and explain the o/p for sizeof() as well

main()
{
char a[3]={'a','b','c'};
printf("strlen= %d\n", strlen(a));
printf("sizeof= %d\n", sizeof(a));
printf("%d %d", strlen(a),sizeo f a);
}


Output


strlen=16
sizeof=3
16 3

Thanks & Regards
Sathish Kumar
Jun 11 '08 #1
2 2187
Banfa
9,065 Recognized Expert Moderator Expert
Try reading your C book (or looking on the web) about how C strings, strlen and the sizeof operator works.

As a little clue you did not pass a string to strlen you passed an array of characters.
Jun 11 '08 #2
jorba101
86 New Member
As far as I know,

sizeof() operator gives the size of a certain variable or type. The fact that it is an operator means that it is applied at compile-time and not runtime.

Since you passed a 3-byte wide var, it gives 3.

On the other hand, strlen gives the length of a string. A string must be NULL terminated (i.e., it is an array of chars where its last character is NULL (which is '\0')). Since strlen cannot find a '\0' in your string, it goes reading bytes in memory (a[0], a[1], a[2], a[3]) until it finds a '\0'. In your particular case at the time you ran your program, a[16] happened to be a '\0'. So there it stopped and said 16 as the length.

You might wonder why your program is accessing a[15] if you declared it to have only a[0], a[1] and a[2]. But that's how the thing works: Welcome to C language and low-level programming.
Jun 11 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

12
3067
by: Sanjeev | last post by:
Output of followin program at Turbo C++ 3.0 is 7 ( Not 2 or 3). Please explain why ? //////////////////////////////////////////////// #include<stdio.h> #include<string.h> void main() {
1
2107
by: djinni | last post by:
It's been a while since I've written anything in C, and I was hoping to get some feedback on the following code. As far as I can tell, the program works fine, but I'm not sure I'm cleaning all the memory up properly, and I think the "tokenize_my_string" function can probably be made better. This program prompts the user for a text sentence, then re-orders the words in a random fashion... ...
11
1996
by: Sathyaish | last post by:
I had recieved an email sometime ago wherein I was asked to write a function that parsed a given string for the longest pallindrome it contained and replaced the pallindrome with another string, given as its second argument. In finding a pallindrome, however, all the characters were to be considered equal. Therefore, a space, a comma, or any punctuation for that matter was to be treated like any other character. In solving that...
10
1793
by: sindica | last post by:
I am using DevC++ 4.0 lately, which uses Mingw port of GCC, on a WinXP. I am surprised to see the malloc behaviour which is not consistent with the documentation. See the program and its output below. #include <stdio.h> #include <stdlib.h> int main(int argc, char *argv) {
5
1559
by: cpptutor2000 | last post by:
I am compiling and running the following code snippet on a Linux box - I am really puzzled by the answers. Could someone please tell me what might be wrong? void test(){ int m = 0; int n = 0; int i = 0; int j = 0;
5
1630
by: Registered User | last post by:
I'm a newbie and have attempted to create a program to convert a string containing the representation of a number in a specified base to a decimal integer. Here's the code: #include <stdio.h> unsigned todecimal( char *s, int base) { unsigned num = 0, mul; int len=0;
23
2130
by: gribouille | last post by:
Hi, via fgets() i create a array containing a text file. fp = fopen(argv, "r"); while ((c = fgetc(fp)) != EOF) { line = c; when i want to print it
4
2349
by: fatboySudsy | last post by:
Hi, I have constructed a client program that has given me some error codes that i just cannot see. I was wondering if a different set of eyes with much more experience than me could help me out. Here are the error codes and underneath i have listed the program. Thanks in advance for looking. client.c: In function `main': client.c:118: error: syntax error before '-' token client.c: At top level: client.c:132: error: conflicting types...
11
1396
by: Pranav | last post by:
The Code is compiling without Error/Bug/Exception.., What are the possibilities for this behaviour? //*************************************************************** #include<stdio.h> typedef struct abc_ *abc; static abc param; abc fun(void)
0
9031
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8873
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7740
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6528
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5862
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4372
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4623
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3052
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2007
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.