Thread: strings in C

  1. #1
    Comment your source code! Lynux-Penguin's Avatar
    Join Date
    Apr 2002
    Posts
    533

    strings in C

    when you have a program like:
    Code:
    #include <stdio.h>
    int main()
    {
    	printf("This is my string\n");
    	return 0;
    }
    I was wondering exactly how it compiles with the string in the middle of the code. I had THIS idea:
    It runs through the code building the tokens etc. It takes the string, "This is my string\n" and puts it in the TXT memory and then when the program calls the std printf() it replaces the string I typed with a pointer to that area in memory.

    I just want to know how it really works. If anyone could explain it or point me to a place where I could discover it, that would be appreciated. Thank you

    -LC
    Asking the right question is sometimes more important than knowing the answer.
    Please read the FAQ
    C Reference Card (A MUST!)
    Pointers and Memory
    The Essentials
    CString lib

  2. #2
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501
    doesn't printf just run through the string one char at a time and putchar it unless it is a "specific conversion", then is just checks the argument list for the replacement, That is how i think it works.

  3. #3
    Comment your source code! Lynux-Penguin's Avatar
    Join Date
    Apr 2002
    Posts
    533
    yeah but if you disassemble it it doesnt say

    Code:
        call printf("lala");
    I think it says...
    Code:
        .text:
            .string  "This is my string\n"
        main:
           ...
            movl  .string, %eax ; I think .string is the ptr to the addr of the string in mem
            call printf
           ...
    So doesn't that mean that printf really just calls with the parameter of the pointer to my string rather than the string itself?

    -LC
    Asking the right question is sometimes more important than knowing the answer.
    Please read the FAQ
    C Reference Card (A MUST!)
    Pointers and Memory
    The Essentials
    CString lib

  4. #4
    Registered User
    Join Date
    Jul 2003
    Posts
    110
    Yes. A string literal is a non-modifiable (but non-const) array of char, so it is converted to a pointer to it's first element in the call. This is true for arrays in general.

    HTH,
    Will

  5. #5
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Within the executable are directories into the various data sections, including the ones marked with flags such as whether the section is readable, writable, executable and so forth. When the linker recalculates addresses it never touches the initialized, static variables anyway. And it's movl %eax, .string, not the other way around. Oh well.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  6. #6
    Registered User
    Join Date
    Jan 2003
    Posts
    88
    also you have linux, so you can look through the glibc source code...or download it from gnu.org

    of course this wont show teach you about the executable structure etc... but you can see how the function works.
    Last edited by lithium; 08-01-2003 at 11:54 PM.

  7. #7
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164

    Re: strings in C

    Originally posted by Lynux-Penguin
    when you have a program like:
    Code:
    #include <stdio.h>
    int main()
    {
    	printf("This is my string\n");
    	return 0;
    }
    I was wondering exactly how it compiles with the string in the middle of the code. I had THIS idea:
    It runs through the code building the tokens etc. It takes the string, "This is my string\n" and puts it in the TXT memory and then when the program calls the std printf() it replaces the string I typed with a pointer to that area in memory.

    I just want to know how it really works. If anyone could explain it or point me to a place where I could discover it, that would be appreciated. Thank you

    -LC
    You are essentially correct
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strings Program
    By limergal in forum C++ Programming
    Replies: 4
    Last Post: 12-02-2006, 03:24 PM
  2. Programming using strings
    By jlu0418 in forum C++ Programming
    Replies: 5
    Last Post: 11-26-2006, 08:07 PM
  3. Reading strings input by the user...
    By Cmuppet in forum C Programming
    Replies: 13
    Last Post: 07-21-2004, 06:37 AM
  4. damn strings
    By jmzl666 in forum C Programming
    Replies: 10
    Last Post: 06-24-2002, 02:09 AM
  5. menus and strings
    By garycastillo in forum C Programming
    Replies: 3
    Last Post: 04-29-2002, 11:23 AM