what is difference between sizeof and strlen

R

rahul8143

hello,
what is difference between sizeof("abcd") and strlen("abcd")? why
both functions gives different output when applied to same string
"abcd".
I tried following example for that.
#include <stdio.h>
#include <string.h>
void main()
{
char *str1="abcd";
char str2[]="abcd";
printf("%d %d %d",sizeof(str1),sizeof(str2),sizeof("abcd"));
printf("%d %d %d",strlen(str1),strlen(str2),strlen("abcd"));
}
 
S

Suman

hello,
what is difference between sizeof("abcd") and strlen("abcd")? why

1. Sizeof is an unary operator. Strlen is a function defined in
string.h.

2. Strlen looks for the first null('\0') and when found returns the
number
of characters that precede the null character.
both functions gives different output when applied to same string

See above.
"abcd".
I tried following example for that.
#include <stdio.h>
#include <string.h>
void main()
should be
int main()
{
char *str1="abcd";
char str2[]="abcd";
Among other things, strlen() returns a size_t object, and you should
use
the `z' length modifier, followed by a conversion specifier d,i,o,u, x
or X.

Sizeof again yields the size of an object and using the specifier for
size_t
(and a cast, I'm not sure if required, before printing) should again be
preferred.
> printf("%d %d %d",sizeof(str1),sizeof(str2),sizeof("abcd"));
printf("%d %d %d",strlen(str1),strlen(str2),strlen("abcd"));
}

Do something like
char str[12];
strcpy(str, "hello");
printf("%zu vs %zu\n", DIM(str), strlen(str));
and you'll know.
 
M

Martin Ambuhl

hello,
what is difference between sizeof("abcd") and strlen("abcd")? why
both functions gives different output when applied to same string
"abcd".
I tried following example for that.
#include <stdio.h>
#include <string.h>
void main()
^^^^
Why do so many people post without
a) following the newsgroup
b) checking the FAQ
or even
c) learning the first thing about C from even the most basic textbook?

It is impossible to take seriously a question from someone who could
screw up something so basic.
 
A

Antonio Contreras

Martin said:
^^^^
Why do so many people post without
a) following the newsgroup
b) checking the FAQ
or even
c) learning the first thing about C from even the most basic textbook?

It is impossible to take seriously a question from someone who could
screw up something so basic.

Indeed it is possible. Most compilers won't issue a warning if you set
the return type to void. They will silently insert code to return a 0
and just go on.
 
J

junky_fellow

hello,
what is difference between sizeof("abcd") and strlen("abcd")? why
both functions gives different output when applied to same string
"abcd".
I tried following example for that.
#include <stdio.h>
#include <string.h>
void main()
{
char *str1="abcd";
char str2[]="abcd";
printf("%d %d %d",sizeof(str1),sizeof(str2),sizeof("abcd"));
printf("%d %d %d",strlen(str1),strlen(str2),strlen("abcd"));
}

sizeof is an operator and strlen is a C library function.
sizeof gives the size in bytes of its operand. size is determined
by the type of operand.
"abcd" is a string literal and its size is 5.
So, sizeof("abcd") = 5.
strlen("abcd") = 4. It does not include the terminating null character.

sizeof(str1) --> The operand str1 is a pointer to char. So, its type
is char pointer and size will be the size of char
pointer.
sizeof(str2) --> str2 is an array of characters. Standard C says
that "sizeof operator When applied to an operand
that has array type, the result is the total number
of bytes in the array. So, sizeof(str2) = 5.

sizeof("abcd") = 5.

strlen(str1) = 4.
strlen(str2) = 4.
strlen("abcd") = 4.
 
R

RAJU

Hi Rahul,

First of all, sizeof() and strlen() are differnt. The sizeof() is an
operator which gives the number of bytes required to store an object of
the type of it's operand and the strlen() is a function which takes a
constant string as an input and returns length of it's argument string.

If a constant string "abcd" is passed then strlen() will always return
4.
This will print the storage required for str1, which is a pointer to
char and pointer size is 4. This is irrespective of the length of the
string it's assigned to.
str2 and "abcd" required '\0' at the end of their string. So, they
require 4 + 1 = 5 bytes of storage and print 5. In this case,
sizeof(constant string) = strlen(constant string) + 1;

Regards,
Raju

hello,
what is difference between sizeof("abcd") and strlen("abcd")? why
both functions gives different output when applied to same string
"abcd".
I tried following example for that.
#include <stdio.h>
#include <string.h>
void main()
{
char *str1="abcd";
char str2[]="abcd";
printf("%d %d %d",sizeof(str1),sizeof(str2),sizeof("abcd"));
printf("%d %d %d",strlen(str1),strlen(str2),strlen("abcd"));
}
 
J

junky_fellow

RAJU said:
Hi Rahul,

First of all, sizeof() and strlen() are differnt. The sizeof() is an
operator which gives the number of bytes required to store an object of
the type of it's operand and the strlen() is a function which takes a
constant string as an input and returns length of it's argument string.

If a constant string "abcd" is passed then strlen() will always return
4.

This will print the storage required for str1, which is a pointer to
char and pointer size is 4. This is irrespective of the length of the
string it's assigned to.

Wrong. Pointer variable size is not always 4. Its implementation
specific.
 
S

Suman

Suman said:
hello,
what is difference between sizeof("abcd") and strlen("abcd")? why

1. Sizeof is an unary operator. Strlen is a function defined in
string.h.

2. Strlen looks for the first null('\0') and when found returns the
number
of characters that precede the null character.
both functions gives different output when applied to same string

See above.
"abcd".
I tried following example for that.
#include <stdio.h>
#include <string.h>
void main()
should be
int main()
{
char *str1="abcd";
char str2[]="abcd";
Among other things, strlen() returns a size_t object, and you should
use
the `z' length modifier, followed by a conversion specifier d,i,o,u, x
or X.

Sizeof again yields the size of an object and using the specifier for
size_t
(and a cast, I'm not sure if required, before printing) should again be
preferred.
printf("%d %d %d",sizeof(str1),sizeof(str2),sizeof("abcd"));
printf("%d %d %d",strlen(str1),strlen(str2),strlen("abcd"));
}

Do something like
#define DIM(x) sizeof x / sizeof x[0]
char str[12];
strcpy(str, "hello");
printf("%zu vs %zu\n", DIM(str), strlen(str));
and you'll know.
 
R

RAJU

You are right. Pointer variable is not always 4. But, on most of the
current 32-bit systems it's 4 bytes.It can be 8 bytes also. Every thing
is depends on Memory Management.

I just wanted to make a point that it's of fixed size for an
environment and it doesn't depend on the input string,

Thanks,
Raju
 
L

Lawrence Kirby

On Fri, 05 Aug 2005 02:25:57 -0700, Antonio Contreras wrote:

....
....

Indeed it is possible. Most compilers won't issue a warning if you set
the return type to void. They will silently insert code to return a 0
and just go on.

Or more likely they won't, the'll just generate code for a function that
returns void. Depending on how the implementation works that may or may
not work where a function that returns int is required. Sometimes it works
and you'll get a garbage value back, but it may not work at all, there's
no guarantee that a main returning void will even be called successfully.

Lawrence
 
K

Kenny McCormack

hello,
what is difference between sizeof("abcd") and strlen("abcd")? why
both functions gives different output when applied to same string
"abcd".
I tried following example for that.
#include <stdio.h>
#include <string.h>
void main()
{
char *str1="abcd";
char str2[]="abcd";
printf("%d %d %d",sizeof(str1),sizeof(str2),sizeof("abcd"));
printf("%d %d %d",strlen(str1),strlen(str2),strlen("abcd"));
}

You'd think the trolls would at least be a little creative.

Personally, I prefer to declare it as "double *main()".
Works just as well.
 
R

rahul8143

Kenny said:
hello,
what is difference between sizeof("abcd") and strlen("abcd")? why
both functions gives different output when applied to same string
"abcd".
I tried following example for that.
#include <stdio.h>
#include <string.h>
void main()
{
char *str1="abcd";
char str2[]="abcd";
printf("%d %d %d",sizeof(str1),sizeof(str2),sizeof("abcd"));
printf("%d %d %d",strlen(str1),strlen(str2),strlen("abcd"));
}

You'd think the trolls would at least be a little creative.

Personally, I prefer to declare it as "double *main()".
Works just as well.

THANKS everybody i understood difference between sizeof operator and
strlen function.
 
K

Keith Thompson

RAJU said:
You are right. Pointer variable is not always 4. But, on most of the
current 32-bit systems it's 4 bytes.It can be 8 bytes also. Every thing
is depends on Memory Management.

I just wanted to make a point that it's of fixed size for an
environment and it doesn't depend on the input string,

Correct, but "pointer size is 4" was perhaps not the best way to make
that point.
 
E

Emmanuel Delahaye

(supersedes <[email protected]>)

RAJU wrote on 05/08/05 :
First of all, sizeof() and strlen() are differnt. The sizeof() is an

There is no sizeof() operator. It's sizeof. The () are around the types
(like a typecast)

int a;

size_t n = sizeof a;

n = sizeof (int);


--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

"It's specified. But anyone who writes code like that should be
transmogrified into earthworms and fed to ducks." -- Chris Dollin CLC
 
E

Emmanuel Delahaye

THANKS everybody i understood difference between sizeof operator and
strlen function.

.... but you still don't understand the declaration of main()...

The following are conforming to the C-language standard definition:

int main ();
int main (void);
int main (int, char **);

others are not standard and invokes an undefined behaviour.

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

"Mal nommer les choses c'est ajouter du malheur au
monde." -- Albert Camus.
 
E

Emmanuel Delahaye

(supersedes <[email protected]>)

THANKS everybody i understood difference between sizeof operator and
strlen function.

.... but you still don't understand the declaration of main()...

The following are conforming to the C-language standard definition:

int main ();
int main (void);
int main (int, char **);

others are not standard and invoke an undefined behaviour.


--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

"C is a sharp tool"
 
K

Kenny McCormack

(supersedes <[email protected]>)

(e-mail address removed) wrote on 05/08/05 :


... but you still don't understand the declaration of main()...

The following are conforming to the C-language standard definition:

int main ();
int main (void);
int main (int, char **);

others are not standard and invoke an undefined behaviour.

Wouldn't it be more accurate to say it invokes implementation defined
behaviour?

(Yes, I know the distinction may be moot in the context of this NG - the
equivalent of discussing the difference between a class 2 mortal sin and
a class 3 mortal sin in other religious contexts)

Anyway, I still think declaring it as: double *main(double,struct stat)
would be better for the newbies.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top