Connect with us

Program to print a string

Discussion in 'Microcontrollers, Programming and IoT' started by vead, Feb 3, 2018.

  1. vead

    vead

    473
    14
    Nov 27, 2011
    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 ?
     
  2. Harald Kapp

    Harald Kapp Moderator Moderator

    9,383
    1,914
    Nov 17, 2011
    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.
     
  3. (*steve*)

    (*steve*) ¡sǝpodᴉʇuɐ ǝɥʇ ɹɐǝɥd Moderator

    25,192
    2,694
    Jan 21, 2010
    Also consider what printf knows about your variable containing a string. How can it tell when it gets to the end of the string?
     
  4. vead

    vead

    473
    14
    Nov 27, 2011
    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

    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
    %s tell to print 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
     
  5. (*steve*)

    (*steve*) ¡sǝpodᴉʇuɐ ǝɥʇ ɹɐǝɥd Moderator

    25,192
    2,694
    Jan 21, 2010
    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?
     
  6. vead

    vead

    473
    14
    Nov 27, 2011
    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

    Each character will take only one byte so if there are 11 character then then will take one bite per character
     
    Last edited: Feb 5, 2018
  7. Harald Kapp

    Harald Kapp Moderator Moderator

    9,383
    1,914
    Nov 17, 2011
    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?
     
  8. vead

    vead

    473
    14
    Nov 27, 2011
    The C compiler automatically add null character '\0' at the end of the string which indicates the end of the string

    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.
     
  9. Harald Kapp

    Harald Kapp Moderator Moderator

    9,383
    1,914
    Nov 17, 2011
    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.

    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.
     
    vead likes this.
Ask a Question
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.
Electronics Point Logo
Continue to site
Quote of the day

-