strings in C

This is a discussion on strings in C within the C Programming forums, part of the General Programming Boards category; when you have a program like: Code: #include <stdio.h> int main() { printf("This is my string\n"); return 0; } I ...

  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,643
    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:
    if( numeric_limits< byte >::digits != bits_per_byte )
        error( "program requires bits_per_byte-bit bytes" );
    24bbs.cpp

  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,157

    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, 02:24 PM
  2. Programming using strings
    By jlu0418 in forum C++ Programming
    Replies: 5
    Last Post: 11-26-2006, 07: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21