[comp.lang.c] Quick Question - Strings

mm@blake.u.washington.edu (Eric Gorr) (12/10/90)

Well, here it is for all you C experts:

In BASIC, you can write the simple, one line instruction:

    TEST = "Hello"

What I want to know is how to do this in C after the code where I declared the
varables.  Please provide some sample code...

Please Respond through E-mail.

Thanx for your help.....!!

------------------------------------------------------------------------------
Mystery_Man           ! All warfare is based on deception - Sun Tzu
                      ! Diplomacy is the art of letting someone else have
mm@                   !  your war                         - Danielle Vare'
blake.u.washington.edu! He who knows when he can fight and when he cannot will
                      !  be victorious                    - Sun Tzu
IBM - I Bought        ! Alway mystify, mislead, and surprise the enemy
      Macintosh       !                                   - Stonewall Jackson
------------------------------------------------------------------------------
   
PS:  If it matters, I am programming on a MAC with Lightspeed C..but I don't
     think it should..

haggas@kean.ucs.mun.ca (12/11/90)

In article <12677@milton.u.washington.edu>, mm@blake.u.washington.edu (Eric Gorr) writes:
> Well, here it is for all you C experts:
> 
> In BASIC, you can write the simple, one line instruction:
> 
>     TEST = "Hello"
> 
> What I want to know is how to do this in C after the code where I declared the
> varables.  Please provide some sample code...
> 
> Please Respond through E-mail.
> 
> Thanx for your help.....!!
> 
> ------------------------------------------------------------------------------
> Mystery_Man           ! All warfare is based on deception - Sun Tzu
>                       ! Diplomacy is the art of letting someone else have
> mm@                   !  your war                         - Danielle Vare'
> blake.u.washington.edu! He who knows when he can fight and when he cannot will
>                       !  be victorious                    - Sun Tzu
> IBM - I Bought        ! Alway mystify, mislead, and surprise the enemy
>       Macintosh       !                                   - Stonewall Jackson
> ------------------------------------------------------------------------------
>    
> PS:  If it matters, I am programming on a MAC with Lightspeed C..but I don't
>      think it should..
I won't reply via E mail because ANEWS is cheaper for us.
You may define a global or externa string array, that is outside of 
the function main(), like this:
char string[] = "Test";	/* the null char '\0' is automatically 
appended */

or you may define an automatic variable in the main() function, like 
this:
static char string[] = "Test";	/* viable and seen only in main() */
				/* local or automatic variables are */
				/* not created until the function is 
called, static variable are created by the compiler and don't 
disappear when the funtion ( main() ) terminates. */

or , if your cranky:

static char string[] = { 'T', 'e', 's', 't', '\0' };
/* character by character - don't forget the end of string terminator 
*/
A global string array will be visible to all the functions succeeeding 
it, if it is global; therefore, if you wish your string array to be 
visible to all functions including main(), it must preceed main()
in the global data block.
Good luck!

gwyn@smoke.brl.mil (Doug Gwyn) (12/11/90)

In article <12677@milton.u.washington.edu> mm@blake.u.washington.edu (Eric Gorr) writes:
>In BASIC, you can write the simple, one line instruction:
>    TEST = "Hello"
>What I want to know is how to do this in C after the code where I declared the
>varables.  Please provide some sample code...

The problem with such a question is that it betrays a lack of training in
the C language.  Rather than asking out-of-context detailed technical
questions like this, you should be concerned with how you can learn to
program effectively in C.  Start with Kernighan & Ritchie's "The C
Programming Language", second edition if possible, which explains all
you need to know about handling strings in C.

gordon@osiris.cso.uiuc.edu (John Gordon) (12/11/90)

	To assign a string to a char array after declaration:

	char my_string[80];

	(some code)
	(some more code)

	strcpy(my_string, "Now is the time for all good men...");


---
John Gordon
Internet: gordon@osiris.cso.uiuc.edu        #include <disclaimer.h>
          gordon@cerl.cecer.army.mil       #include <clever_saying.h>

gordon@osiris.cso.uiuc.edu (John Gordon) (12/11/90)

gwyn@smoke.brl.mil (Doug Gwyn) writes:

>In article <12677@milton.u.washington.edu> mm@blake.u.washington.edu (Eric Gorr) writes:
>>In BASIC, you can write the simple, one line instruction:
>>    TEST = "Hello"
>>What I want to know is how to do this in C after the code where I declared the
>>varables.  Please provide some sample code...

>The problem with such a question is that it betrays a lack of training in
>the C language.  Rather than asking out-of-context detailed technical

	Out-of-context?!  Err, maybe you didn't have your coffee this morning,
but from *my* point of view this looks like comp.lang.c, which is *exactly*
the place to ask questions like the above.

		Bucket Brigade, stand ready!

gwyn@smoke.brl.mil (Doug Gwyn) (12/11/90)

In article <1990Dec11.023928.8098@ux1.cso.uiuc.edu> gordon@osiris.cso.uiuc.edu (John Gordon) writes:
>	Out-of-context?!  Err, maybe you didn't have your coffee this morning,
>but from *my* point of view this looks like comp.lang.c, which is *exactly*
>the place to ask questions like the above.

That wasn't at all what I meant.  Haven't you heard about things being
taken "out of context" before?  I thought that was common English usage.