Skip to main content

All Questions

Tagged with
94 votes
4 answers
111k views

Is it possible to print out only a certain section of a C-string, without making a separate substring?

Say I have the following: char *string = "Hello, how are you?"; Is it possible to print out only the last 5 bytes of this string? What about the first 5 bytes only? Is there some variation ...
Tim's user avatar
  • 4,365
26 votes
3 answers
18k views

Is sprintf(buffer, "%s […]", buffer, […]) safe?

I saw use of this pattern to concatenate onto a string in some code I was working on: sprintf(buffer, "%s <input type='file' name='%s' />\r\n", buffer, id); sprintf(buffer, "%s</td>", ...
Paul Fisher's user avatar
  • 9,674
22 votes
9 answers
5k views

'\0' and printf() in C

In an introductory course of C, I have learned that while storing the strings are stored with null character \0 at the end of it. But what if I wanted to print a string, say printf("hello") although I'...
xrfxlp's user avatar
  • 421
11 votes
6 answers
2k views

Unexpected printf output [duplicate]

I just discovered very weird behavior from the C compiler. It's very simple code. I tried it in many online C compilers, but the result is always the same, which is driving me insane. #include <...
Vertinhol's user avatar
  • 289
7 votes
5 answers
911 views

What will be the output, if we print a string that contains "%s" in it?

I'd like to know what the result would be if we print a string that contains "%s" in its content? I tried printing it as "hi%s". char *ptr="hi%s"; printf(ptr); I expected the output to be as "hi". ...
chetan vamshi's user avatar
5 votes
3 answers
3k views

Format string with multiple percent signs

I know %% is used to escape actual % signs in a string, so %%%ds will end up with %10s in the following format string, but I don't know why I need %%5s in this string? After all, there are only two ...
Matt Elson's user avatar
  • 4,395
5 votes
1 answer
67 views

Portablilty of using percison when printf-ing non 0 terminated strings

As multiple questions on here also point out, you can printf a nonterminated string by formatting with a precision as maximum length to print. Something like printf("%.*s\n", length, str); ...
Poohl's user avatar
  • 1,924
4 votes
3 answers
726 views

Does each call to printf() create a new internal buffer, or do all subsequent calls use the same buffer as the first?

When I was working on a small program using printf(), I noticed that each \t character used in following printf() calls continued from the end of the string in the previous printf() call. The ...
user avatar
3 votes
10 answers
1k views

using 0 instead of '\0' in C program

why my program did not go in infinite loop. I have not used '\0' for testing string end in while loop, instead of that I have use 0. Does '\0' and 0 same in C? If yes, then if 0 is in the middle of a ...
Ravi's user avatar
  • 303
3 votes
1 answer
1k views

How to concat strings in for loop with snprintf or similar function (C)?

I have the following code, which is my attempt to make a single string containing the stored HTTP header fields: typedef struct _header { char* name; char* value; } header; const header ...
user162097's user avatar
  • 1,258
2 votes
3 answers
190 views

Why is the strlen here equal to 25?

Just so I can confirm if what I think is going on is really going on. The following code prints out 25 when I give it the (26 letter) alphabet as an input, is it because fgets always automatically ...
Guilherme Cintra's user avatar
2 votes
2 answers
8k views

Is printf unsafe to use in C

I am new to C so I started playing around with some code. I have a bug in my code as I believe using printf(pass) is not safe to use because it can overwrite the value and hence not safe for the user. ...
user avatar
2 votes
2 answers
1k views

Why is the printf specifiers, "%s", printing multiple variables at once?

I declared four string variables and initialized in four different ways. I then used the printf function four times, as the name implies, to print to the console the four string variables. I already ...
Diego Salas's user avatar
2 votes
1 answer
430 views

Playing with pointers, dazed and confused by char * strings

So i was playing with pointers to understand different use cases, I'm not so experienced at this and unable to wrap my head around some ideas. I'll mark the lines where i have a problem. Please help ...
abhik's user avatar
  • 60
2 votes
2 answers
5k views

Why is printf causing a segmentation fault? [closed]

I have this code: char* env; if (getenv("MP") == NULL) { env = "/usr"; } else { env = getenv("MP"); } printf("($MP is %s)\n", env); printf("The program seg faults without printing me :("); ...
Outback's user avatar
  • 542

15 30 50 per page
1
2 3 4 5
7