E. Robert Tisdale wrote: int main(int argc, char* argv[]) { quad_t m = {0, 1, 2, 3}; int r; fprintf(stdout, "m = ("); for (size_t j = 0; j < 4; ++j)
Why did you declare j as type size_t ?
fprintf(stdout, " %d", m[j]); fprintf(stdout, ")\n");
WHy did you use fprintf here?
w0(&r, m); fprintf(stdout, "r = %d\n", r); return 0;
I'm only asking because I'm one of those people who started out using
functions like gets, strcat, atol and strcpy before being adviced to
use alternatives (fgets, strncat, strtol, and strncpy). Maybe you call
tell me what type of "potential" bug you are trying to prevent by using
fprintf and size_t.
Thanks
Gaya 17 2791
G Patel wrote: E. Robert Tisdale wrote:
int main(int argc, char* argv[]) { quad_t m = {0, 1, 2, 3}; int r; fprintf(stdout, "m = ("); for (size_t j = 0; j < 4; ++j)
Why did you declare j as type size_t?
fprintf(stdout, " %d", m[j]); fprintf(stdout, ")\n");
WHy did you use fprintf here?
w0(&r, m); fprintf(stdout, "r = %d\n", r); return 0;
I'm only asking because I'm one of those people who started out using functions like gets, strcat, atol and strcpy before being advized to use alternatives (fgets, strncat, strtol, and strncpy). Maybe you call tell me what type of "potential" bug you are trying to prevent by using fprintf and size_t.
Don't look for profound reasoning here.
It's mostly a matter of style.
I use size_t for subscript j and extent n because
0<= j < n
in the declaration of array
int m[n];
I should have written something like:
FILE* myout = stdout;
fprintf(myout, "m = (");
for (size_t j = 0; j < 4; ++j)
fprintf(myout, " %d", m[j]);
fprintf(myout, ")\n");
but I was lazy and
I didn't want to distract attention from the problem at hand.
But this is convenient if, later, I decide that
I really need to redirect this output to a log file for example.
On 25 Jan 2005 19:10:03 -0800, "G Patel" <ga********@gma il.com> wrote
in comp.lang.c: E. Robert Tisdale wrote: int main(int argc, char* argv[]) { quad_t m = {0, 1, 2, 3}; int r; fprintf(stdout, "m = ("); for (size_t j = 0; j < 4; ++j) Why did you declare j as type size_t ?
Tisdale is a troll and an idiot. There is absolutely no reason at all
to prefer size_t to int in this situation. fprintf(stdout, " %d", m[j]); fprintf(stdout, ")\n");
WHy did you use fprintf here?
Tisdale is still a troll and an idiot. There is absolutely no reason
to prefer fprintf(stdout, /* whatever */) to printf(/* whatever */)
here, or under any circumstances that I can think of off-hand.
Although if there is a case where there is a difference, someone here
will correct me.
There are times when it can be considered reasonable to use
fputs(text_stri ng, stdout) over puts(text_strin g), when you do not,
for some reason, want a '\n' appended and you are otherwise not using
printf(). w0(&r, m); fprintf(stdout, "r = %d\n", r); return 0;
I'm only asking because I'm one of those people who started out using functions like gets, strcat, atol and strcpy before being adviced to use alternatives (fgets, strncat, strtol, and strncpy). Maybe you call tell me what type of "potential" bug you are trying to prevent by using fprintf and size_t.
Thanks
There is nothing wrong with asking the question, if you don't know the
relative reliability of the posters. The best piece of advice you
advice you can gather from this particular exchange is to ignore
Tisdale. Period.
Personally, I have him kill filed, and only see such of his posts as
people like you quote in replies.
--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.l earn.c-c++ http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
G Patel wrote: E. Robert Tisdale wrote: int main(int argc, char* argv[]) { quad_t m = {0, 1, 2, 3}; int r; fprintf(stdout, "m = ("); for (size_t j = 0; j < 4; ++j)
Why did you declare j as type size_t ?
fprintf(stdout, " %d", m[j]); fprintf(stdout, ")\n");
WHy did you use fprintf here?
w0(&r, m); fprintf(stdout, "r = %d\n", r); return 0;
I'm only asking because I'm one of those people who started out using functions like gets, strcat, atol and strcpy before being adviced to use alternatives (fgets, strncat, strtol, and strncpy). Maybe you call tell me what type of "potential" bug you are trying to prevent by using fprintf and size_t.
Tisdale has a lot of strange coding practices. The above is not
wrong, but is definitely not preferable standard C coding
practices.
My suggestion is that you ignore him. At his best he's just a fool,
but its not uncommon for him to be plain wrong.
Erik
--
+-----------------------------------------------------------+
Erik de Castro Lopo no****@mega-nerd.com (Yes it's valid)
+-----------------------------------------------------------+
Being really good at C++ is like being really good at using rocks to
sharpen sticks." -- Thant Tessman
Jack Klein wrote: Why did you declare j as type size_t ?
Tisdale is a troll and an idiot. There is absolutely no reason at all to prefer size_t to int in this situation.
fprintf(stdout, " %d", m[j]);
size_t is the first type that comes to my mind
when I consider which type to use for an array index.
--
pete
pete wrote: Jack Klein wrote:
Why did you declare j as type size_t ?
Tisdale is a troll and an idiot. There is absolutely no reason at all to prefer size_t to int in this situation.
fprintf(stdout, " %d", m[j]);
size_t is the first type that comes to my mind when I consider which type to use for an array index.
OK, you have rebutted Jacks last assertion. Are you also cavilling
about his other assertions? Also consider that a size_t MAY
require more storage space and processing than an int.
--
"If you want to post a followup via groups.google.c om, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
in comp.lang.c i read: I'm only asking because I'm one of those people who started out using functions like gets, strcat, atol and strcpy before being adviced to use alternatives (fgets, strncat, strtol, and strncpy).
as an aside: i hope someone explained how unsafe strncpy can be, and thus
how to use it semi-safely.
--
a signature
CBFalconer wrote: pete wrote: Jack Klein wrote:
Why did you declare j as type size_t ?
Tisdale is a troll and an idiot. There is absolutely no reason at all to prefer size_t to int in this situation.
> fprintf(stdout, " %d", m[j]); size_t is the first type that comes to my mind when I consider which type to use for an array index.
OK, you have rebutted Jacks last assertion. Are you also cavilling about his other assertions?
No, but I want to get off that bandwagon as soon as possible.
Also consider that a size_t MAY require more storage space and processing than an int.
int is not my first choice to default to
for selecting the type of an array index.
I prefer size_t and my second choice is unsigned.
I know that size_t has the range to index into any array.
I also consider that array index values
are likely to be derived from or compared to
expressions like (sizeof array / sizeof *array)
or expressions involving strlen, which have size_t types.
As far as any speed concerns go, I think that this
particular premature optimization of assuming that size_t
is significantly slower than int, is very premature.
Not worth consider without knowing the performance requirments
and the implementation.
My philosphy on saving memory by using small types
is that it mostly only makes sense for large arrays of small types.
Otherwise a situation, where the difference
between declaring a variable of type long
and declaring a variable of type int,
makes a real difference in the amount of available memory,
would be a very special situation indeed.
In such a situation, I think it would be worth investigating
whether further memory could be saved by using a char type instead.
If I had a special reason to use something instead of size_t
for an array index, then I would. For example, if I wanted
to print out the index values in C89 without using a cast.
But then, I would would still prefer unsigned over int,
to avoid any signed/unsigned mismatch problems from comparison and
assignment operations with sizeof and strlen expressions.
--
pete
those who know me have no need of my name <no************ ****@usa.net> writes: as an aside: i hope someone explained how unsafe strncpy can be, and thus how to use it semi-safely.
Here's my strncpy() boilerplate:
There is occasionally a good reason to use strncpy(). However:
* Using strncpy() into a large buffer can be very inefficient.
strncpy() always writes to every byte in the destination
buffer, which can waste a lot of time if the destination
buffer is much longer than the source string.
* If the source string is longer than the size of the
destination buffer, then strncpy() doesn't write a
terminating null. So a call to strncpy() must be followed
by explicitly writing a null terminator at the end of the
destination buffer in most cases.
--
int main(void){char p[]="ABCDEFGHIJKLM NOPQRSTUVWXYZab cdefghijklmnopq rstuvwxyz.\
\n",*q="kl BIcNBFr.NKEzjwC IxNJC";int i=sizeof p/2;char *strchr();int putchar(\
);while(*q){i+= strchr(p,*q++)-p;if(i>=(int)si zeof p)i-=sizeof p-1;putchar(p[i]\
);}return 0;}
pete wrote:
.... snip ... I know that size_t has the range to index into any array. I also consider that array index values are likely to be derived from or compared to expressions like (sizeof array / sizeof *array) or expressions involving strlen, which have size_t types.
As far as any speed concerns go, I think that this particular premature optimization of assuming that size_t is significantly slower than int, is very premature. Not worth consider without knowing the performance requirments and the implementation.
Of course the proper way is to specify the range required for the
index variable, and let the compiler use that to select the optimal
storage. Pascal does this. In C we only have the option of
selecting something that is big enough. However, surely when we
write:
for (i = 0; i < 4; i++) ....
we have a good idea that i should never have values outside of
1..3, and knowing that int can cover this range, and is the most
efficiently handled integral type, we should have very few qualms
about selecting it as the index type.
--
"If you want to post a followup via groups.google.c om, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: hpy_awad |
last post by:
formatting float variables to fprintf has error to my writing to
output file called rental and I do not the reason that a rabish is
written to the file instead of the actual input screen values ?
#include <stdio.h>
//part09_le01_file_processing_file_setup_ver_01_iti_r01_ch09.c
struct name {
int int___member1;
float float_member2;
char char__member3;
|
by: Xenos |
last post by:
Is there a standard way to determine the max. value of size_t as a
compile-time constant? Will:
#define SIZE_T_MAX ((size_t) -1)
work in all cases, or just on 2s comp. machines?
DrX
|
by: hpy_awad |
last post by:
I am writing stings ((*cust).name),((*cust).address)to a file using
fgets but rabish is being wrote to that file ? Look to my source
please and help me finding the reason why this rabish is being
written.
/* Book name :
File name : E:\programs\cpp\iti01\ch10\ex09_5p1.cpp
Program discription: Adding name,Address to customer_record SETUP
PROGRAM
|
by: Smegly |
last post by:
Hi,
I'm confused about a situation i have ..
fprintf works in one function, but not in the other . .
This is my part of my code for the two functions ..
void customer(FILE *fin, FILE *fout, int msqid, message_buf sbuf, size_t
|
by: GGarramuno |
last post by:
I have a program that expects its input in a specific format.
Mainly, it expects floating-point values to be formatted in the form:
1.32
1.
3.
3.2345
In case you missed it, all floating point values have a period, even when they
are round numbers. This is a somewhat similar behavior to the %g flag of
| |
by: Joakim Hove |
last post by:
Hello,
I have code which makses use of variables of type size_t. The code is
originally developed on a 32 bit machine, but now it is run on both a
32 bit and a 64 bit machine.
In the code have statements like this:
size_t buffer_size;
printf("Total buffer size: %ud bytes \n",buffer_size);
|
by: ray.webster |
last post by:
Should the following work *if* I have a c99 compiler please?
#include <stdio.h>
int main(void)
{
size_t t = 42;
# if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L
|
by: Yevgen Muntyan |
last post by:
Hey,
I was reading C99 Rationale, and it has the following two QUIET CHANGE
paragraphs:
6.5.3.4: "With the introduction of the long long and extended integer
types, the sizeof operator may yield a value that exceeds the range
of an unsigned long."
6.5.6: "With the introduction of the long long and extended integer
|
by: Prayag Narula |
last post by:
Hi,
I want to redefine fprintf for debugging purposes. That is I want that
all the output that is going to the stdout should be logged in a
file.
I tried something like
#define fprintf (x,y,z) my_fprintf(x,y,z)
|
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...
|
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,...
| |
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |