LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Why the compiler cannot check the function parameters error?

using cvi 2013 edition, OS is 64-bit windows 7. when using the call :

    sprintf ( varStr, "%s, %s, %s", text_1, text_2, num);


here, the parameter "num" is an integer, and the conversion sign should be : %d. when compilling the program, the compiler cannot recognize the error. I  think  this shoud be treated before running.

 

David

0 Kudos
Message 1 of 10
(5,136 Views)

Same reason as you are not warned by the compiler when you type:

 

printf ("Hello Woldr");

 

The contents of the format string is evaluated at runtime, not compile time.

Compiler checks for type consistency and it is satisfied in that function call.

 

If you look at the prototype of sprintf, you would see smth like:

int sprintf(char *str, const char *format, ...)

It uses a variable argument list, which also requires the inputs after the format string can only be extracted and evaluated during run time.

S. Eren BALCI
IMESTEK
0 Kudos
Message 2 of 10
(5,117 Views)

But once fogetting input the parameter "num" , the compilier should know that there is a parameter missed. This should be checked at the compiling time.

0 Kudos
Message 3 of 10
(5,112 Views)

Well, to achieve that, the compiler has to count the format specifiers in the format string.

 

This is also not possible, or better to say, not the concern of the compiler.

 

The "..." at the end of the function input list is the variable part.

You can put as many inputs as you like in there as long as they fit into the stack size reserved for the application.

 

You are right, if you think it is too much of a risk for the health of the programs.

But it is also a very powerful feature. Similar to the concept of raw pointer operations.

You can crash a computer by a few lines of "irresponsibly" written pointer code.

 

It is a gift, and a curse 🙂

S. Eren BALCI
IMESTEK
0 Kudos
Message 4 of 10
(5,106 Views)

Hello all,

 

Enabling the Format warning (-wformat) for the active configuration will yield a compiler warning on that statement:

 

warning: conversion specifies type 'char *' but the argument has type 'int' [-Wformat]

 

Best regards,

= Nelu F. || National Instruments.

0 Kudos
Message 5 of 10
(5,086 Views)

Hi Nelu,

 

That really surprised me!

So it is actually possible to check the type consistency for printf family of functions, is that right?!

 

So I hope it is a new feature?

I should have heard or read about it otherwise ...

S. Eren BALCI
IMESTEK
0 Kudos
Message 6 of 10
(5,078 Views)

Hello ebalci,

 

You are correct. Starting with LabWindows/CVI 2013, the compiler can check the  type consistency for printf family of functions. It currently returns a warning with the flag -wformat. This warning can be enabled individually from the Compiler Warnings dialog (under Warning Level All).

 

Best regards,

= Nelu F. || National Instruments.

0 Kudos
Message 7 of 10
(5,074 Views)

Hi All,

 

Is this the config: the "Format" checkbox?

 

捕获.jpg

0 Kudos
Message 8 of 10
(5,057 Views)

Hello David_Lee,

 

Yes, that is the warning. Did it work for you?

 

Best regards,

= Nelu F. || National Instruments.

0 Kudos
Message 9 of 10
(5,048 Views)

Yes, it is working. Thanks a lot!

0 Kudos
Message 10 of 10
(5,030 Views)