Questions tagged [printf]

1

votes
2

answer
2.1k

Views

PHP formatting of positive and negative floating point numbers with leading zeros

Scenario: To trim leading zeros from positive and negative floating point numbers Input: 000.123 00.123 01.123 -001.123 -00.123 -040.123 Desired output: 0.123 0.123 1.123 -1.123 -0.123 -40.123 Question: Is there an inbuilt function which will make this specific formatting easier and more efficient t...
verbumSapienti
1

votes
3

answer
331

Views

concatenate the result of echo and a command output

I have the following code: names=$(ls *$1*.txt) head -q -n 1 $names | cut -d "_" -f 2 where the first line finds and stores all names matching the command line input into a variable called names, and the second grabs the first line in each file (element of the variable names) and outputs the second...
brucezepplin
0

votes
1

answer
13

Views

Bash function global variable fails

I'm having a weird situation where when I run a conditional check against a function return inside a print statement, the global variable that is set in the function does not want to work. Let me give the example: function VALIDATE() { BLAH BLAH BLAH ERROR="IT FAILED" return 0 } ######## Start relev...
Atomiklan
0

votes
0

answer
4

Views

Print Data With Image as Badge From MSAccess DB Using VB6

Image Capturing Working Fine for Visitor Management Loading, Saving & Retrieving Working Fine Using VB6 & MS Access How to Print Image with details associated with that particular record number is MYPROBLEM. Want working source code in VB6 to resolve my problem. How to attach entire code if required...
1

votes
1

answer
31

Views

scanf not saving values correctly

while using scanf within a fucntion, which I have defined, to change values within an array that I have passed by referance to that function it doesn't change the value at all, while being double type. When i change the place holder from: scanf("%1f", &x) to scanf("%d", &x) it works as expected, b...
William Massey
0

votes
0

answer
9

Views

_L_lock_791() when printf() called

bt #0 __lll_lock_wait_private () at ../nptl/sysdeps/unix/sysv/linux/x86_64/lowlevellock.S:97 #1 0x00007f8bbbc215e0 in _L_lock_791 () from /lib64/libc.so.6 #2 0x00007f8bbbc1bf60 in _IO_vfprintf_internal (s=0x7f8bbbf67040, format=0xcc3ae8 "send wrapper data: gate_sid=%d,uclient_sid=%d,cmd_type=%d\n...
王晓亮
0

votes
1

answer
19

Views

printf and string concatenation I think; beginner at this future looks bleak

