printf

Help gcc to not warn about not using a string literal format string

I'm creating a function in C to convert an index value into a string, which is a verbose description of the "field" represented by the index. So, I have a nice array with all the verbose descriptions indexed by, well the index. To dump it into a buffer I use code like this #define BUFFER_SIZE 40 void format_verbose(uint32_t my_index, ...

Print to console without flooding in C++

My apologies for an inaccurate title, but I'm not sure what this is called exactly. How would one print to the console a single, updating line? For example, if I wanted to print a percent completion status every cycle but not flood the console with steams of text, how would I accomplish this? (What is this called? -- for future Googlin...

C : Printing big numbers

Take the following : #include <stdio.h> main() { unsigned long long verybig = 285212672; printf("Without variable : %llu\n", 285212672); printf("With variable : %llu", verybig); } This is the output of the above program : Without variable : 18035667472744448 With variable : 285212672 As you can see from the a...

I'm new to C, can someone explain why the size of this string can change?

I have never really done much C but am starting to play around with it. I am writing little snippets like the one below to try to understand the usage and behaviour of key constructs/functions in C. The one below I wrote trying to understand the difference between char* string and char string[] and how then lengths of strings work. Furth...

Use printf to display off_t, nlink_t, size_t and others

I have been programming an FTP server in my free time. But I have a problem with sending listings of a directory. I use the Unix format, also used by ls drwxr-xr-x 28 kasper kasper 4096 2009-08-14 01:32 Music drwxr-xr-x 4 kasper kasper 4096 2009-09-06 13:52 Pictures drwxr-xr-x 14 kasper kasper 4096 2009-09-09 18:49 Source drwxr-xr-x 2...

Simple program adding "D" to output

I have a very simple program that just prints the number of newlines as an integer and I get a "D" after every number. Sample input: d [enter] e [enter] f [enter] Ctrl-D [enter] Sample output: 3D What am I doing wrong? This is verbatim from The C Programming Language 2nd edition, pg. 19: #include <stdio.h> main() { int c...

Printing chars and their ASCII-code in C

How do I print a char and its equivalent ASCII value in C? ...

printf seems to be ignoring string precision

So, I'm a bit stymied. According to man 3 printf on my system, the string format "%5s" should use the specified precision to limit the number of characters printed from the string argument given. % man 3 printf PRINTF(3) BSD Library Functions Manual PRINTF(3) NAME printf, fprintf, sprintf, snprintf,...

A simple C program without #include <stdio.h>

How to call "printf" directly without including stdio.h ? I found a interesting tutorial here: http://www.halcode.com/archives/2008/05/11/hello-world-c-and-gnu-as/ So, here's my attempt: int main(){ char ss[] = "hello"; asm ( "pushl %ebp ;" "movl %esp, %ebp ;" "subl $4, %esp ;" "movl $ss, (%esp) ;" "call _printf ;" "mov...

printf executing .exe files in cygwin

so i had an assignment requiring me to 'break' a piece of vulnerable code. snippet: int main(int argc, char **argv) { /*...*/ while(i < argc-1) { switch(argv[i][0]-48) { case 1: SmashHeap(argc,argv); break; /*...*/ case 8: PrintfVulnerability(argv[++i]); break; default: printf("%s ...

awk and printf in bash

Hi, I am trying to get the rounded number of the average load in the past 5 mins. So here goes my command: uptime | awk -F, '{print $5}'|printf "%.0f\n" It seems incorrect as it always give me 0. If I tried to use a variable as intermediate between awk and printf, then it is correct avgload=$(uptime | awk -F, '{print $5}') prin...

wrapper printf function that filters according to user preferences

My program writes to a log and to stdout. Every message, however, has a certain priority and the user specifies in Preferences which priorities go to which stream (log or stdout). unsigned short PRIO_HIGH = 0x0001; unsigned short PRIO_NORMAL = 0x0002; unsigned short PRIO_LOW = 0x0003; The preferences is handled by some flags: unsign...

Why do I get a trailing '1' after Perl's printf output?

When I implement the code below I get the correct dates: 10/05/2008 10/05/2009 When I use printf instead of sprintf, I get the following: 10/05/200910/05/20081 1 Any ideas on why printf prints the trailing 1? #!/usr/bin/perl use strict; use warnings; my ($from_date, $to_date) = to_from_dates(); print "$from_date\n"; print "$t...

printf just before a delay doesn't work in C

Hi! Does anyone know why if i put a printf just before a delay it waits until the delay is finished before it prints de message? Code1 with sleep(): int main (void) { printf ("hi world"); system("sleep 3"); } Code2 with a self implemented delay: void delay(float sec) { time_t start; time_t current; time(&sta...

how can I put a breakpoint on "something is printed to the terminal" in gdb?

I would like to know from where inside a huge application a certain message is printed. The application is so big and old that it uses all conceivable ways of printing text to the terminal; for example printf(), fprintf(stdout, ...) etc. I write to put a breakpoint on the write() system call but then I'm flooded with too many breakpoint...

Clean code to printf size_t in C++ (or: Nearest equivalent of C99's %z in C++)

I have some C++ code that prints a size_t: size_t a; printf("%lu", a); I'd like this to compile without warnings on both 32- and 64-bit architectures. If this were C99, I could use printf("%z", a);. But AFAICT %z doesn't exist in any standard C++ dialect. So instead, I have to do printf("%lu", (unsigned long) a); which is really...

Call C function from Assembly

Hello! I will be working on a big Assembly project but am now just starting to learn this new language. I am trying to make some simple examples like you might find for c++ in highschool (sum two numbers, is a number prime, etc). Now I've got to display all prime numbers up to n. The problem is that the application freezes at "call pri...

What Is "\t%.10g\n"

Hello, I'm new at Bison, but in C/C++ no and at this time of development and regular expressions i never heard something like this, only the \n that's used for a new line, but i want to know what is the explanation of \t%.10g, that in the code is like this: line: '\n' | exp '\n' { printf ("\t%.10g\n", $1); } ; Best Rega...

C format specifier question

While I was working i came across a code which was written by somebody else. i see a statement as , sprintf(o_params->o_file_name, "%s_%s_%04.4d_%s_%s.ASC", "OUTD", "RM", sequence_no, DateStamp_buf1, TimeStamp_buf1 ); In the above statement, I see %04.4d. Is this a correct format specifier? The variable sequence_no ...

How do I get $(/bin/printf -6) to return -6 and not think -6 is an option.

I have a bash shell script which has the line: g=$(/bin/printf ${i}) when ${i} contains something like -6, printf thinks its being passed an option. It does not recognize the option so produces an error. if wrap ${i} in quotes, printf still thinks its being passed an option. g=$(/bin/printf "${i}") if I escape the quotes, variab...