473,654 Members | 3,109 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

strchr declaration

ts
Hello all,

Can somebody explain why strchr is declared the way it is? Here is
the declaration:

char *strchr(const char *s, int c);

Mainly I do not understand why the second parameter has the 'int'
type. From my knowledge it is not even portable. On a machine where
'char' has the same size as an 'int' and where 'char' has the same
behaviour as an 'unsigned char' the following piece of code could not
work as expected:

{
char c;
char *p;

/* some statements here initializing c */

p = strchr(my_strin g, c);
}

I am not a portability expert, so probably I am wrong. But having the
second parameter of 'char' type is less confusing and does not change
the behaviour at all.

thx
cristi
Mar 14 '07 #1
2 3090
ts <cr****@mediala b.sissa.itwrote :
Can somebody explain why strchr is declared the way it is? Here is
the declaration:

char *strchr(const char *s, int c);
This has been asked here before. Have a look at:

Message-ID: <o2LIa.12908$KF 1.274554@amstwi st00>
(http://groups.google.de/group/comp.l...658a994d3c28f/)
HTH
Flo
Mar 14 '07 #2
ts wrote:
Can somebody explain why strchr is declared the way it is? Here is
the declaration:

char *strchr(const char *s, int c);

Mainly I do not understand why the second parameter has the 'int'
type.
Historical reasons, I believe. Remember that `strchr` existed before
function prototypes did, and that a character literal such as 'X'
has type /int/ not type /char/. Suppose (as would be the case for
a /lot/ of code move from pre-Standard to post_Standard) that
`strchr` is declared without a prototype. Then the default argument
promotions apply, so `char`s get promoted to `int`s, so if the
/definition/ of `strchr` has second argument `char` the effects are
undefined. This is a Bad Thing.
From my knowledge it is not even portable. On a machine where
'char' has the same size as an 'int'
.... various problems arise in any case, if I recall correctly. An
easy fix is to make such an implementation freestanding, in which
case they don't have to provide the C standard library [1]!
I am not a portability expert, so probably I am wrong. But having the
second parameter of 'char' type is less confusing and does not change
the behaviour at all.
It would be, but for history; and it does, even if only by a little.

[1] Perhaps this is "fixed" as in "You're well and truly fixed.".

--
Chris "electric hedgehog" Dollin
"What I don't understand is this ..." Trevor Chaplin, /The Beiderbeck Affair/

Mar 14 '07 #3

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

Similar topics

83
6479
by: Alexander Zatvornitskiy | last post by:
Hello All! I'am novice in python, and I find one very bad thing (from my point of view) in language. There is no keyword or syntax to declare variable, like 'var' in Pascal, or special syntax in C. It can cause very ugly errors,like this: epsilon=0 S=0 while epsilon<10: S=S+epsilon
32
8830
by: ataru | last post by:
I want to find the position of a character in a string and replace it another if it is actually there, and I'd like the operation to be efficient. I'm assuming the "standard" way to do this is something like string s = "blah"; int i = s.find_first_of('a'); /* returns 2, I presume? */ s.replace(i,0,"e"); Right? Is this the best way (assuming I haven't made some stupid error)? Is there a way to (easily!) get a character pointer from...
8
2453
by: newmans | last post by:
Perhaps one of the experts can straighten me out on this point... In Bjarne Stroustrup's book 'The C++ Programming Language' 3rd Edition, section 4.9, indicates that typedef complex<short> Point; is not only a declaration, but also a definition. The ISO/IEC 14882:2003(E) standard states in section 3.1 clause 2 that
8
2451
by: Servé Lau | last post by:
Sometimes I code like this: char *p = strchr(buf,'\n'); if (p) *p = 0; But I've also seen the shorter expression: buf = 0;
10
20858
by: Kobu | last post by:
My question is about the use and meaning of the terms "declaration" and "definition" as it pertains to the C language. I've read sources that mix the two up when talking about such things as setting aside storage for an object, defining/declaring a struct, parts of a function, referencing an external variable in another module. sourcefile1.c ============== extern long globalfoo; /* declaration? */
4
6448
by: David Warner | last post by:
Greetings! In looking into some C coding, I am looking for the C function that will search for multiple occurances of a same character in a string. For Instance: char str = "We the people of the United States"; strchr will be successful if i am looking for the "U". It returns a
25
2793
by: venky | last post by:
Hi main() { int x; /* it declaration or defination??*/ }
4
6103
by: nospam_timur | last post by:
Let's say I have two files, myfile.h and myfile.c: myfile.h: int myfunction(int x); myfile.c: #include "myfile.h"
10
1883
by: runsun | last post by:
four strings are in a 2-D array; use strchr to find characters a, b, c, respectively; search results are to be put in another 2-D arrray ter: results of searching 'a' in the 1st string go to ter, 'b' to ter... ... 2nd string to to ter , 'b' to ter... ... here are the codes: ... int i, k; char str={"adevf", "dcdhn", "oledc", "brdca"}; char chr={'a', 'b', 'c'};
0
8372
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8285
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8814
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8706
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...
1
8475
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8591
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
4149
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...
1
2709
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
1
1915
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.