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

submitted by mkenny400 on 03/19/18 1

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! www.patreon.com/calebcurry Subscribe to my newsletter: eepurl.com/-8qtH Donate!: bit.ly/DonateCTVM2. ~~~~~~~~~~~~~~~Additional Links~~~~~~~~~~~~~~~ More content: CalebCurry.com Facebook: www.facebook.com/CalebTheVideoMaker Google+: plus.google.com/+CalebTheVideoMaker2 Twitter: twitter.com/calebCurry Amazing Web Hosting - www.dreamhost.com/r.cgi?1487063 (The best web hosting for a cheap price!)

Leave a comment

Be the first to comment

Collections with this video
Email
Message
×
Embed video on a website or blog
Width
px
Height
px
×
Join Huzzaz
Start collecting all your favorite videos
×
Log in
Join Huzzaz

facebook login
×
Retrieve username and password
Name
Enter your email address to retrieve your username and password
(Check your spam folder if you don't find it in your inbox)

×