Page 1 of 1
C. Printf is positional
Posted: Tue Feb 24, 2015 1:07 am
by radarblue
I found out that Printf is positional.
I wondered how the printf could "know" what to display format specifyers, ( %d, %c, %f ) in the printf ( print function ) .
// PROGRAM START. ANSI C
// Printf test. To see if one Declared Variable can be printed as several outputs
// or with several different Format Specifyers (%d, %f, %c) for one Declared Variable Var A.
// The answer is yes and no.
// Yes : int A can be displayed as a char and an int. Probably because they are both 8 and 16 bit.
// No : an int can NOT be Specified as a float and demand a separate declaration. Float is signed 32bit.
// I here declare each variable, when they are to be displayed with different Format Specifyer.
// Conclusion : the printf is Positional. The Format Specifyers inside the "..." quotation marks
// must respond to the position of the Declared Variables after the "..." quotation marks
// inside the printf (print function) to give the expected result .
// Codelite 7.0
// Compiler MinGW ( TDM64-gcc-4.9.2-3 )
// Windows 8, 64 bit
// K.H.O 2015 ( own work )
# include <stdio.h>
int main ()
{
// Declare
int A, D, E;
char B;
float C;
// Valuate
A = 10;
B = 11;
C = 12;
D = 13;
E = 14;
// Calculate
printf ("Displaying the Format Specifyers and the resulting output\n\n\n");
printf ("A 10 (%%d) = %d\t\tDecimal\nB 11 (%%c) = %c\t\tCharacter\nC 12 (%%f) = %f\tFloating number FPU\nD 13 (%%X) = %X\t\tHex\nE 14 (%%o) = %o\t\tOctal\n", A, B, C, D, E);
// printf ("A 10 (%%d) = %d\nB 11 (%%c) = %c\nC 12 (%%f) = %f\nD 13 (%%X) = %X Hex\nE 14 (%%o) = %o Octal\n", A, B, C, D, E);
// printf ("A = %d\nB = %c\nC = %f\n", A, B, C);
// printf ("\nAn error will occure if the Declarations Outside\nthe quotation markes is switched place.\n\nA = %d\nB = %c\nC = %f\n", C, A, B);
// printf ("A = %d\nB = %c\nC = %f\n", C, A, B); // gives unpredicted results.
// printf ("A = %d\nA = %c\nA = %f\n",A); // float declaration will give a 0.0000
// switching place on the Declared Variables outside the "..." quotation marks will give an error.
return 0;
}
Re: C. Printf is positional
Posted: Tue Feb 24, 2015 12:44 pm
by Jarod42
Unrelated to codelite.
printf uses
ellipsis and so type is lost. It uses format string (with %c, %i, ..) to interpret memory.
Re: C. Printf is positional
Posted: Wed Feb 25, 2015 4:26 am
by radarblue
sorry mate. Im just a happy camper
Can you be more specific Jarod42. I dont understand "Elipsis", isnt that not a longated circular shape ?
Do you mean this :
http://en.wikipedia.org/wiki/Ellipsis
Wiki : Ellipses are often used in an operating system's taskbars or web browser tabs to indicate that a user interface string is longer than what can fit in the screen. Hovering the cursor over the tab often displays a tooltip of the full title.
Im cramming too many arguments into the printf. I see !
I agree on the esteticism, the code is however not lost. I will see to it that it dosent happen again.
Re: C. Printf is positional
Posted: Wed Feb 25, 2015 5:02 am
by Gibbon1
Codelite forums maybe should have a general coding forum
Anyways, the C language allows you to define functions as having a variable number of arguments. You can google 'variable argument Lists in C va_list' for many fine tutorials.
And example.I have a stripped down printf function that I wrote about 20 years ago defined as such.
void dbg_printf(const char *format, ...);
The ... tells the compiler that dbg_printf() takes 1 to any number of arguments.
BTW: I think it's possible to turn on type checking for printf() via a compiler switch.
Re: C. Printf is positional
Posted: Wed Feb 25, 2015 2:09 pm
by Jarod42
@Gibbon1:
For gcc `-Wformat` warns with bad type with format (and `__attribute__((format(printf, string_pos, ellipsis_pos)))` for user function as `dbg_printf` where string_pos is 1)
@radarblue:
I have added the link to ellipsis for C++ :
http://en.wikipedia.org/wiki/Ellipsis_( ... nd_C.2B.2B
Re: C. Printf is positional
Posted: Fri Feb 27, 2015 1:33 am
by radarblue
Pardon me, but I am new to data programming.
Im just happy that it works . Its my first program run, on my own .
This forum subfolder is called "using Codelite" isnt it ?
Honestly I dont understand what the big deal is with the "ellipses ..." mentioned.
Do you mean I should write each variable in a specific printf ?
in that case my point being that the printf is positional is lost !
Meaning the "declarations" are repeated in a sequence behind the quotation marks, that contain a row of Format Specifyers.
Printf ("%d %f, %c", decimal, float, character);
Printf ("%decimal %floating number, %character", repeat declaration position);
The Compiler tolerates it and so does the Codelite Editor .
I see now . Jarod42
I however disagree that type is lost. and that the post is unrelated to Codelite.
The link you provide gives a good description.
Jarod42 wrote:Unrelated to codelite.
printf uses
ellipsis and so type is lost. It uses format string (with %c, %i, ..) to interpret memory.
Re: C. Printf is positional
Posted: Fri Feb 27, 2015 3:12 am
by Gibbon1
I however disagree that type is lost. and that the post is unrelated to Codelite.
Codelite is just a very fancy text editor. It's more than that, but what it does not do is compile programs itself. it relies on external tools (gcc, etc) for that. Anything having to do with the external tools is probably off topic. Getting those tools to work with codelite is on topic.
The work flow is
1) codelite .workspace file defines workspace which points to one or more codelite .project files.
2>The project files list all the files that are to be built and the compiler and compiler options needed to build the program.
When you hit F7 to build the following happens.
3)codelite creates a make file your_project_name.mk and then calls some version of make.exe, probably mingw32-make.exe
4) make then shells out and runs the gcc, llvm vc or whatever compiler that is specified in .mk file to compile each file.
5) And finally runs the linker which creates the executable.
Make, GCC Compilers etc, the C/C++ languages, etc are separate programs from codelite itself. How codelite interfaces to those tools is on topic. What those tools do to your code is off topic.
Honestly I dont understand what the big deal is with the "ellipses ..." mentioned.
Do you mean I should write each variable in a specific printf ?
The ellipses is a feature of the C language. The deal with printf is what happens is the list of variables gets passed as raw list of raw binary data on the stack. Printf uses the format string to interpret what the list of raw binary data means, including size. So when the format string doesn't match the raw data you passed, then printf spits out really weird stuff.
Anyway, I remember trying to figure out printf about 30 years ago, it drove me a bit mad at first, especially no internet only badly written books.
Re: C. Printf is positional
Posted: Tue Mar 03, 2015 3:36 am
by radarblue
Thank you very much Gibbon1
This applies very well to my study book.
X86 Assembly Language and C fundamentals by J. Canvanagh
On the pages 124 - 125 it seems advisable to put the Integere Declaration in a separate printf, so the same Format Specifyer can be applied . Char and int 8 - 16 bits is seemingly working interchangablely. For instance
printf (" %d %d %d", int1, int2, int1+int2);
In the next example Figure 4.20, page 125.
Put the floating number Declarations in another separate printf, for %f float FPU Format Specifyers. 32-64 bit, signed .
It will make a mess put several Format Specifyers and different Declarations in one and the same printf !
For instance
printf ( "%d %f %X %c", decimal1, float1, hex1, character1);
Re: C. Printf is positional
Posted: Sun Mar 29, 2015 12:18 am
by radarblue
After some thought I strongly disagree that the printf should contain only Floats or only Integers.
The printf allows both Char, Hex, float and Int in one and the same printf !
How could I be led to think otherwise ... ?
I do not share the experience Gibbon1, where the Float and Integer interfere with one another in the Compiler once positioned correctly in the Editor . However I do agree that a wierd output is producecd, if the Floats and Integeres are positioned wrong. And isnt that the defintion of wrong . when placed wrong by the programmer . How to expect the compiler to do it right ?
This book is by the old boys of ANSI C
The C Programming Language by Brian W. Kernighan . 1988
Re: C. Printf is positional
Posted: Sun Mar 29, 2015 11:06 am
by Gibbon1
You might do well to study this code till you understand what's going on.
http://www.cs.fsu.edu/~baker/devices/lx ... t/printf.c