473,396 Members | 1,714 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Please explain the behaviour of strlen() in the program

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),sizeof a);
}


Output


strlen=16
sizeof=3
16 3

Thanks & Regards
Sathish Kumar
Jun 11 '08 #1
2 2162
Banfa
9,065 Expert Mod 8TB
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
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
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
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...
11
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,...
10
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...
5
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;...
5
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> ...
23
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
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. ...
11
by: Pranav | last post by:
The Code is compiling without Error/Bug/Exception.., What are the possibilities for this behaviour? //*************************************************************** #include<stdio.h> typedef...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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,...

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.