Class 6 Exam  >  Class 6 Notes  >  C Programming for Beginners  >  C Strings & User Input

C Strings & User Input | C Programming for Beginners - Class 6 PDF Download

Strings

  • Strings are used for storing text/characters.
  • For example, "Hello World" is a string of characters.
  • Unlike many other programming languages, C does not have a String type to easily create string variables. 

However, you can use the char type and create an array of characters to make a string in C:

char greetings[] = "Hello World!";

Note that you have to use double quotes.
To output the string, you can use the printf() function together with the format specifier %s to tell C that we are now working with strings:

Example

char greetings[] = "Hello World!";

printf("%s", greetings);

Access Strings

  • Since strings are actually arrays in C, you can access a string by referring to its index number inside square brackets [].
  • This example prints the first character (0) in greetings:

Example

char greetings[] = "Hello World!";

printf("%c", greetings[0]);

Note that we have to use the %c format specifier to print a single character.

Modify Strings

To change the value of a specific character in a string, refer to the index number, and use single quotes:

Example

char greetings[] = "Hello World!";

greetings[0] = 'J';

printf("%s", greetings);

// Outputs Jello World! instead of Hello World!

Another Way Of Creating Strings

  • In the examples above, we used a "string literal" to create a string variable. This is the easiest way to create a string in C.
  • You should also note that you can to create a string with a set of characters. This example will produce the same result as the one above:

Example

char greetings[] = {'H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd', '!', '\0'};

printf("%s", greetings);

Why do we include the \0 character at the end? This is known as the "null termininating character", and must be included when creating strings using this method. It tells C that this is the end of the string.

Differences

  • The difference between the two ways of creating strings, is that the first method is easier to write, and you do not have to include the \0 character, as C will do it for you.
  • You should note that the size of both arrays is the same: They both have 13 characters (space also counts as a character by the way), including the \0 character:

Example

char greetings[] = {'H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd', '!', '\0'};

char greetings2[] = "Hello World!";


printf("%lu\n", sizeof(greetings));   // Outputs 13

printf("%lu\n", sizeof(greetings2));  // Outputs 13

C User Input

User Input

  • You have already learned that printf() is used to output values in C.
  • To get user input, you can use the scanf() function:

Example

Output a number entered by the user:

// Create an integer variable that will store the number we get from the user

int myNum;


// Ask the user to type a number

printf("Type a number: \n");


// Get and save the number the user types

scanf("%d", &myNum);


// Output the number the user typed

printf("Your number is: %d", myNum);

The scanf() function takes two arguments: the format specifier of the variable (%d in the example above) and the reference operator (&myNum), which stores the memory address of the variable.

Tip: You will learn more about memory addresses and functions in the next chapter.

User Input Strings

You can also get a string entered by the user:
Example

Output the name of a user:

// Create a string

char firstName[30];


// Ask the user to input some text

printf("Enter your first name: \n");


// Get and save the text

scanf("%s", firstName);


// Output the text

printf("Hello %s.", firstName);

Note that you must specify the size of the string/array (we used a very high number, 30, but atleast then we are certain it will store enough characters for the first name), and you don't have to specify the reference operator (&) when working with strings in scanf().

The document C Strings & User Input | C Programming for Beginners - Class 6 is a part of the Class 6 Course C Programming for Beginners.
All you need of Class 6 at this link: Class 6
10 videos|13 docs|15 tests

Top Courses for Class 6

10 videos|13 docs|15 tests
Download as PDF
Explore Courses for Class 6 exam

Top Courses for Class 6

Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev
Related Searches

past year papers

,

mock tests for examination

,

Exam

,

Semester Notes

,

practice quizzes

,

Objective type Questions

,

shortcuts and tricks

,

C Strings & User Input | C Programming for Beginners - Class 6

,

study material

,

Previous Year Questions with Solutions

,

Important questions

,

Sample Paper

,

video lectures

,

Extra Questions

,

Viva Questions

,

Free

,

Summary

,

C Strings & User Input | C Programming for Beginners - Class 6

,

pdf

,

MCQs

,

C Strings & User Input | C Programming for Beginners - Class 6

,

ppt

;