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

How to get warnings about implicit narrowing in c99 code

Hi all.

Apologies, since this is more a tool question, than strictly a language
question, but hey, it seemed like an appropriate place to ask...

I'm a c newbie (and have been now for about 6 years!) and I'd like to use
an automatic tool to show me warnings about the following code:

#include <stdio.h>
int main(void){
int i=99999;
short s;
s=i;
printf("s is %d\n", s);
return 0;
}

Running splint against this gives:

another.c: (in function main)
another.c:5:3: Assignment of int to short int: s = i

which is exactly what I'm after. The downside? As soon as I use c99
constructs such as declaring variables after any statement, splint barfs
horribly with a "parse error" (I believe this is simply due to splint not
being updated to understand any c99 stuff yet).

I've already asked about gcc on the gcc-help mailing list, and apparently
the "-Wconversion" flag will soon do the same (although it doesn't at the
moment).

So I was wondering, what static verification tools do experienced C
programmers recommend at the moment? (or does everyone use splint, and
keep their c to c89/c90 rather than c99?)

Thanks in advance, Jaime :-)
Jun 6 '07 #1
4 2109
Op Wed, 6 Jun 2007 13:11:52 +0000 (UTC) schreef jaime:
Hi all.

Apologies, since this is more a tool question, than strictly a language
question, but hey, it seemed like an appropriate place to ask...

I'm a c newbie (and have been now for about 6 years!) and I'd like to use
an automatic tool to show me warnings about the following code:

#include <stdio.h>
int main(void){
int i=99999;
short s;
s=i;
printf("s is %d\n", s);
return 0;
}

Running splint against this gives:

another.c: (in function main)
another.c:5:3: Assignment of int to short int: s = i

which is exactly what I'm after. The downside? As soon as I use c99
constructs such as declaring variables after any statement, splint barfs
horribly with a "parse error" (I believe this is simply due to splint not
being updated to understand any c99 stuff yet).

I've already asked about gcc on the gcc-help mailing list, and apparently
the "-Wconversion" flag will soon do the same (although it doesn't at the
moment).

So I was wondering, what static verification tools do experienced C
programmers recommend at the moment? (or does everyone use splint, and
keep their c to c89/c90 rather than c99?)
This has nothing to do with c99 or c90.
What are (in your implememtation) sizeof(int) and sizeof(short)?
If CHAR_BIT is 8 and sizeof(short) is 2, 99999 won't fit in a short.
--
Coos
Jun 6 '07 #2
jaime wrote:
So I was wondering, what static verification tools do experienced C
programmers recommend at the moment? (or does everyone use splint, and
keep their c to c89/c90 rather than c99?)
I'm not planning on moving to C99 for a long time, currently I use my C code
on mainframes, as well as on embedded systems.

I do beleave the lint on Solaris has a -Xc99 flag, but I have not used it.

--
Tor <torust [at] online [dot] no>

Jun 6 '07 #3
On Wed, 06 Jun 2007 20:22:13 +0200, Coos Haak wrote:
Op Wed, 6 Jun 2007 13:11:52 +0000 (UTC) schreef jaime:
>Hi all.

Apologies, since this is more a tool question, than strictly a language
question, but hey, it seemed like an appropriate place to ask...

I'm a c newbie (and have been now for about 6 years!) and I'd like to use
an automatic tool to show me warnings about the following code:

#include <stdio.h>
int main(void){
int i=99999;
short s;
s=i;
printf("s is %d\n", s);
return 0;
}

Running splint against this gives:

another.c: (in function main)
another.c:5:3: Assignment of int to short int: s = i

which is exactly what I'm after. The downside? As soon as I use c99
constructs such as declaring variables after any statement, splint barfs
horribly with a "parse error" (I believe this is simply due to splint not
being updated to understand any c99 stuff yet).

I've already asked about gcc on the gcc-help mailing list, and apparently
the "-Wconversion" flag will soon do the same (although it doesn't at the
moment).

