Thread: How to store string

  1. #1
    Registered User
    Join Date
    Nov 2019
    Posts
    90

    How to store string

    If the user enters a string, I want to store it, like if I want to store someone's name, how can I do that?

    I tried to write a program but I am having some warnings. What might be the correct way to store string?
    Code:
    #include<stdio.h>
    int main(void)
    {
     int i, size;
     
     char string [size];
     
     printf("Enter Size of string \n");
     
     scanf("%d",&size);
     
     printf(" Size of string : %d \n", size);
     
     for (int i = 0; i < size; i++)
     {
      scanf ("%c", &string[i]);
      printf ("%c", &string[i]);
     }
      
      return 0;
    }
    warning: format '%c' expects argument of type 'int', but argument 2 has type 'char *' [-Wformat=]
    printf ("%c", &string[i]);
    ^
    warning: unused variable 'i' [-Wunused-variable]
    int i, size;

  2. #2
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    Again, defining an array of variable length using an uninitialized 'size' is WRONG.
    And the warnings are correct.

  3. #3
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,791
    I hate VLAs even when they're done "right"

  4. #4
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    Quote Originally Posted by Hodor View Post
    I hate VLAs even when they're done "right"
    Me too... I prefer to allocate the buffer myself, in the heap, and keep the stack as short as possible.

  5. #5
    Registered User
    Join Date
    Mar 2008
    Location
    India
    Posts
    147
    I made two changes to remove the errors .

    Why size is being taken from the user , what purpose it is serving..?

    Code:
    #include<stdio.h>
    
    int main(void)
    {
     int i, size;
    
     char string [size];
    
     printf("Enter Size of string \n");
    
      scanf("%d",&size);
    
     printf(" Size of string : %d \n", size);
    
     for (i = 0; i < size; i++)
     {
      scanf ("%c", &string[i]);
      printf ("%c", string[i]);
     }
    
      return 0;
    }

  6. #6
    null pointer Structure's Avatar
    Join Date
    May 2019
    Posts
    338
    Code:
    #include<stdio.h>
     
    int main(void) {
      printf("Enter Size of string \n");
      
      int size;
      scanf("%d",&size); 
      char string[size];
      
      printf(" Size of string : %d \n", size);
     
      for (int i = 0; i < size; i++) {
        printf( "%c", string[i] );
      }
      
      return 0;
    }
    "without goto we would be wtf'd"

  7. #7
    Registered User
    Join Date
    Aug 2019
    Location
    inside a singularity
    Posts
    308
    Firstly, the program is wrong because size is an uninitialised non-const variable from which you are trying to create an array of characters. Neither does the programmer nor the computer know what size the array is. Technically, the computer does but forget about that because size is a variable holding some junk value and for all you know, it might be -29348 or something. Your compiler will flag that as an issue as long as you haven't suppressed warnings. Don't create arrays of unknown length.

    Secondly, think for a minute why you may need to know the size from the user. Let's say you need to maintain a record of names. Every name doesn't have the same size. So, in practice, you allot a fixed size and tell the user that "max size you can enter is X".

    Thirdly, you should be creating variable length arrays dynamically instead statically.

    Fourthly, what you need is a good C/C++ book that explains all of this step by step.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook, The Wizardry Compiled

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array to store integers and string
    By vajra11 in forum C Programming
    Replies: 3
    Last Post: 08-09-2018, 06:59 AM
  2. Best way to store an input string
    By workisnotfun in forum C Programming
    Replies: 2
    Last Post: 09-16-2013, 10:57 PM
  3. How C programs store string literals?
    By envec83 in forum C Programming
    Replies: 2
    Last Post: 12-11-2011, 04:31 PM
  4. How to store string dinamically
    By nick048 in forum C Programming
    Replies: 4
    Last Post: 04-05-2007, 02:01 AM
  5. Q: How can you store whitespace in a string?
    By barrie in forum C Programming
    Replies: 7
    Last Post: 01-05-2007, 09:09 AM

Tags for this Thread