Languages-C & C Plus Plus [TCS Placement]: Sample Questions 86 - 87 of 119

Get unlimited access to the best preparation resource for competitive exams : get questions, notes, tests, video lectures and more- for all subjects of your exam.

Question 86

C & C Plus Plus
Edit

Describe in Detail

Essay▾

How do you determine the length of a string value that was stored in a variable?

Explanation

  • To get the length of a string value, use the function strlen () .
  • For example, variable named “FullName” can get the length of the stored string by using statement: i = strlen (FullName) ;
  • Variable i will have the character length of the string value.
Given the Image is Define the Length of a String Value That Was Stored in a Variable

Question 87

C & C Plus Plus
Edit

Describe in Detail

Essay▾

Do these two program statements prodice the same output?

  1. scanf ( “% c” , &letter) ;
  2. letter = getchar ()

Explanation

  • Yes, they both do the exact same thing, which is to accept the next key pressed by the user and assign it to variable named letter.
  1. scanf ( “% c” , &letter) ;
    • Function int scanf (const char ⚹ format, …) reads formatted input from stdin.
    • If successful, the total number of characters written is returned, otherwise a negative number is returned.
    • Example:
      1. #include <tdio.h >
      2. intmain()
      3. {
      4.     charstr1[20];
      5.     printf(“Entername:”);
      6.     scanf(“%s”, str1);
      7.     return0;
      8. }
    • Output:

    Enter name: Sweta

  2. letter = getchar ()
  • Function int getchar (void) gets a character (an unsigned char) from stdin. This is equivalent to getc with stdin as its argument.
  • This function returns the character read as an unsigned char cast to an int or EOF on end of file or error.
  • Example:
    1. #include <tdio.h >
    2. intmain()
    3. {
    4.     charc;
    5.     printf(“Entername:”);
    6.     c=getchar();
    7.     return0;
    8. }
  • Output:

Enter name: Jasmin

Developed by: