Questions tagged [printf]

0

votes
0

answer
6

Views

Insert bytes into file using shell

I would like to use a linux shell (bash, zsh, etc.) to insert a set of known bytes into a file at a certain position. Similar questions have been asked, but they modify in-place the bytes of a file. These questions don't address inserting new bytes at particular positions. For example, if my file ha...
fuzzybear3965
1

votes
2

answer
75

Views

Printing type-cast types in C

#include int main(void) { int nr = 5; char castChar = (char)nr; char realChar = '5'; printf('The value is: %d\n', castChar); } If the above code is compiled, the output will be: The value is: 5 But if the code below is compiled, the console will output the value 53 instead. Why doesn't it print the...
MoonOnAStick
1

votes
3

answer
81

Views

%d in scanf not working. number 4223092 comes up [closed]

Can anyone help me with this code? I don't see any problem, but somehow it doesn't work. When I typed my favorite number and press enter, the number 4223092 came up. int target; int after; #include int main() { printf('What is 6 x 4?: '); scanf('%d', &target); if (target == 24) { printf('Correct!\n...
1

votes
1

answer
30

Views

Segmentation fault when creating a new file in C

I am confused why this piece of code returns a segmentation fault. Basically, I try to create a new text file using fopen() and fprintf(). void my_function() { FILE *g; char s1[30]; printf('\nNew file (text): '); scanf('%s',&s1); //gets(s1); g = fopen(s1,'w'); fprintf(g,'something'); fclo...
s.dragos
1

votes
4

answer
2.7k

Views

How to print three bytes in a struct in C

I have the following C code: person->title The title is a three byte char array. All I want to do is print those three bytes. I can't use the string formatter with printf because there is no null byte. How can I print this out without having to copy it or add a null byte to it? Thanks in advance! UP...
PeterM
1

votes
3

answer
471

Views

How does the compiler know the prototype of printf( ) in this code?

In the following code, #include clrscr(); gotoxy(10, 20); ch= getch(a); we can see that the library functions have been called without defining their prototype, the three library functions: clrscr() gotoxy(int int) and getch() have their prototypes defined in the conio.h header file which appear in...
coderredoc
1

votes
3

answer
302

Views

How to keep track of all nodes in a linkedlist using only one variable?

The program will read values that are typed in by the user iteratively in a while loop. Every time the value is read, a new node will be created with this integer value. Now I'll make a LinkedList using these created nodes. And then print out every value stored in the LinkedList. I expect the order...
OptatootatpO
1

votes
0

answer
68

Views

sprintf() and -INF return (positive) INF

sprintf('%E', -INF); returns INF (infinity without negative sign) Is this a bug or desired behaviour?
Mvorisek
1

votes
0

answer
112

Views

C - printing to a file results in garbage characters but printing to stdout doesn't?

I made this thread a few hours ago: Tricky Segmentation faults with BST recursion in C Managed to figure it out after hours of trying. I've run into another snag, however. It's a weird one. Relevant code for perspective: extern char *optarg; extern int optind; int c, err = 0; char currentLine[STRING...
enharmonics
1

votes
1

answer
35

Views

fprintf exercise working but the txt is not valid after running

I'm having a problem I can't tell what to do. I have this task that asks me to take the words from a file (let's say input.txt) and write them all in another file (let's say output.txt) in the reverse order. The fun part is that when I execute my program (I have a VM of Xubuntu given to me from my U...
Giuliolio93
1

votes
0

answer
131

Views

sprintf %d invalid format depending on order

This is very similar to this question: sprintf invalid format '%d', but it is different. sprintf() gives an error formatting to '%d' for c(NA_integer_, 1), but not c(1, NA_integer). sprintf('%d', NA) #> [1] 'NA' sprintf('%d', 1) #> [1] '1' sprintf('%d', NA_integer_) #> [1] 'NA' sprintf('%d', c(1, NA...
Oliver
1

votes
0

answer
67

Views

The programs quits before printing the last lines

The program finishes before printing the last lines before returning the value 0, it doesn't print the ones I marked with /* ** */: int main() { int num,sum=0,num1; printf('Enter size of input:\n'); scanf('%d',&num); int *arr=(int*)malloc(sizeof(int)*num); printf('Enter numbers:\n'); for(int i=0;i
Yasmin12
0

votes
2

answer
465

Views

Recursive functions in C and printf

So, first of all I'm a total beginner in C, we're studying it at University on the course 'Structured Programming'. Now, the last few lectures about 'Recursive functions' have been a pain for me as a beginner. Could anyone of you be kind enough to explain me this: So I have this little script, a rec...
Baron
1

votes
0

answer
206

Views

Segmentation fault when using printf from assembly to print float number [duplicate]

This question already has an answer here: Segmentation fault on printf - NASM 64bit Linux 1 answer How to print a single-precision float with printf 1 answer assembly code: section .text global calculateRoot extern printFloat calculateRoot: push rbp ; save Base Pointer (bp...
Adam Jensen
1

votes
0

answer
53

Views

php difference between sprintf() and round() results [duplicate]

This question already has an answer here: PHP rounding error 5 answers round() breaks when PHP precision is set to 18+ 1 answer Why does PHP's sprintf not round 5s reliably? 3 answers ini_set('precision', 32); var_dump(0.855); //float(0.84999999999999997779553950749687) var_dump(round(0.855, 2, P...
chris
1

votes
0

answer
17

Views

How can I resolve it with Insert Into sprintif?

I installed a system with Xampp 5.6. Apache 5.6 and PhpMyAdmin 4.7.7. and I have this problem when insert on system. You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ')' at line 1 My code for insert is: if ((isse...
1

votes
2

answer
86

Views

How do I make pointers work?

I have 2 functions. The first function called 'chooser' which scans in 10 numbers into an array, and prints out 3 numbers from that array. The second function called 'calculator' determines 3 numbers should be printed out. However, I think I've done something wrong with the pointers? void chooser()...
Zoey Malkov
1

votes
1

answer
1.1k

Views

Print Binary Tree in C

How can i display an binary tree in c?, like this 37 / \ 16 3 / \ / \ 9 26 58 81 \ / \ 35 55 74 i can display level by level 1 23 4567 89 but i need draw this BTs in the exactly format void printLevel(NoArvore * node , int level) { if(node != NULL && level == 0) { print...
ezequiel
1

votes
2

answer
308

Views

Why vsnwprintf missing

I using vsnprintf for ansi string and it works well, returns -1 only on error and returns count of needen size if it does not fit into buffer. I need same function for wide strings, vsnwprintf just not exists. I also tried _vsnwprintf, but it acts really different, it returns -1 even if buffer too s...
0

votes
1

answer
21

Views

How to print several lines to stdout while overwriting what was printed before?

I'm a student currently developing a version of Othello for a university project. It is mostly finalized so I'm looking for things I can improve on. One of these is making the board (which is printed to stdout) just replace the previous one with each play, ensuring the terminal doesn't move. Below i...
Pedro António
1

votes
1

answer
42

Views

zsh truncates previous stdout when prompt width is same as COLUMNS

I have this simple zshrc which displays time in prommpt and resets it every 1 second below is simplified version of my zshrc repeat_string(){ # this works fine printf '-%.0s' $(seq 1 $(( $COLUMNS - 1))) # this doesn't works fine # printf '-%.0s' $(seq 1 $COLUMNS) } TMOUT=1 TRAPALRM() { PROMPT='$(re...
mohitmun
1

votes
0

answer
190

Views

C program on Mac's Eclipse says “symbol(s) not found for architecture”

I am doing my uni course by following the inscructions for Mac that were given on the uni website. This is what we are supposed to write: #include int main(void) { printf('C is cool!!!'); return 0; } This is what shows up in the console: make all Building target: SimpleProj Invoking: MacOS X C Li...
Mark Horowitz
1

votes
2

answer
51

Views

Find words in multiple files and sort in another

Need help with 'printf' and 'for' loop. I have individual files each named after a user (e.g. john.txt, david.txt) and contains various commands that each user ran. Example of commands are (SUCCESS, TERMINATED, FAIL, etc.). Files have multiple lines with various text but each line contains one of th...
PFD
1

votes
0

answer
40

Views

sprintf() produces different string when unicode character (specifically, é) is used

Why does sprintf('%-10s: 1.4f\n', 'Renyi', 0) and sprintf('%-10s: 1.4f\n', 'Rényi', 0) not give the same string (R version 3.4.2)? The latter is one character shorter, removing a space before :. The latter version is what I actually want to print; how can I print it without this formatting differen...
cgmil
1

votes
1

answer
231

Views

Printing multiple variables in single printf() statement [duplicate]

This question already has an answer here: How can you print multiple variables inside a string using printf? 2 answers What I have so far: printf('Entered string is %c', string, 'with length %d', i, 'and ID number is %s', number, 'with length %d', j); The result I'm getting is: Entered string is 4
xmdoh
1

votes
2

answer
65

Views

fprintf not writing on ^C but is writing on exit

I am writing a terminal shell language in C called tsh (tech shell). The shell has to be able to handle output redirection ('>'). I am using fprintf with fopen to accomplish this. The issue looks similar to the following: If I use the redirection as it in my shell with the exit command: tsh$ pwd > o...
JSON Brody
1

votes
2

answer
171

Views

printf in a while loop is printing twice instead of once [duplicate]

This question already has an answer here: scanf() leaves the new line char in the buffer 4 answers In this code it need to calculate the number of times the operators appear. It doesn't calculate it and while running the code it prints 'Please enter a note. to finish press Q:' twice and i don't kno...
Shir Ben Avi
-3

votes
2

answer
22

Views

Why does the result of printf number change when include tab?

The below code show wrong result for 44 and 84. So, I tried to change \t into space and the numbers were shown correctly as shown below. Why just having tab in printf cause the number to be print incorrectly? I have tried to change 4 into 7 and 8 and the same problem occurred. #include int main() {...
Yossiri
1

votes
0

answer
24

Views

Wrong Results Being Printed in C After Being Inserted in 2D Char Array

int const size = 100; float grades[size]; char students[size][size]; char temp_students[size][size]; int main() { int length = 0; int i = 0; float g = 0; printf('How many students do you want to enter? \n '); scanf('%d', &length); while((i < length)){ printf('Enter the name and grade \n'); //...
Kann
1

votes
0

answer
27

Views

How can I find the source location of console output on Windows?

I'm writing some software that involves Qt, GStreamer, and RTI Connext DDS. On Windows the code hangs at a certain point under certain circumstances. Just before this happens, the message 'Buffer creation failed' is printed on the console. However, this message does not occur in my code, so I assume...
Pepijn
1

votes
1

answer
65

Views

How can my own printf do the same round as the original one?

I am trying to make my own printf and I have a problem with the '%f' option, precisely with rounding. When I try to prints this test : print('Real : %.8f\n',1685.2622); The original one gave : 1685.26220000 and mine gave : 1685.26220704. I don't know how to fix it ... Here is my code below : #inc...
saouas
1

votes
0

answer
38

Views

Can I store the string result of that function and concat it to another string?

I'm reading an array from db and i want to format that to provide a table like text for my telegram bot. I try to use the sprintf php funtion to format the text, but when i try to put the result into a string the result is that any string was putted in the string. This is my array: // This is my com...
Daniele Tentoni
1

votes
1

answer
57

Views

How to printf a std::string_view in gdb?

Just trying to debug something, and: (gdb) Thread 1 'SciTE' hit Breakpoint 2, PropSetFile::Set ([email protected]=0x7fffffffbea0, key='LS_COLORS', val='rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:mi=00:su=37;41:sg=30;43:ca=30;41:tw=30;42:ow=34;42:st=3...
sdaau
1

votes
1

answer
40

Views

Unexpected printf() behavior in Octave for-loop

I just recently switched from MATLAB to Octave because my license ran out and I wanted to give it a shot before renewing, so I can't check the behavior in MATLAB right now. I am not sure if I am making a grave mistake here, but I am having some headache over a for-loop in Octave. The loop was suppos...
JROS
1

votes
1

answer
34

Views

How can I display a floating number with no trailing zeros with printf [duplicate]

This question already has an answer here: Avoid trailing zeroes in printf() 12 answers I'd like to format floating numbers in such a way that at most 2 decimals are added after the dot. 1.24 should be displayed as 1.24 1.246 should be displayed as 1.25 1.2 should be displayed as 1.2 1.0 should be d...
viebel
1

votes
1

answer
54

Views

c++: printf and wprintf don't work together (linux)

The code: #include int main() { printf('printf\n'); wprintf(L'wprintf\n'); } https://onlinegdb.com/BkRqZttEE http://cpp.sh/4ogx3 I read a similar question here, but I didn’t know how to solve the problem of writing these commands in one code without hassle. How to properly change orientation?
ilw
1

votes
0

answer
45

Views

Why is this bitwise operation doesn't give expected output? [duplicate]

This question already has an answer here: Erratic printing by printf [closed] 1 answer If char c = 0x80, why does printf(“%d\n”, c
Lelouch Yagami
1

votes
0

answer
38

Views

Is there any way to use scanf() with an editable default text in C?

I'm trying to get a line of data from the user, but I want to preload some default data in the line so the user doesn't need to type a whole lot, it's enough to edit the line. The point would be so that the user can fix typos in a line of data.
kardosrobertkh
1

votes
3

answer
22

Views

Add link with variable to a WooCommerce mail

How do i make a printf function to display a html link filled with a woocommerce variable? See the code below:
Rick van Baalen
1

votes
1

answer
29

Views

how do i get the keyboard input written into a txt file

I am trying to read the keyboard input and write them into a new file (a nonexistent file) line by line until a specific letter is written (-x- in this case) but it doesnt seem to be working, i keep getting errors what am i doing wrong? void newfunc(){ FILE *fp; fp = fopen('newfile.txt', 'w+'); char...

View additional questions