C Programming language

C Standard Library Function

Day 1: Introduction and brief history of C Programming language

Day 1: Advantages and Disadvantages of C, C Keywords, Data type modifiers in C

Day 1: Data types in C Programming language

Day 1: Secondary data types, Primitive and Non-primitive data types

Day 1: C Variables, C Constant, Format Specifiers in C

Day 2: Write first C program

Day 2: Flow of C program with example, main(), printf(), scanf()

Day 2: Operaters in C Programming language, Arithmetic operators

Day 2: Relational operators and Logical Operators in C Programming language

Day 2: Assignment, Increments and Decrement Operators in C Programming language

Day 3: Conditional statement: if else statement in C Programming language

Day 3: Conditional statement: switch statement in C Programming language

Day 3: Jump statements: return statement in C Programming language

Day 3: Jump statements: go to statement in C Programming language

Day 3: Jump statements: break statement in C Programming language

Day 3: Jump statements: continue statement in C Programming language

Day 4: Loops OR Iteration statement in C Programming language: for Loop

Day 4: Loops OR Iteration statement in C Programming language: while Loop

Day 4: Loops OR Iteration statement in C Programming language: do while Loop

Day 5: Array in C Programming language

Day 5: Access elements of Array in C Programming language

Day 5: One dimensional Array representation in memory using C Programming language

Day 5: Two dimensional Array representation in memory using C Programming language

Day 5: Multidimensional Array in C Programming language

Day 6: Function in C Programming language

Day 6: Definition, Declaration and Calling a Function in C Programming language

Day 6: Passing array to a function in C Programming language

Day 6: Calling Function in C Programming language : Call by value

Day 6: Calling Function in C Programming language : Call by reference

Day 6: Recursive Function in C Programming language

Day 6: Adding function to the library in C Programming language

Day 7: Pointer in C Programming language, How to use Pointer, Pointer declaration

Day 7: NULL Pointers in C Programming language

Day 7: Array of Pointers in C Programming language

Day 7: Pointer arithmetic in C Programming language

Day 7: Pointer to Pointer in C Programming language

Day 7: Pointer to Function in C Programming language: Passing pointers to functions

Day 7: Pointer to Function in C Programming language: Return pointer from functions

Day 8: Strings in C Programming language, Declaring String in C Programming language

Day 8: String functions in C Programming language

Introduction of Structure

Accessing the members of Structure

Structure With typedef Keyword and Use of sizeof function

Example of Structure

Dynamic memory allocation in C: Introduction

adplus-dvertising
Day 8 Strings in C Programming language
Previous Home Next

String in C is an array of char values which terminated by a special null character value '\0'. '\0' indicates the end of the string. So when you define a string you should be sure to have sufficient space for the null terminator. In ASCII table, the null terminator has value 0. Strings are always enclosed by double quotes. Whereas, character is enclosed by single quotes in C.

# Some Points

  • String are one-dimensional Array
  • string in/output: printf("%s",S), scanf("%s",S)
  • <string.h> : Collection of functions for string manipulation
# Declaring String

Let us create a string consisting of the word "hellor4r". To hold the null character at the end of the array, the size of the character array containing the string is one more than the number of characters in the word "hellor4r."

String can be initialized in different number of ways. When define string you should have extra space for '\0'(null character)

char string[9] = {'H', 'e', 'l', 'l', 'o','r','4','r', '\0'};

/* OR */
/* character is enclosed by single quotes */
char string[] = {'H', 'e', 'l', 'l', 'o','r','4','r', '\0'};

/* OR */
/* strings are always enclosed by double quotes */
char string[] = "Hellor4r";

/* OR */
char string[9] = "Hellor4r";

Following is the memory presentation of above defined string in C

Index 0 1 2 3 4 5 6 7 8
Variable H e l l o r 4 r '\0'
Address 0x26461 0x26462 0x26463 0x26464 0x26465 0x26466 0x26467 0x26468 0x26469

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: XYZ PQR
Your name is XYZ.

Here, program will ignore PQR because, scanf() function takes only string before the white space. Now use getchar() function to 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]='\0';       // inserting null character at end
    printf("Name: %s",name);
    return 0;
}
Output

Enter name: XYZ PQR
Your name is XYZ PQR.

Note: There are predefined functions gets() and puts in C language to read and display string respectively.

Previous Home Next