Strings
  • In C, a String is an array of characters, but the string holds the characters followed by a null. 
  • The difference between an array and a string is the string is terminated with a unique character ‘\0’.i.e., a String is a null-terminated character array.

          Syntax:

                           char string_name[size];

 Memory representation of string in C:

String: 

      H

E

L

L

O

\0

Indexes:          0                     1                     2                    3                      4              5     

Address:0x23451

0x23451

0x23451

0x23451

0x23451

0x23451

  • There is no need to place the null character at the end of a string constant because the C compiler automatically places ‘\0’ at the end of the string at the time of array initialization.
  • But, When we declare a character array that holds a string, we need to declare one character longer than it contains. 
  • For example, to declare an array string that can hold a 5-character string, we have to write a char string[6]. 
  • When we use a quoted string constant in our program, we create a null-terminated string.
  • In C, a string constant is a list of characters that are enclosed in double quotes.

          Example: ”hello” 

          There is no need to add the null to the end of string constants manually— the compiler does this for you automatically.

          Program:

#include<stdio.h>
int main()
{
    char Message[13] = {'S','c','r','i','p','t','W','i','t','h','M','e'};
    printf("Message is: %s\n", Message);
    return 0;
}
Output:
Message is: ScriptWithMe
  • C supports a wide range of functions to manipulate strings:

strlen(str1);

          This function returns the length of string str1.

strcpy(str1, str2);

          This function Copies string str2 into the string str1.

strcmp(str1, str2);

          This function compares the strings str1 and str2. It returns 0 if str1 and str2 are the same and gives a value less than 0 if str1s2.

strcat(str1, str2);

         This function concatenates string str2 onto the end of string str1.

Program:

#include<stdio.h>
#include<string.h>
int main()
{
    char str1[14]= "Hello";
    char str2[14]= "ScriptWithMe";
    int len;
    len = strlen(str1);
    printf("length of string 1 is: %d\n", len);
    strcpy(str1,str2);
    printf("copying string 2 into string 1: %s\n", str1);
    strcat(str1,str2);
    printf("concatenation of string 1 and string 2: %s\n", str1);
    return 0;
}

Output:

length of string 1 is: 5
copying string 2 into string 1: ScriptWithMe
concatenation of string 1 and string 2: ScriptWithMeScriptWithMe
Constants
  • A constant is a value or variable that can’t be changed once created.
  • A keyword const is used to define constant in C programming.

Example 1:

                    const float a=5.1;

#include<stdio.h>
int main()
{
  const float a = 5.1;
  printf("The value of a is: %f", a);
  return 0;
}
Output:
The value of a is: 5.100000
Example 2:
#include<stdio.h>
int main()
{
  const float a = 5.1;
  a = 4.7;
  printf("The value of a is: %f", a);
  return 0;
}
Output:
In function ‘main’:
error: assignment of read-only variable ‘a’
    a = 4.7;
      ^
  • We can see that the variable ‘a’ is declared constant in the first program, and we got the output.
  • But in the second program, we tried to change the value of the constant ‘a’. So we will get an error message because once a variable is declared constant, its value can’t be changed.
Escape Sequence
  • When a character in C language is preceded by a backslash (\) and gives a special meaning to the compiler, it is known as an Escape Sequence.
  • It is also known as Backslash Character Constants or Special Characters.

Escape Sequence                  Meaning

          \b                                    Backspace

          \f                                     Form feed

          \n                                    New line

          \r                                     Carriage return

          \t                                     Horizontal tab

          \”                                     Double quote

          \’                                     Single quote

          \ \                                    Backslash

          \v                                    Vertical tab

          \a                                    Alert

          \?                                    Question mark

Program:

#include<stdio.h>
int main()
{
    printf("Enjoy \n learning \n\'C\' language \nwith  \n\"ScriptWithMe\"");
    return 0;
}
Output:
Enjoy
learning
'C' language
with
"ScriptWithMe"
 
Identifiers
  • The variable names, function names, labels, and several other user-defined items are called identifiers in the C language.
  • The length of identifiers can vary from one to several characters, i.e., identifiers may be of any size.
  • The initial character should be a letter or an underscore, and subsequent characters must be letters, digits, or underscores.
  • An identifier should not begin with any numerical digit.
  • In an identifier, upper-case and lowercase letters are treated as distinct. Hence, we can say that the word count, Count, and COUNT are the three separate identifiers.
  • An identifier cannot be identical to a C keyword.
  • It should not have a similar name as functions that are in the C library.

Valid identifiers :

                                      profit, total, sum, avg, _n _, sum_5, etc.

Invalid identifiers:

                                        char (reserved word) 

                                        float (reserved word) 

                                        1sum (starts with a numerical digit) 

                                        x+y (special character, i.e., ‘+’) 

Types of Identifiers:

          C defines two kinds of identifiers:

  1. External Identifier
  2. Internal Identifier
  1. External Identifier
  • If an identifier is involved in an external link process, this type of identifier is called External.
  • It includes function names and global variable names that are shared between source files.
  1. Internal Identifier:
  •  If an identifier is not used in an external link process, then this type of identifier is called an Internal.
  •  It includes local variable names.

Who is the course for?

Prerequisites before starting this course:

Questions 🙵 Answers.

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent eu orci faucibus orci malesuada semper eget non tellus. Cras sed dignissim purus. Mauris varius neque leo, eu pellentesque justo venenatis et. Sed ultricies risus non turpis tempus, nec  nulla suscipit. In comdo urna eu turpis accumsan, et viverra mauris fringillaCras interdum 

Video 48 Min  + 2 Min read to complete

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent eu orci faucibus orci malesuada semper eget non tellus. Cras sed dignissim purus. Mauris varius neque leo, eu pellentesque justo venenatis et. Sed ultricies risus non turpis tempus, nec  nulla suscipit. In comdo urna eu turpis accumsan, et viverra mauris fringillaCras interdum 

Video 48 Min  + 2 Min read to complete

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent eu orci faucibus orci malesuada semper eget non tellus. Cras sed dignissim purus. Mauris varius neque leo, eu pellentesque justo venenatis et. Sed ultricies risus non turpis tempus, nec  nulla suscipit. In comdo urna eu turpis accumsan, et viverra mauris fringillaCras interdum 

Video 48 Min  + 2 Min read to complete

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent eu orci faucibus orci malesuada semper eget non tellus. Cras sed dignissim purus. Mauris varius neque leo, eu pellentesque justo venenatis et. Sed ultricies risus non turpis tempus, nec  nulla suscipit. In comdo urna eu turpis accumsan, et viverra mauris fringillaCras interdum 

Video 48 Min  + 2 Min read to complete

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent eu orci faucibus orci malesuada semper eget non tellus. Cras sed dignissim purus. Mauris varius neque leo, eu pellentesque justo venenatis et. Sed ultricies risus non turpis tempus, nec  nulla suscipit. In comdo urna eu turpis accumsan, et viverra mauris fringillaCras interdum 

Video 48 Min  + 2 Min read to complete

More Courses

You might also be interested in these courses