Thread: String in integer variable

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    3

    String in integer variable

    Hello. I'm new and I hope my question is not too naïve.

    Years ago I studied Pascal, Visual Basic, and a little bit of a couple of other languages in school. Now, I'm thinking of picking up programming again for personal interest. I've learnt some Python and like it but I've concluded that I won't be comfortable until I'm decent at coding in c.

    Now, I'm reading the Kernighan-Ritchie book, and one thing has struck me especially: it seems to me that the idea of a "string" type (in Pascal, if I remember rightly, and other languages) is misleading. Apparently in c you can put text either in an array of chars or in an int, because at bottom there's no such thing as "text" as opposed to numbers. Data types are only concerned with containing a certain numbers of binary digits. Correct?

    I'm getting to the question. When I read in the K-R book there are examples where it's necessary for a variable to be able to contain the value of EOF in addition to legitimate chars, it struck me that putting text in an int or an array of chars is just a matter of convenience (e.g. dealing in characters instead of bits) and economy. Correct?

    I then figured: what if I assign a text string to an int and then printf the variable? It works, as it should. But an int is not infinite, so I used a web-app to convert a string to binary to see that it was larger than an integer. Would I get an error if I assigned it to the int variable? I didn't. I know next to nothing about promotion now, but I supposed the int had been promoted to a float or another larger type??

    Then I assigned a number like "999999999999", which turned out to be significantly shorter in binary than the text string I'd tried before. Now I got an error, SIGSEGV, which I suppose is just because an integer can't hold such a number.

    So what is it that I don't get? I hope I get most of it... Thanks!

    Oh, another thing. Not sure if it's on topic, though:

    Code:
    int blah="Hello";
    
    printf(blah);
    prints blah as text by default, that is "Hello".

    Code:
    int blah="Hello";
    printf("%d", blah);
    will print 69600. I assumed, in my ignorance, that this was ascii to binary back to decimal. Except that I've tried several web text-to-binary and binary-to-decimal converters, and I'm mistaken. I suspect I'm under some huge misconception about how these things work. What do you think?

    Also, more on topic, while I was still thinking that 69600 was what I thought it was, I tried:

    Code:
    int blah=69600;
    
    printf("%s", blah);
    Never mind the maths for a moment. Why doesn't c even try to do what I want? Because it's useless (but so is printing text as "%d", right?) and wasn't provided for or because it doesn't make any sense?

    Briangriffin

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Ok, plain and simple... C has no string type and is totally ignorant of text. To work with text you use arrays of characters, which have to be big enough to hold your text plus a single null (0) at the end. C's library functions all mimic string functions by manipulating these "C-Strings".

    Code:
    char text[100]; // space for 99 characters
    Also you didn't get an error because unlike Pascal (which I miss dearly) C has absolutely no run time error checking ... it's up to you as a programmer to write code that doesn't mess up.

    Moreover; your compiler should be screeching at you with warnings and errors for what you've tried to do... Make sure your error reporting is turned up to maximum and treat every error and warning as a problem that has to be fixed.
    Last edited by CommonTater; 11-05-2011 at 01:13 PM.

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    3
    Quote Originally Posted by CommonTater View Post
    Also you didn't get an error because unlike Pascal (which I miss dearly) C has absolutely no run time error checking ... it's up to you as a programmer to write code that doesn't mess up.
    Thanks for the clarification.

    Briangriffin

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 09-19-2011, 01:37 PM
  2. Convert Integer to String and String to Integer
    By hqt in forum C++ Programming
    Replies: 26
    Last Post: 09-15-2011, 11:39 AM
  3. Replies: 7
    Last Post: 09-04-2011, 09:29 PM
  4. Replies: 4
    Last Post: 12-04-2009, 10:22 AM
  5. Save integer variable
    By metaTron in forum C Programming
    Replies: 3
    Last Post: 01-11-2006, 11:21 PM

Tags for this Thread