February 23, 2025

Telugu Tech Tuts

TimeComputers.in

How to learn c programming in telugu Strings example2 part 34

how to learn c programming in telugu

The C library function int puts(const char *str) writes a string to stdout up to but not including the null character. A newline character is appended to the output.

The C library function char *gets(char *str) reads a line from stdin and stores it into the string pointed to by str.It stops when either the newline character is read or when the end-of-file is reached, whichever comes first.

String variable c can only take a word. It is beacause when white space is encountered, the scanf() function terminates.

Write a C program to illustrate how to read string from terminal.

#include <stdio.h>
int main(){
    char name[20];
    printf("Enter name: ");
    scanf("%s",name);
    printf("Your name is %s.",name);
    return 0;
}

Output

Enter name: Dennis Ritchie
Your name is Dennis.

Here, program will ignore Ritchie because, scanf() function takes only string before the white space.

Reading a line of text

C program to read line of text manually.

#include <stdio.h>
int main(){
    char name[30],ch;
    int i=0;
    printf("Enter name: ");
    while(ch!='n')    // terminates if user hit enter
    {
        ch=getchar();
        name[i]=ch;
        i++;
    }
    name[i]='';       // inserting null character at end
    printf("Name: %s",name);
    return 0;
}

This process to take string is tedious. There are predefined functions gets() and puts in C language to read and display string respectively.