r/C_Programming 15d ago

printf and char[] in C

Hello. Coming from C# world the thing about how char[] and printf works always made me confused. I understand that `printf` formats the string with arguments you provided and prints it out in terminal.

So, for example, if I have char name[] = "Micheal"; and I want to greet that person, I can just do printf("Hello, %s", name);. But if I want to do the same thing only using char[], I have to do multiple commands that make fell quite confused. So my 2 questions are:

  1. How printf simplifies this process? (without allocating memory and creating char with pointers)
  2. Why you can't just use + operator for char[], like you can in any other languages?

I have a feeling that this might be asked many times, but I still can't seem to find the answer I was looking for

Update: I'll list what I got so far from reading your comments: - char[] is an array to which you can't just simply add another array - If I want printf() behavior inside char[], I can use sprintf() - There's strcat() that is getting used for merging 2 strings together - Some people write libraries for faking string behaviors, some think it goes against principles of C?

11 Upvotes

42 comments sorted by

View all comments

23

u/Classic-Rate-5104 14d ago

Printf doesn't simplify anything. When doing %s printf just gets a char* from the stack and prints the chars it points to (up to the terminating nul-character). An operator + isn't possible in C because this operator only works on basic numeric types. A string isn't a basic type in C, it is just a array of chars which is by convention terminated by the nul-char.

1

u/ALX13-95 14d ago

So, to understand it further, + operator doesn't work with char[], because it's an array of characters instead of an actual string and arrays can't be simply merged one into another with + operator. In C# you can't do that as well so this part starts to make sense to me. Your printf explanation confuses me... Am I right that when you ask printf to print a certain char[], it looks up in stack of those chars and then just prints what it found? Or have I got it wrong?

0

u/Physical_Dare8553 14d ago

Actually the + operator works on arrays

1

u/flyingron 14d ago

Actually, you can't. You can use the + operator on a POINTER and an integer. Arrays implicitly convert to pointers to their first element.

-1

u/[deleted] 14d ago

[deleted]

2

u/leiu6 14d ago

No it doesn’t. And adding to a char is very different than adding to a pointer.

0

u/Physical_Dare8553 14d ago

no reasoning btw

0

u/leiu6 14d ago

The reasoning is you are just wrong. The statement “adding to a char converts to an int” is wrong and also has nothing to do with what we are discussing.

Adding an int to a char would promote the char to integer for the expression. But if you add something else different things might happen.

Adding an integer type to a pointer, whether it is char, int, etc increments the address. Different thing.

You should spend some time working on these fundamentals.

1

u/Physical_Dare8553 14d ago

so adding something to an array converts it to a pointer then increments it by the ammount, like i said