C Programming Tutorial 14 – C Basics Part 6 – Working with Strings

When we first started this series, we created a program where all it did was say hello world on the screen.

Well, what if we wanted this to say something like our name?

We could modify the program by replacing the printf() line with this:

printf(“Hello %s, You look nice today.\n”, “Caleb”);

We are passing as an argument what is known as a string, which always has double quotes. You can think of a string a bunch of characters strung together.

This is cool and all, but what if we want to ask the user using our program what his or her name is?

In order to do that, before we print we need to create a variable. We’ll discuss the specifics of how this variable works in a future video, but we are going to be creating what is known as a char array.

char name[31];

This allows the user to put in up to 30 characters. You actually don’t want to use the last character because that character is used to say it is the end of your name. If you don’t reserve that last character, the computer might think your name goes on longer than it really does. The last character in that string is called the null terminator.

Now, we can get input using scanf();

printf(“Yo gurl enter yo name: “);
scanf(“%s”, name);

Now, in previous videos we had to use an ampersand before the variable name, but we actually don’t have to do this with arrays, as we will discuss more in the future when we start working with arrays and pointers.

scanf() only accepts one word, so you can’t put your entire name, just use your first name.

if we run the program, it still does the same thing. We are getting the input but not doing anything with it.

Lastly, we can modify our final printf():

printf(“Hello %s, You look nice today.\n”, name);

By the time you get through this series, this stuff will be easy. I just wanted to give you some of it now so you could start making your own cool programs!

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Support me! http://www.patreon.com/calebcurry

Subscribe to my newsletter: http://eepurl.com/-8qtH

Donate!: http://bit.ly/DonateCTVM2.

~~~~~~~~~~~~~~~Additional Links~~~~~~~~~~~~~~~

More content: http://CalebCurry.com
Facebook: http://www.facebook.com/CalebTheVideoMaker
Google+: https://plus.google.com/+CalebTheVideoMaker2
Twitter: http://twitter.com/calebCurry

Amazing Web Hosting – https://www.dreamhost.com/r.cgi?1487063 (The best web hosting for a cheap price!)

Comments

Hank Chan says:

Just curious. Is scanf() deprecated? I was told to favor the use of fgets() over scanf()

Men On A Mission with JAMES ADAMS says:

Hello how are you? I followed one of your video instructing me on how to install my Logitech C20 webcam to my laptop and things went great all is well in that department, but for some reason it won’t let me download my video to YouTube, do you have any suggestion on what could be the problem and how I can fix it? Thank you in advance for your time… “Sincerely Your Subscriber”

Mike Woodward says:

Solid tutorial… Do you have anything about storing strings into pointers in c?

Write a comment

*

Human Verification: In order to verify that you are a human and not a spam bot, please enter the answer into the following box below based on the instructions contained in the graphic.


Do you like our videos?
Do you want to see more like that?

Please click below to support us on Facebook!

Send this to a friend

▷ Other ReviewsVehicles▷ Show Cars▷ Motorbikes▷ Scooters▷ Rims & Tires▷ Luxury BoatsFashion▷ Sunglasses▷ Luxury Watches▷ Luxury Purses▷ Jeans Wear▷ High Heels▷ Perfumes▷ Jewellery▷ Cosmetics▷ Shaving Helpers▷ Fashion HatsFooding▷ Chef Club▷ Fooding Helpers▷ Coktails & LiquorsSports▷ Sport Shoes▷ Fitness & Detox▷ Golf Gear▷ Racquets▷ Diving Equipment▷ Ski Gear▷ Snowboards▷ Surf Boards▷ Rollers & SkatesEntertainment▷ DIY Guides▷ Zik Instruments▷ Published Books▷ Music Albums▷ Cine Movies▷ Trading Helpers▷ Make Money▷ Fishing Equipment▷ Paintball Supplies▷ Trading Card Games▷ Telescopes▷ Knives▷ VapesHigh Tech▷ Flat Screens▷ Tech Devices▷ Camera Lenses▷ Printers▷ USB Devices▷ PC Hardware▷ Network Gear▷ Cloud Servers▷ Software Helpers▷ Programmer Helpers▷ Mobile Apps▷ Hearing AidsHome▷ Home Furniture▷ Home Appliances▷ Beddings▷ Floor Layings▷ Barbecues▷ Aquarium Gear▷ Safe Boxes▷ Office Supplies▷ Security Locks▷ Cleaning ProductsKids▷ Baby Strollers▷ Child Car Seats▷ Remote ControlledTravel▷ Luggages & Bags▷ Airlines Seats▷ Hotel Rooms▷ Fun Trips▷ Cruise Ships▷ Mexico Tours