So I was wondering, what static verification tools do experienced C
programmers recommend at the moment? (or does everyone use splint, and
keep their c to c89/c90 rather than c99?)
This has nothing to do with c99 or c90.
What are (in your implememtation) sizeof(int) and sizeof(short)?
If CHAR_BIT is 8 and sizeof(short) is 2, 99999 won't fit in a short.
Sorry - I can see I didn't explain myself very well.

I realize that fitting ints into shorts _isn't_ a c90/c99 issue, but splint
not being able to help me analyse my code _is_ a c90/c99 issue.

I write c, and I'd like to use tools to help me write better c. I'd like
to use splint, as it can show me easy-to-miss errors, like implicit
narrowing (an example of which I've given above), but if I write c99,
splint can't help me (if I write c90, splint _can_ help me).

As a clearer example, what tool can I use to point out the implicit
narrowing in the following piece of code?:

#include <stdio.h>
int main(void){
printf("Just a line to confuse splint");
int i=99999;
short s;
s=i;
printf("s is %d\n", s);
return 0;
}

splint returns:
another.c:4:6: Parse Error.

gcc is perfectly happy with this - no warnings, no (compile-time) errors.

But there's an implicit narrowing in there, that neither gcc nor splint
will tell me about. Do experienced c programmers check these things by
hand, or are there tools that help them?
Jun 6 '07 #4
Tor Rustad <to****@online.nowrites:
jaime wrote:
>So I was wondering, what static verification tools do experienced C
programmers recommend at the moment? (or does everyone use splint, and
keep their c to c89/c90 rather than c99?)

I'm not planning on moving to C99 for a long time, currently I use
my C code on mainframes, as well as on embedded systems.

I do beleave the lint on Solaris has a -Xc99 flag, but I have not used it.
On Solaris 9, this program:

#include <stdio.h>
int main(void){
int i=99999;
short s;
s=i;
printf("s is %d\n", s);
double x;
return 0;
}

gives me this:

% lint -Xc99 c.c

variable unused in function
(7) x in main

assignment causes implicit narrowing conversion
(5)

function returns value which is always ignored
printf

The FAQ for splint says it implements most C99 features. I'm
surprised that mixing of declarations and statements isn't one of
them.

Note that *not* mixing declarations and statements is perfectly legal
in C99. You might consider restructuring your code. You can add
nested blocks if you need to.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jun 6 '07 #5

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

Similar topics

3
by: Pete | last post by:
I'd like to have warnings for code like this: void blah(char c) {} int main() {   int a = 500;   blah(a); // want a warning here   char b = a; // warning here would be nice too }
687
by: cody | last post by:
no this is no trollposting and please don't get it wrong but iam very curious why people still use C instead of other languages especially C++. i heard people say C++ is slower than C but i can't...
6
by: Gecko | last post by:
I would like to know if there is a way to stop the runtime from implicitly casting values. For exampel, I would like the following code to crash: byte someByte = 5; int someInt = someByte; I...
9
by: Codemonkey | last post by:
Hi, Sorry for a stupid question, but is it possible to do a narrowing conversion with an object array with Option Strict On in VB? E.g: ------------------ Dim aBase as Base() = {New...
34
by: thibault.langlois | last post by:
Hello, I get a warning when I compile: #include <string.h> int main (int argc, char ** argv) { char * s; s = strdup("a string"); return 0; }
6
by: bob | last post by:
Hi, I have a shi*t load of warnings that are present in a legacy app. We need to remove them. We have a template class that is used extensively throughout the code. The class looks something...
17
by: arindam.mukerjee | last post by:
I was running code like: #include <stdio.h> int main() { printf("%f\n", 9/5); return 0; }
82
by: robert bristow-johnson | last post by:
here is a post i put out (using Google Groups) that got dropped by google: i am using gcc as so: $ gcc -v Using built-in specs. Target: i386-redhat-linux Configured with: ../configure...
3
by: gil | last post by:
Hi, I'm trying to find the best way to work with compiler warnings. I'd like to remove *all* warnings from the code, and playing around with the warning level, I've noticed that compiling with...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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
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,...
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
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...
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.