Thread: Can we store integer in a char variable?? (With reference to my code)

  1. #1
    Registered User
    Join Date
    Aug 2017
    Posts
    28

    Can we store integer in a char variable?? (With reference to my code)

    Here is my code:
    Code:
    //Programming in C
    
    
    /*
     *
     * @author Dushyant Kaushik
     * 
     */
    
    
    #include <stdio.h>
    #include <stdlib.h>
    
    
    int main(void)
    {
    
    
        system("clear");
        
        char s1[100], s2[100], i;
        
        printf("Enter string s1: ");
        scanf("%s", s1);
        
        for (i = 0; s1[i] != '\0'; ++i) //I storing integer while declared as char data type
        {
            s2[i] = s1[i];
            
        }
        
        s2[i] = '\0'; //What's the importance of this line? 
        
        printf("String s2: %s", s2);
        getchar();
        return 0;
    
    
    }

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,732
    Yes you can, but you shouldn't unless it is absolutely necessary. Besides the fact that it can contain a much smaller range of values that an int, it is also generally slower. Moreover, the standard doesn't mandate whether a plain "char" is signed or unsigned, which can run you into problems with different compilers. When you need a generic, all-purpose integer, go with int.
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    4,557
    Where in your code are you trying to store an int in a char?

    The importance of s2[i] = '\0'; is that a C-string must be properly terminated with the end of string character '\0'. And since you try to print s2 as a string it must be properly terminated.

    Jim

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,294
    What's the relevance of integer here?

    Your code works just as well if you type
    12345
    or
    hello
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,732
    I guess both of you missed it, so let me explain. OP was talking about line #21, where they declare i as a char, then use it as the loop counter to index the array.
    Devoted my life to programming...

  6. #6
    Registered User
    Join Date
    Aug 2017
    Posts
    28
    Concept cleared.

    Thanks to all of u

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can i store 300 in the char variable?
    By danishzaidi in forum C Programming
    Replies: 3
    Last Post: 11-10-2011, 04:06 AM
  2. Replies: 7
    Last Post: 09-04-2011, 09:29 PM
  3. how i can store 2^99999 in integer variable
    By 62049913377 in forum C Programming
    Replies: 7
    Last Post: 08-07-2010, 01:09 AM
  4. using char to store an integer in the range [128,127]
    By nikhil22 in forum C Programming
    Replies: 5
    Last Post: 07-26-2008, 09:44 AM
  5. Char variable can't store numbers?
    By Kespoosh in forum C++ Programming
    Replies: 11
    Last Post: 03-15-2003, 07:24 PM

Tags for this Thread