Jump to content

How do you read a word in C

Guest
Go to solution Solved by 2FA,
4 minutes ago, deXxterlab97 said:

oh no way. no it only scans single character, i thought about arrays or smoething like that

char stringarray[256];

scanf(%s , stringarray);

Let's say I want to read a word. The word is read after I hit ENTER.

Link to comment
Share on other sites

Link to post
Share on other sites

Just now, Nicholatian said:

You need to be more specific. What do you mean by “word”? This has many differen definitions depending on context, especially in computer science.

Sorry. Basically a word with more than 1 character. Like ilikeblue

Link to comment
Share on other sites

Link to post
Share on other sites

scanf()

[Out-of-date] Want to learn how to make your own custom Windows 10 image?

 

Desktop: AMD R9 3900X | ASUS ROG Strix X570-F | Radeon RX 5700 XT | EVGA GTX 1080 SC | 32GB Trident Z Neo 3600MHz | 1TB 970 EVO | 256GB 840 EVO | 960GB Corsair Force LE | EVGA G2 850W | Phanteks P400S

Laptop: Intel M-5Y10c | Intel HD Graphics | 8GB RAM | 250GB Micron SSD | Asus UX305FA

Server 01: Intel Xeon D 1541 | ASRock Rack D1541D4I-2L2T | 32GB Hynix ECC DDR4 | 4x8TB Western Digital HDDs | 32TB Raw 16TB Usable

Server 02: Intel i7 7700K | Gigabye Z170N Gaming5 | 16GB Trident Z 3200MHz

Link to comment
Share on other sites

Link to post
Share on other sites

Just now, DeadEyePsycho said:

scanf()

oh no way. no it only scans single character, i thought about arrays or smoething like that

Link to comment
Share on other sites

Link to post
Share on other sites

4 minutes ago, deXxterlab97 said:

oh no way. no it only scans single character, i thought about arrays or smoething like that

char stringarray[256];

scanf(%s , stringarray);

[Out-of-date] Want to learn how to make your own custom Windows 10 image?

 

Desktop: AMD R9 3900X | ASUS ROG Strix X570-F | Radeon RX 5700 XT | EVGA GTX 1080 SC | 32GB Trident Z Neo 3600MHz | 1TB 970 EVO | 256GB 840 EVO | 960GB Corsair Force LE | EVGA G2 850W | Phanteks P400S

Laptop: Intel M-5Y10c | Intel HD Graphics | 8GB RAM | 250GB Micron SSD | Asus UX305FA

Server 01: Intel Xeon D 1541 | ASRock Rack D1541D4I-2L2T | 32GB Hynix ECC DDR4 | 4x8TB Western Digital HDDs | 32TB Raw 16TB Usable

Server 02: Intel i7 7700K | Gigabye Z170N Gaming5 | 16GB Trident Z 3200MHz

Link to comment
Share on other sites

Link to post
Share on other sites

7 minutes ago, DeadEyePsycho said:

char stringarray[256]

scanf(%s , stringarray)

yeah but then

format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘char (*)[256]’ [-Wformat=]

 

Link to comment
Share on other sites

Link to post
Share on other sites

4 minutes ago, deXxterlab97 said:

yeah but then


format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘char (*)[256]’ [-Wformat=]

 

May I see what you put for the scanf and array?

 

Edit: I durred and forgot some things I think

 

scanf("%s", stringarray);

 

or

 

scanf("%s", &stringarray);

[Out-of-date] Want to learn how to make your own custom Windows 10 image?

 

Desktop: AMD R9 3900X | ASUS ROG Strix X570-F | Radeon RX 5700 XT | EVGA GTX 1080 SC | 32GB Trident Z Neo 3600MHz | 1TB 970 EVO | 256GB 840 EVO | 960GB Corsair Force LE | EVGA G2 850W | Phanteks P400S

Laptop: Intel M-5Y10c | Intel HD Graphics | 8GB RAM | 250GB Micron SSD | Asus UX305FA

Server 01: Intel Xeon D 1541 | ASRock Rack D1541D4I-2L2T | 32GB Hynix ECC DDR4 | 4x8TB Western Digital HDDs | 32TB Raw 16TB Usable

Server 02: Intel i7 7700K | Gigabye Z170N Gaming5 | 16GB Trident Z 3200MHz

Link to comment
Share on other sites

Link to post
Share on other sites

3 minutes ago, DeadEyePsycho said:

May I see what you put for the scanf and array?

 

Edit: I durred and forgot some things I think

 

scanf("%s", stringarray);

 

or

 

scanf("%s", &stringarray);

ok nvm thanks, i get used to putting & symbol when scanning

Link to comment
Share on other sites

Link to post
Share on other sites

Just now, deXxterlab97 said:

ok nvm thanks, i get used to putting & symbol when scanning

Sometimes you need to though, particularly when working with multiple functions and you're using pointers.

 

stringarray is equivalent to stringarray[0].

 

&stringarray is passing a pointer.

[Out-of-date] Want to learn how to make your own custom Windows 10 image?

 

Desktop: AMD R9 3900X | ASUS ROG Strix X570-F | Radeon RX 5700 XT | EVGA GTX 1080 SC | 32GB Trident Z Neo 3600MHz | 1TB 970 EVO | 256GB 840 EVO | 960GB Corsair Force LE | EVGA G2 850W | Phanteks P400S

Laptop: Intel M-5Y10c | Intel HD Graphics | 8GB RAM | 250GB Micron SSD | Asus UX305FA

Server 01: Intel Xeon D 1541 | ASRock Rack D1541D4I-2L2T | 32GB Hynix ECC DDR4 | 4x8TB Western Digital HDDs | 32TB Raw 16TB Usable

Server 02: Intel i7 7700K | Gigabye Z170N Gaming5 | 16GB Trident Z 3200MHz

Link to comment
Share on other sites

Link to post
Share on other sites

13 hours ago, DeadEyePsycho said:

stringarray is equivalent to stringarray[0].

 

&stringarray is passing a pointer.

Whoa there, it's a little more complicated then that...

 

stringarray decays into a pointer pointing to the first element of the array, stringarray[0] IS the first element in the array, 2 very diffirent things:

char stringarray[] = "Foo";

//stringarray decays into a pointer pointing to the first element, thus:
stringarray == &stringarray[0]

//However:
sizeof(stringarray) != sizeof(&stringarray[0])
  
//The first returns the size of the entire array, the latter returns the 
//size of a char* pointer.
  

There is also a difference between stringarray and &stringarray:

//stringarray and &stringarray both point to the first element in the array, thus:
stringarray == &stringarray
  
//but, they are of diffirent types, thus:
(stringarray + 1) != (&stringarray + 1)
  
//Because stringarray is of type char* and &stringarray is of type 
//char (*)[4]

 

Link to comment
Share on other sites

Link to post
Share on other sites

5 hours ago, DeadEyePsycho said:

char stringarray[256];

scanf(%s , stringarray);

scanf is widely considered not safe, as there is  no way to guard against buffer overflow when the user enters more characters then the length of the array. Common practice is to use fgets on stdin, which allows to pass a maximum amount of characters to read:

char stringarray[256];

fgets(stringarray, 256, stdin);

And simply extract the first word manually, ignoring the rest, this could be easily done by searching the string and replacing the first space with a 0, effectively truncating the string after the first word.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×