Course Content
History of C Language
0/2
Basic Structure of C Program
0/1
Types of Errors
0/1
Language Fundamentals
0/1
Data Types and Modifiers
0/1
Programming with C Language
About Lesson

Reading and Writing Strings

Since character array and string are group of characters we have to use the format specifier %s instead of %c to perform read or write operations on the character array or string using standard i/o functions(i.e. printf and scanf).  C language also providing some other functions which are only works with strings or character arrays like gets(), puts(), getch(), putch() etc.  These functions are also known as unformatted i/o functions.

//Example code showing how to read or write into character array

#include<stdio.h>

void main()

{

    char name[5];

    printf("Enter your name");

    scanf("%s",name); //note there is no subscript or & is used for 'name'

    printf("nHi %s, Welcome",name);

}

When you running the above program, we may observe you could not print the second word in your name.  This is because scanf() function only takes input until a delimiter like space has inputted.

Change the scanf() in the above program like below and try again

scanf(“%[^n]s”,name);

You can also unformatted i/o function gets() as a solution to the above problem which will be discussed later in this chapter.

//Example code to get an Enter from User

#include

void main()

{

    printf(“press Enter:”);

    scanf(“%[^n]”);

    printf(“nThank you…”);

}

Unformatted I/O

Unformatted I/O in C language is done by the functions like getch, getche, putch, purchar() etc.  These functions directly expect a character or text input from the user or print text based output on to the console.  No format specifiers will be used for these functions.

getchar()

This function gets the character from user and stores in the variable.  It doesn’t accept input until pressed Enter.

#include

main()

{

            printf(“Enter a character:”);

char a=getchar();

            printf(“nThe character is %c”,a);

}

puts() and putchar()

It writes the entire sentence, which is in double quotes to the screen.  It supports escape sequences but not format specifiers.  The putchar() writes its character argument to the screen.

#include

main()

{

            char a;

            puts(“Enter a character:”);

            a=getchar();

            puts(“nThe character entered is”);

            putchar(a);

}

getche()

This function assigns the character obtained to the variable, by displaying it on the screen (‘e’ stands for echoing) while getch() returns the obtained character without echoing.

#include

main()

{

            char a;

            puts(“Enter a character:”);

            a=getche();       //replace the getche() with getch() and observe the difference

            printf(“nThe character entered is %c”,a);

}

gets()

The function takes entire text string or phrase from the user and inserts into a character array or string.

//code demonstrates working of gets() function

#include

main()

{

            char name[10];

            puts(“Enter your Name:”);

            gets(name);

            printf(“nHello %s, Welcome to C programming”,name);

}

You cannot copy content of this page