Maker Pro
Maker Pro

Program to print a string

I have written two different to print string in c programming
Code:
#include <stdio.h>

 int main()
{
    char Book  = "Electronics";
 
    printf("Name of Book is  %s \n",Book);
    
    return 0;
}

warning: initialization makes integer from pointer without a cast [-Wint-conversion]
char Book = "Electronics";

Program 2
Code:
#include <stdio.h>

 int main()
{
    char Book [20] = "Electronics";
 
    printf("Name of Book is  %s \n",Book);
    
    return 0;
}
Name of Book is Electronics

Why string doesn't store without array ?
 

Harald Kapp

Moderator
Moderator
Why string doesn't store without array ?
Learn by finding out for yourself.
What is the size of a variable of type "char"?
How much memory do you need to store a string like "Electronics"?
Then tell why a char is insufficient to store a string.
 

(*steve*)

¡sǝpodᴉʇuɐ ǝɥʇ ɹɐǝɥd
Moderator
Also consider what printf knows about your variable containing a string. How can it tell when it gets to the end of the string?
 
What is the size of a variable of type "char"?
Size of char variable is 1 byte
Code:
#include <stdio.h>
int main()
{
    
    char charType;

    printf("Size of char: %ld byte\n",sizeof(charType));

    return 0;
}
Size of char: 1 byte

How much memory do you need to store a string like "Electronics"?
Then tell why a char is insufficient to store a string.
Each char type variable can store only one letter
"Electronics" is string there are 11 letters we need one byte to store each element
'E' take 1 byte
'l' take 1 byte
'e' take 1 byte
'c' take 1 byte
't' take 1 byte
'r' take 1 byte
'o' take 1 byte
'n' take 1 byte
'i' take 1 byte
'c' take 1 byte
's' take 1 byte

Array can store multiple value of same data type

so when we write this
char Book [12] = "Electronics";

That mean char variable store 11 letters or one string
Code:
#include <stdio.h>

 int main()
{
    char Book [12] = "Electronics";
 
    printf("Name of Book is  %s \n",Book);
    
    return 0;
}

Name of Book is Electronics
Also consider what printf knows about your variable containing a string.
%s tell to print the string
How can it tell when it gets to the end of the string
I am not clear about this what do you want to ask, after deceleration and initialization of array we use for loop to see the each element
 

(*steve*)

¡sǝpodᴉʇuɐ ǝɥʇ ɹɐǝɥd
Moderator
When you pass a string to printf, you pass the address of the first character of the string.

From the address of the first character, can printf know the length of your defined array?

How does it know where the string ends?

In your example above, why do you think there array has space allocated for 12 characters when the string is only 11 characters?
 
When you pass a string to printf, you pass the address of the first character of the string.

From the address of the first character, can printf know the length of your defined array?

How does it know where the string ends?
we can use loop to store limited value
Code:
#include <stdio.h>

 int main()
{
char Book [11] = {'E','l','e','c','t','r','o','n','i','c','s'};
  
   int i;
  
   for (i = 0; i < 11; i++)
      
       {
           printf("Name of Book is = %c  \n",Book[i]);
          
       } 
      
       printf("Name of Book is = %s  \n",Book);
  
    return 0;
}
Name of Book is = E
Name of Book is = l
Name of Book is = e
Name of Book is = c
Name of Book is = t
Name of Book is = r
Name of Book is = o
Name of Book is = n
Name of Book is = i
Name of Book is = c
Name of Book is = s
Name of Book is = Electronics

In your example above, why do you think there array has space allocated for 12 characters when the string is only 11 characters?
Each character will take only one byte so if there are 11 character then then will take one bite per character
 
Last edited:

Harald Kapp

Moderator
Moderator
I'm quite sure what Steve is after is described here. Please read this. There's more to a string like "Electronics" than just these 11 characters.

Coming back to your original question: Do you now know why you can't store a string in a "char" varaiable?
 
I'm quite sure what Steve is after is described here. Please read this. There's more to a string like "Electronics" than just these 11 characters.
The C compiler automatically add null character '\0' at the end of the string which indicates the end of the string

Coming back to your original question: Do you now know why you can't store a string in a "char" varaiable?
char variable store only one letter and it take 1 byte memory space. one char variable store ASCII value of one character.

string is combination of many letters like "Electronics", each character has it's own ASCII values so if we want to store string we have to store ASCII values associated with characters.
 

Harald Kapp

Moderator
Moderator
The C compiler automatically add null character '\0' at the end of the string which indicates the end of the string
Be careful with this assumption, it may not always be true. When the array size is too small, the compiler can't add /0 at the end. Also once you start manipulating strings within your program, always check whether the assumed trailing /0 is present. If you can't ensure that /0 is present, use string manipulation functions that terminate after a max. number of characters. See this discussion.

if we want to store string we have to store ASCII values associated with characters.
We do, but that is not relevant to your question. A string is an array of characters. To store an array you need an array structure, not a single "char" variable.
 
Top