473,387 Members | 1,942 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,387 software developers and data experts.

question about gcc option..

here is the code

#include <stdio.h>
int main()
{
char *str1="Borland International";
printf("%d\n", strlen(str1));
return 0;
}

i learned when i use strlen i have to include <string.h>
but the above runs o.k without <string.h>
i compiled it with gcc -Wall -ansi -pedantic -o

Could anyone explain about this ?
Nov 13 '05 #1
5 4527
In 'comp.lang.c', "herrcho" <he*********@kornet.net> wrote:
here is the code

#include <stdio.h>
Missing
#include <string.h>
int main()
{
char *str1="Borland International";
printf("%d\n", strlen(str1));
printf ("%d\n", (int) strlen(str1));

or better

printf ("%lu\n", (unsigned long) strlen(str1));
return 0;
}

i learned when i use strlen i have to include <string.h>
True enough.
but the above runs o.k without <string.h>
i compiled it with gcc -Wall -ansi -pedantic -o
What was the warning messages?
Could anyone explain about this ?


It runs by chance only. It's a UB, and UB are tricky. Don't do that.

--
-ed- em**********@noos.fr [remove YOURBRA before answering me]
The C-language FAQ: http://www.eskimo.com/~scs/C-faq/top.html
<blank line>
FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/
Nov 13 '05 #2
Emmanuel Delahaye wrote:
In 'comp.lang.c', "herrcho" <he*********@kornet.net> wrote:
here is the code

#include <stdio.h>


Missing
#include <string.h>
int main()
{
char *str1="Borland International";
printf("%d\n", strlen(str1));


printf ("%d\n", (int) strlen(str1));

or better

printf ("%lu\n", (unsigned long) strlen(str1));
return 0;
}

i learned when i use strlen i have to include <string.h>


True enough.
but the above runs o.k without <string.h>
i compiled it with gcc -Wall -ansi -pedantic -o


What was the warning messages?
Could anyone explain about this ?


It runs by chance only. It's a UB, and UB are tricky. Don't do that.


there was no warning message..

by the way.. what is UB ?? i'm new to c programming..
Nov 13 '05 #3
In 'comp.lang.c', "herrcho" <he*********@kornet.net> wrote:
> i compiled it with gcc -Wall -ansi -pedantic -o
What was the warning messages?
> Could anyone explain about this ?


It runs by chance only. It's a UB, and UB are tricky. Don't do that.


there was no warning message..


Try again with

gcc -W -Wall -ansi -pedantic -o
by the way.. what is UB ?? i'm new to c programming..


Sorry about that. Undefined Behaviour. anything can happen, including 'good'
behaviour. This is why it's so a dangerous bug.

--
-ed- em**********@noos.fr [remove YOURBRA before answering me]
The C-language FAQ: http://www.eskimo.com/~scs/C-faq/top.html
<blank line>
FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/
Nov 13 '05 #4
In 'comp.lang.c', Emmanuel Delahaye <em**********@noos.fr> wrote:
Try again with

gcc -W -Wall -ansi -pedantic -o


I meant -O (or -O2, -O3 etc.)

gcc -W -Wall -ansi -pedantic -O2

--
-ed- em**********@noos.fr [remove YOURBRA before answering me]
The C-language FAQ: http://www.eskimo.com/~scs/C-faq/top.html
<blank line>
FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/
Nov 13 '05 #5
bd
On Thu, 07 Aug 2003 22:05:33 +0900, herrcho wrote:
here is the code

#include <stdio.h>
int main()
{
char *str1="Borland International";
printf("%d\n", strlen(str1));
return 0;
}

i learned when i use strlen i have to include <string.h> but the above
runs o.k without <string.h> i compiled it with gcc -Wall -ansi -pedantic
-o

Could anyone explain about this ?


If you don't #include it, it assumes it's a function that returns int, and
has whatever you gave it as arguments. strlen()'s prototype is:

size_t strlen(const char *)

Since size_t isn't the same as int, this is Undefined Behavior, so
anything can happen. In this case it apparently worked, but you shouldn't
rely on it.

Also, "%d" is incorrect - if you have a C99 compiler, you can use "%zu",
otherwise, cast to the proper type (for "%d", int)

--
Freenet distribution not available
<Shinobi> There are worse things than Perl....ASP comes to mind

Nov 13 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Mark Watkins | last post by:
Of course, this is not an html board, but some of you may be able to answer this question. Copy and past the html below into a text file so you can open it in IE and see what I mean: ...
6
by: calfliu | last post by:
hello: everybody! I have a simple question. How can I translate the selected value from art to recipient(hidden input button)? <form name="send" method="post" action="send.cgi"> <select...
3
by: David | last post by:
Hi, I have the following Javascript that fires from an onChange event for a dropdownlist. When the dropdownlist changes, it shows a dialog box with Yes and Cancel. If I press Cancel, I want the...
6
by: Bonge Boo! | last post by:
This has got to be obvious, but I can't make it work. I have a form called with 3 pull down menus. They are linked to a database which generates the values for the <SELECT? Pull-downs. Lets...
3
by: josephweiss | last post by:
Here's my quandry my bundles table ID bottleTotal price ========================== 1 1 10.00 2 6 8.00 3 12 7.50
3
by: Bruno Alexandre | last post by:
Hi guys, I'm performing a form validation using the javascript in the ASPX pages and I got into a part that I can't figure it out how to move foward. All is resumed in this page: ...
8
by: olanglois | last post by:
Hi, I was asking myself to following question. What is better to erase an element from a STL map: calling (option #1) size_type erase(const key_type& k) or calling (option #2)
7
by: Brave | last post by:
I have two questions about pulldown menus on forms. 1: Can I have the options of one pulldown menu be dictated by the choice from another pulldown menu (example beow)? 2: Can I have a form be...
1
by: Ben | last post by:
Hi, There are two radiobuttonlists created and fed dynamically. Both has AutoPostBack=True. When an option of a radiobuttonlist is checked, its ID and its selectedvalue must appear on screen...
25
by: bonneylake | last post by:
Hey Everyone, Well i am not sure if my question needs to be here or in coldfusion. If i have my question is in the wrong section i am sorry in advance an will move it to the correct section. ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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:
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...

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.