-3

In c programming when we print a string. We are not using * . But when print a number using printf we are using *. So how it is understanding, i am printing a string or int. Is understanding using %s operator?

Attaching an example code

#include<stdio.h>

int main(int argc,char* argv){

char data[]="This is an example of pointer";
char *pointerstringdata =data;
printf("print the string data is >> %s\n",pointerstringdata);   /* Here we are not using * why?  case -1*/


int numberdata =100;
int *pointerintdata=&numberdata;
printf("print the int data is >> %d\n",*pointerintdata);   /* Here we are  using * why?  case -2*/
return 0;
}
alk
  • 69,737
  • 10
  • 105
  • 255
Vipin
  • 938
  • 5
  • 18
  • 36
  • Why it is voted down ? – Vipin Jan 12 '19 at 16:18
  • You can either `printf("%d", numberdata)` or you can `printf("%d", *pointerintdata)`. The first is passing the data directly from the variable, the second is passing the same data but accessed through the pointer. However, the `%s` format specifier is expecting a pointer, not a value, so you pass `pointerstringdata` which is a pointer. – Weather Vane Jan 12 '19 at 16:19
  • @weather vane Thanks for replying. My question was between two data type. For number i understand the difference between numberdata *pointerintdata. For printing number we using *pointerintdata. Why not for string? This is why question. – Vipin Jan 12 '19 at 16:25
  • 2
    You need to look up format specifiers for printf. %s expects a pointer, %d expects a (integer) VALUE. – GermanNerd Jan 12 '19 at 16:26
  • @Vipin there is no data type "string" in C. You might want read about how in C `char` arrays are used to mimik "strings" in particular and how arrays relate to/differ from pointers in general. – alk Jan 12 '19 at 16:40

6 Answers6

3

when we print a string. We are not using * . But when print a number using printf we are using *

Because the d conversion specifier expects an int, whereas the s conversion specifier expects a pointer (to a char and with this to the 1st element of a 0-terminated char array, which in fact is what C uses to mimik what commonly is called a "string").

alk
  • 69,737
  • 10
  • 105
  • 255
2

The C language has no provision for treating a string as a value. You cannot pass a string to function. The pointer pointerstringdata is just a pointer to a char, so *pointerstringdata is one char, not a string. Passing *pointerstringdata will pass only one character, not a string.

To print strings, when %s is used, printf expects the argument to be a pointer. It uses this pointer to read from memory, and it reads and prints characters until it finds null characters.

In contrast, C does support treating numbers as values, so they can be passed to functions directly.

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312
  • Good answer, you kinda wrote a short version of my answer at the same time I wrote mine, You boiled it down to the important points, I tend to explain a lot. +1 – Sam Jan 12 '19 at 17:21
1

in

char data[]="This is an example of pointer";
char *pointerstringdata =data;
printf("print the string data is >> %s\n",pointerstringdata);   /* Here we are not using * why?  case -1*/

if you want to print all the string you have to give its address, no *

if you want to print its first character you do `printf("%c", *pointerstringdata);


in

int numberdata =100;
int *pointerintdata=&numberdata;
printf("print the int data is >> %d\n",*pointerintdata);   /* Here we are  using * why?  case -2*/

you do not want to print the address memorized in pointerintdata but the value same in that address, so you have to dereference

there is no difference with a string ... except that you want to write all the string

a pointer is a pointer, whatever it is a pointer to a char or a pointer to an int

bruno
  • 32,421
  • 7
  • 25
  • 37
1

The %s format specifier is expecting a pointer.

If you pass *pointerstringdata the function will receive the first character in the array, which the function will try to dereference, and probably cause a crash.

Weather Vane
  • 33,872
  • 7
  • 36
  • 56
1

Disclaimer: This is an explanation about how it appear to the developer, this is not how it is after compiling the code (especially because the optimizer might change it all).


C is a very low level language. You need to understand that a variable always contains a value of a few bytes.

C is also one of the languages that made it very convenient to access larger structures.

The content of the variable can be:

  • A value (e.g: as you mentioned a number)
  • A address in the RAM
  • A structure that uses more consecutive ram (and C makes it nice to use it as if it was more than that)
    • stuct (fixed length)
    • array with fixed length

There is no real concept of having a dynamic length variable as a value, therefor strings as well as arrays of dynamic length only have the address in the variable.

As stings are variable length, the convention in C is:

  • Have an address in the variable
  • Read the real data byte by byte starting at that address
  • Read data until the byte is 0 (NULL)

That is called a null-terminated string.

This way you can pass data of variable length to printf, and printf will find out the length by looking for the first byte that is 0.

Converting variables containing address to those containing value works like this:
var_with_value = *var_with_address
var_with_address = &var_with_value

"var_with_address" is called a pointer.

In conclusion: You need to pass strings as address not as value, and numbers as value not as address, and that is the difference why you have to use *

Sam
  • 117
  • 12
0

Because pointers hold reference to the object. * dereference this object. So if the pointer holds the reference to the char object when we dereference it we get this object. So dereference char pointer is just the single char not the address of the first char in the string.

0___________
  • 60,014
  • 4
  • 34
  • 74