The code works fine. Just looking for information on how to set up my print f and get the desired results. signs more than one print f statement(s) import java.util.Scanner; public class Coupon { public static void main(String[] args) { // TODO Auto-generated method stub Scanner input = new Scanner...
Ben DeRue
1

votes
1

answer
84

Views

How offset of structure is calculated? Explain printf statement here?

#include main() { unsigned char c; typedef struct name { long a; int b; long c; }r; r re = {3,4,5}; r *na = &re; printf("%d", *(int*) ((char*)na + (unsigned int) & ( (struct name *)0 )->b)); } OUTPUT : 4 I would be thankful if somebody explain in detail what printf statement is doing in this c pro...
Punit Vara
1

votes
2

answer
103

Views

Formatting file with awk

ok so here is the issue on this next file i have so im trying to figure out how to format a file that i created which contains all the information i need but i need to change the output looks so cat file Jackson D. Bob 0001 book pizza apple 4.00 123as Filer J. Jake 0002 happy apple hat 4.00 124ab Me...
NewEffect
1

votes
2

answer
250

Views

Print elements in list in OCaml

I want to write a tail recursive function to print elements in a string list in separate lines like this: # printlist ["a";"b";"c"];; a b c - : unit = () # printlist ["hello";"thanks"];; hello thanks - : unit = () I was able to get it to work using print_endline with no problem: let rec printlist st...
cscisgqr
1

votes
3

answer
59

Views

how to set timer for each script that runs

Dear friend and colleges it's lovely to be here in stack overflow the best cool site Under /tmp/scripts we have around 128 scripts that perform many tests As verify_dns.sh verify_ip.sh verify_HW.sh And so on we decided to run all scripts under the current folder - /tmp/scfipt with the following cod...
Judy
1

votes
2

answer
49

Views

How to retrieve a portion of a string?

I'm reading in a string of file permissions from stdin and I want to create another string that will hold all characters expect the first from the string the user inputs in stdin. int main(){ char allPermissions[10]; char permissions[9]; scanf("%s", allPermissions); for(int i = 1; i
aDabOfRanch
1

votes
4

answer
2.4k

Views

C90 printf with \n or \r\n not working in cygwin; but fflush(stdout); works fine. why?

Cygwin64 bit Command to compile: gcc hello.c -o hello -ansi -pedantic-errors Command to run ./hello hello.c #include int main() { /*setbuf(stdout, 0); I KNOW THIS WILL WORK IF ADDED, it is a solution, but I want to know why line break itself is not working*/ printf("hello world!\n"); printf("...
milesma
1

votes
4

answer
3.4k

Views

printing int array as string

I am trying to print int array with %s. But it is not working. Any ideas why? #include main() { int a[8]; a[0]='a'; a[1]='r'; a[2]='i'; a[3]='g'; a[4]='a'; a[5]='t'; a[6]='o'; a[7] = '\0'; printf("%s", a); } It prints just a. I tried with short as well, but it also does not work.
sakura
1

votes
1

answer
322

Views

What's the type of the “precision” specifier for printf?

Suppose I use the following printf() specifier string: printf("%.*s", length, str); which asks printf() to print the first length characters in str. The question is which type length has? I only see brief mentions of integer number in the documentation. So it looks like it is int. Then it looks like...
sharptooth
1

votes
1

answer
1.1k

Views

bash printf: ignore parameters or access by index?

I have a bash function that fetches values (using curl and cut) and creates a file name from them. Now I want to support a second naming scheme that needs a different set of parameters. Example: #!/bin/bash TEMPLATE="%02i. %s.txt" foo() { a="Imagine these to" b="be set dynamically" c="42" filename="...
user1252434
1

votes
2

answer
126

Views

Undesired output c program

I have wrote a program that takes two integers(m and n) as input and then writes to a file all the prime numbers between them. I wanted the output to look neat so I print each number 7 spaces wide using the field-width modifier and only print 10 per line. The output is as expected apart from the fir...
1

votes
1

answer
88

Views

Taking structure's inputs from a file and print it in C [closed]

When i try to print expectedRuntime variable , it prints the address of expectedRuntime. But i can print timeOfSubmission variable correctly . Can anyone help me please ? struct process { int timeOfSubmission; int remainingRunTime; int expectedRunTime; char processName[20]; }; int main() { FILE *my...
Bilal Aybar
1

votes
1

answer
128

Views

C: snprintf not accepting \n for GUI display. Need suggestions

I need to put the last valid character as NUL, '\0'. There is a char array char cursStr[128]; My intention is: cursStr [lastCharacter where ends] = '\0' Here is the code snippet of how, this array is getting populated: snprintf(num_range, sizeof(num_range), "%d-%d", devIndex, devIndex+127); snprintf...
kingsmasher1
1

votes
2

answer
176

Views

Varying behavior of “%s” in wprintf() in different libc implementations

I've noticed that while #include wprintf( L"Hello, %s\n", "world" ); works with standard glibc under Linux, the same thing does not work the same way with newlib. It seems newlib expects wide string for "%s" there, so the following works instead: wprintf( L"Hello, %s\n", L"world" ); Which behavior...
dragonroot
1

votes
2

answer
31.8k

Views

Use sprintf to format floats with no decimal places if integer

Originally I was using sprintf with floats always with 2 decimal places using the following code: static void MyFunc(char* buffer, const float percentage) { sprintf(buffer, "%.2f", percentage); } One of the percentage values passed was 0x419FFFFF 20 (debugger view), this printed 20.00 into buffer. I...
integra753
1

votes
3

answer
966

Views

How to fill in empty space with 0 using printf within Java

How do I fill in the space of the first column in the following code with 0's? for(int i = 0; i < 10; i++) { System.out.printf("%-3.0f %-1s%8.2f%1s %8.2f\n", (double)i, "[", 10d, "]", 10000d); } Output: 0 [ 10.00] 10000.00 1 [ 10.00] 10000.00 ... Ouput I want 000 [ 10.00] 10000.00 001...
Alex Mohr
1

votes
1

answer
147

Views

Printf is adding newlines in BASH, and I'm not sure how to get rid of them

I have the following printf statement in my BASH script to show my script's output nicely: printf "%-20s %-25s %-5s %-10s %-20s \n" $hostn $kernl $uptim $owner $team The output from this is the following: Hostname1 2.6.32.12-0.7-default 307 Lastname, Firstname Team1 Hostname2 2.6...
EGr
1

votes
2

answer
1.3k

Views

System.out.printf() for float datatype

I've a very short code snippet here: public class FormatFloat { public static void main(String[] args) { float x = 12.345f; System.out.printf("%18.7f%s", x, "\n"); System.out.printf("%18.8f%s", x, "\n"); System.out.printf("%18.10f%s", x, "\n"); System.out.printf("%18.15f%s", x, "\n"); } } I'd su...
Mohammad Faisal
1

votes
2

answer
2k

Views

Java format modifiers doubles in string

I received an assignment at university in Java where I have to use printf to format output to the console. It was all nice and dandy but for some reason I am getting the output 10500.000000000002, the right output should be 10500.00. I tried to use the %0.2f, but because I formatted in a String I ca...
1

votes
1

answer
1.3k

Views

Printf is not a part of c language. What actually happens when following PRINTF's code is executed from stdio.h?

int printf (const char *__format, ...) { register int __retval; __builtin_va_list __local_argv; __builtin_va_start( __local_argv, __format ); __retval = __mingw_vprintf( __format, __local_argv ); __builtin_va_end( __local_argv ); return __retval; } above lines are part of stdio library corresponding...
1

votes
3

answer
88

Views

The reason behind this Output

I have made an test where it have appeared something like this: char* trim(char* strr, char* str1) { char* s = strr; while(*str1 == 32) str1++; while( (*str1 != 32) && (*str1 != 0) ) *s++ = *str1++; *s = 0; return strr; } int main(void) { char str[20] = "???"; char str1[20] =" bcd \0"; printf("(...
dreamcrash
1

votes
1

answer
497

Views

snprintf - security risk

The answer of unwind to my previous question leaded me another question. I used to ask question about const char *INTERFACE = "wlan0"; struct ifreq ifr; memset(&ifr, 0, sizeof(ifr)); snprintf(ifr.ifr_name, sizeof(ifr.ifr_name), INTERFACE); unwind replied as It's worth warning for since it c...
sven
1

votes
2

answer
33

Views

Appending a string on different lines of a file?

I am learning C and I had a question. I am trying to append a string into a file. However, every time a string is appended it has to be on the next line (Sort of like println instead of print). I cannot make the function append on the next line. Instead, it just keeps appending on the same line. Ho...
Giga Tocka
1

votes
1

answer
4.4k

Views

How to get fprintf to display higher precision?

I am trying to use the following fprintf code n m fprintf('The minimum value of the modulus of the image of C_r is %d \n\nThe correspoding point is at %d \n\n',m,n) and I get the result n = 3.632213809443102 + 3.680287679864827i m = 5.170831128945713 The minimum value of the modulus of the image of...
user27182
1

votes
1

answer
431

Views

Printing int variables with float format specifier

int main() { int a=5; float b=7.5; printf("%d %f\n",a,b); printf("%d %f\n",a,a); return 0; } when i compile this in gcc compiler output is 5 7.500000 5 7.500000 But when in visual studio, output is 5 7.500000 5 0.000000 I am not able to understand gcc compiler output.
Mohan Mangal
1

votes
1

answer
4.9k

Views

C - Error print char with scanf_s in Visual Studio 2012

First, my code: #include int main(){ int a; char b; printf("Insert number: "); scanf_s("%i",&a); getchar(); printf("Insert character: "); scanf_s("%c",&b); printf("Number: %i\n",a); printf("Character: %c\n",b); getchar(); getchar(); return 0; } I use scanf_s but visual studio 2012 say error with sc...
arwayts
1

votes
2

answer
256

Views

Weird behavior in C printf

I've the following piece of code. char str[MAXS]; gets(str); N = strlen(str); for (i = N / 2 - 1; i >= 0; i--) { printf("%c", str[i]); } for (i = N - 1; i > N / 2 - 1; i--) { printf("%c", str[i]); } printf("\n"); For and input string, for example "I ENIL SIHTHSIREBBIG S", it should print simply "THI...
aajjbb
1

votes
3

answer
146

Views

Issue with snprintf “date” and file locations

I have two issues I would like help with on solving. I have a program that captures the given information in a key file. In my case, the information is for backing up different directories, to a given location, for X amount of days. The program will compress the source directories, then push them to...
user2630969
1

votes
4

answer
328

Views

printf, adding a header to an output file

I have printf("%12.8f, %12.8f, %6.3f, %04u/%02u/%02u %i:%i:4.2f\n",sbet.longitude1,sbet.latitude1,sbet.altitude ,myyear,mymonth, myday, sbet.hour, sbet.minute, sbet.second); inside an If statement. And it produces exactly what i want line by line. But now i want to add a header/title to the ver...
user2718341
1

votes
1

answer
305

Views

Left-pads the number with ` -` instead of spaces and 0 [duplicate]

This question already has an answer here: Variable sized padding in printf 4 answers When we use printf we can write this : int i = 1; printf("%06d" i); Then I got 000001, the 0 fill the space So is there a way a I can use other character like - to fill the space instead of 0?
Lidong Guo
1

votes
2

answer
116

Views

print a line from every 5 elements of a column

I am looking for a way to select a column (e. g. eighth column) of a data file and write the first five numbers of that column in a row, the next five numbers in second row, and so on. I have been testing with awk and printf without success.
user1993416
1

votes
2

answer
189

Views

Disable/Remove printfs from library

we have a vendor provided library which has printfs turned on. This is a major annoyance, and we would like to turn it off or disable it someway. Is there a way to completely disable printfs for everything in this library without access to library source code? We would like to have printfs in other...
Bogi
1

votes
3

answer
142

Views

Can I differentiate between one and multiple arguments?

Is there any new facility in C++11 or C++14 that allows us to stop having to use: TRACE0("one-string-no-args"); and TRACE("formatting string with one-or-more args", arg1, arg2, etc); Such functions have had to be differentiated by name, because overloading can't distinguish the zero-additional...
Mordachai
1

votes
1

answer
44

Views

Use of $ in formatted output functions

I was going through the man page for the printf function and I came across a statement which explains about using $ in the format string (which I actually didn't understand very well). I then tried to look for some examples for this, but I was not able to find good examples which can explain its usa...
Rahil Arora

View additional questions