All Questions
397 questions
0
votes
2
answers
102
views
why printf can not print string of a pointer?
I write this program for print content of a string in c language.
but dont print string!
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define GROW_BY 10
int main(){
...
0
votes
1
answer
59
views
CS50 - Pset 2 - Substitution - Issue with Check50 Not Detecting Output
I'm working on the CS50x 2024 Substitution problem set, and I'm encountering an issue with Check50 and Submit50 not detecting the output from my formatted string. I can see the correct output when I ...
2
votes
1
answer
392
views
The dangers of overflow using sprintf and how to avoid them
I've been using an IDE making calls to the compiler without me having to configure much, but from the options I can see it seems my project is set to use gnu99 for the C Language Standard and gnu++11 ...
4
votes
2
answers
93
views
When printing string why I am getting warning when constant qualifier not used in c?
#include<stdio.h>
int main()
{
const char arr[10] = "hello";
printf(arr);
return 0;
}
When compiling the above code with don't gave any warning. But when I remove the
const from ...
1
vote
1
answer
77
views
Initializing a Variable for Strings Using Ternary Operators in C
So I want to set strings as my variables' value which is dependent on some other test cases.
const char* trial_1;
const char* trial_2;
const char* trial_3;
trial_1 = (...
0
votes
2
answers
76
views
Why doesnt it print the array in the second loop like it does in the first one?
The programme below prints the array hello correctly in the first loop. But in the second one all it prints is o. Of course the integer k is pointless but I used it because I thought that the reason ...
2
votes
1
answer
120
views
Why does my program print invisible chars instead of string?
I need to write a program in C that receives a number N and N×words(strings). Finds the word with the minimum number of characters, reduces every other word to the min character size and then prints ...
2
votes
3
answers
421
views
What is the maximum string length for %g float representation?
How big does a stack allocated string need to be in order to store any float/double in %g format?
int main()
{
float f;
double d;
char f_str[ ?? ];
char d_str[ ?? ];
...
1
vote
1
answer
36
views
C converts file from x columns to x/2 cols with second half of file on next line using fprintf string %.*s
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char** argv) {
/* return 0; */
FILE *in, *out;
char text_buffer[80];
char text_buffer2[80];
puts (&...
1
vote
1
answer
95
views
In C printf print on stdout "\0" but shouldn't
I use
./file.exe test.in | od -c
for print on the stdout even the hidden char printed by a printf("someString"), like "\t" or "\n", the problem is that even "\0&...
0
votes
2
answers
320
views
Does snprintf clobber the buffer after null-terminator?
Consider the following code:
char buffer[5]{};
snprintf(buffer, sizeof(buffer), "hi");
snprintf writes 'h', 'i' and '\0' to buffer.
My question is: is there any guarantee that the snprintf ...
0
votes
0
answers
67
views
Alternative to sprintf() for bytes buffer in C
Hello guys I have an issue when i try to put my bytes buffer into a sql buffer for sqlite3.c.
For example, I have this buffer:
static unsigned char bytes_buffer[] = {
0x30, 0x82, 0x01, 0x4b, ...
0
votes
1
answer
49
views
Exception thrown in strings input output
I had been having an issue with string in school project and couldn't manage to fix it, made a test program to troubleshoot but haven't had any luck. I'm trying to just make a simple program to take a ...
0
votes
4
answers
84
views
Why doesn't this code seg fault? Does gcc turn it into a string literal?
#include <stdio.h>
void print(char* c) {
printf("%s\n", c); //Uses %s to print a string
}
int main() {
char a = 'd';
print(&a);
return 0;
}
How does printf know ...
2
votes
1
answer
332
views
Why the command \r in printf() doesn't work?
I'm trying to update a text on the terminal without have to print again the text. Right now I'm trying to do it on a simple code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h&...
0
votes
1
answer
194
views
Printf function prints another string too
Hi i am fairly new in C language and i was trying to understand the strings. As i know, strings are just an array of characters and there shouldn't be a difference between char a[]= "car" ...
0
votes
1
answer
39
views
count_alpha in c languahe, a programme that counts occurances of letters in a string and prints the letter next to its count number
can someone advise how can I print the repeating letter with its final count/occurance once? I have the following code and after it the output but I want the output to be: 2a, 1b, 3c instead of 1a, 2a,...
-7
votes
1
answer
778
views
Difference between control string in printf function and scanf function [closed]
Difference between the control string in printf function and scan function?
I want to know the answer
0
votes
0
answers
165
views
Sprintf function converting int to a single char instead of a string
I'm trying to convert the unsigned long integer converted_binary, which contains 10000000000 to a string, but sprintf converts it to a single character 1 instead.
I am able to know this through the ...
2
votes
1
answer
231
views
Why does converting string to hex need to be done with 0xff in C?
Following on this question How to convert a string to hex and vice versa in c??
I run the following code:
#include <stdio.h>
int main (int argc, char *argv[])
{
char str[] = { 0x58, 0x01, ...
1
vote
3
answers
699
views
Why do I need "&" in printf when I want to print a pointer
So I wrote this code where I scan 2 strings. One is declared as an array and one as a pointer.
Now to my question: Why do I need for printing text2 in the printf-statment the "&" before ...
0
votes
2
answers
61
views
How to format outputs from printf in c
Im new to C and trying to format a string that correlates to the spacing of another.
I am aiming for the example output below:
Orders for Pizzeria Freddy's
# Customer Pizza ...
1
vote
1
answer
585
views
Printing a string in C without declaring a variable
I've couldn't find an already existing source for this so I've come to ask here.
If you know a good source that I can refer to please let me know it by comments.
Looking at many textbooks and ...
1
vote
1
answer
90
views
C string padding to separate two strings to fixed width
I'm trying to print a list in C that lines up the index of the list and a colon to the ouput as follows:
...
8 : SOME STUFF
9 : MORE STUFF
10 : MORE STUFF BUT LESS PADDING
...
100: MORE STUFF BUT NO ...
1
vote
1
answer
1k
views
Printf string with 20 characters regardless of string length C
I want to print a string with exactly 20 characters regardless of whether the length of the str is shorter or longer. Right now I am using:
printf("%20s:",r->str);
This doesn't shorten ...
-1
votes
2
answers
72
views
Restrict string size and display the rest of the string in next line
I'm working on a string formatting problem where I'm trying to display the string to the specified size. I need to split the string after 4 commas and display the string in the next line.
INPUT:
char *...
0
votes
1
answer
106
views
strcat() failing when concatenating various strings
I'm having trouble manipulating strings on C.
I wrote this code to concatenate various values and run a python scrip from my C program.
void python_print(float circle_1[3],float circle_2[3],float ...
0
votes
1
answer
28
views
Trying to print a sring,but getting "@@" in place of data
I am trying to get a string from the user, then remove all characters in a string, except the alphabets.
The string a string containing whitespace.
I have input a string, but the output is only "@...
3
votes
3
answers
121
views
Strings and Arrays in C language
#include <stdio.h>
int main()
{
printf("%d", "123456"[1]);
return 0;
}
The expected output is 123456, but it actually outputs 50. Why is that?
0
votes
1
answer
59
views
Why printed string is shorter when locale-charater is used
I wrote the following code. I'm trying to print a string of specified length with a non-ASCII character.
int main(int argc, char **argv)
{
setlocale(LC_ALL,"pl_PL");
printf("%...
1
vote
2
answers
844
views
Output not showing in C
I'm writing a code that must identify the letter 't' or 'T' in a word, before or after the middle of it.
If the first half of the word does contain a 't' or a 'T', the program should output a 1. If ...
0
votes
1
answer
65
views
Why is my program outputting garbage characters? (Language: C)
I have the following problem:
for an assignment I am supposed to read a txt file line by line, make every line an element in a linked list and then print them out in the correct order.
If I understand ...
1
vote
2
answers
115
views
Printing a char variable in C doesnt show properly
So my code is:
#include <stdio.h>
int main() {
char ch[5] = "funny";
printf("gum: ");
printf("ze numbre is %c \n", ch);
}
as far as I learned, it ...
2
votes
1
answer
1k
views
Unexpected line break in printf in c
I'm learning string in c, and i'm working on my homework which ask me to write a program to replace part of string under certain circumstances. Here is my source code(undone):
#include <stdio.h>
...
-3
votes
2
answers
95
views
Why are my strings getting concatenated as I initialize them? [duplicate]
I'm trying to practice declaring Strings but I'm not getting correct output.
#include <stdio.h> //Im using vscode and gcc
int main()
{
char k[]= "prac c";
...
-1
votes
1
answer
3k
views
How do I print a string between two pointers?
I'm building a rocket for Elon Musk and the memory usage is very important to me.
I have text and a pointer to it pText. It's chilling in the heap.
Sometimes I need to analyse the string, its words. I ...
0
votes
3
answers
624
views
function returns address of local variable [-Wreturn-local-addr] sprintf
i ma new c and i am trying sprintf along with pointers. all i get in console is return buf; as is please help me with this code.
#include <stdio.h>
char* stringa(char* str);
int main()
{
...
1
vote
3
answers
181
views
how can I fix the way printf manage strings with UTF8 and %10s, in c?
I am trying to print a string with non ASCII chars using c and printf, this is the program:
include <stdio.h>
void main(void){
printf("<0123456789> BOTH %s\n","<%5s>&...
0
votes
1
answer
114
views
Possible to combine two strings, each with length of PATH_MAX?
I want to build a string containing a filename including the full path. My approach on that is as follows:
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
int main( int ...
1
vote
1
answer
187
views
Output not printing to standard output after reading from a file, in C using printf ("%s", <a char* variable>);
In a C program in Windows, I am reading from a file after creating the file and nothing is printing to the screen, here is my code:
#include <stdio.h>
#include <stdlib.h>
#include <...
-3
votes
1
answer
43
views
I am getting a segementation Error in pointer when i try to print a string using char * it,why? [closed]
I am trying to store strings using
char *p,*s;:
scanf("%s", p);
scanf("%s", s);
printf("%s\n", p);
It works so far but when I call printf on s it's giving me a ...
0
votes
1
answer
130
views
Linked List strings not printing in C
I am creating a telephone directory app in C and have encountered problems printing the strings in the linked list (firstname and lastname), as seen in the display function. The 'number' integer is ...
0
votes
2
answers
566
views
Adding an integer to C-String while printing
int main(){
printf("hello world"+2);
}
test.c:32:25: warning: adding 'int' to a string does not append to the string
[-Wstring-plus-int]
printf("hello world"+2);
...
0
votes
0
answers
69
views
Proper indentation for using scanf("%[^\n]%*c", s) statement to get string
I use scanf("%[^\n]%*c", s) statement to get string, but what is the proper indentation to print this string?
I am a beginner and overall confused with string input-output instructions. Some ...
1
vote
3
answers
974
views
Why does printf print one extra byte of a character array, but only if the last element isn't the null character \0?
I'm learning C, coming from Python, and I'm trying to understand why printf() behaves this way.
My understanding is that a string in C is an array of characters followed by a null character.
If you ...
0
votes
1
answer
31
views
I want to print all the input of this string in for loop but it only prints the last input
Help me correcting this code.... I don't know what extra details should I give so it lets me post.
#include<stdio.h>
int main(){
char s[34];
int a = 4;
for (int i = 0; i < a; i++...
0
votes
3
answers
920
views
How to print text as italicized on a text file?
I'm making a program that asks the user to input citation info (i.e. author names, article title, journal title, volume number) as strings. It will then process that info to convert it into the proper ...
0
votes
3
answers
60
views
Why de-referencing is not used in case of printing of a string?
How we use a string name in C to print a string without dereferencing it?
Like here:
char str[];
printf("%s",str);
But in case of arrays we use dereferencing by square brackets[] to print ...
0
votes
0
answers
21
views
why string nam is not inside in this dyn_sprintf command? [duplicate]
I was going through a pre written program where I find sprintf command.
if(IFROOT){
str1="bicoted; diameter(d):;
str2=dyn_sprint(", distance=" GFORM, diskratio);
}
here, str1 and str2 ...
1
vote
1
answer
78
views
Weird thing with scanning strings in c
I am aware that for scanning up to 4 chars you need to use %4s. As in code:
#include <stdio.h>
int main(void){
char str0[4] = "123"; // 1 char for '\0'
char str1[5]; // +1 char ...