Strings
Strings
C Strings are nothing but array of characters ended with null character (‘\0’).
This null character indicates the end of the string.
Strings are always enclosed by double quotes. Whereas, character is enclosed by single
quotes in C.
char str[100];
It holds characters as you would expect: str[0] is the first character of the string, str[1] is the
second character, and so on. But why is a 100-element array unable to hold up to 100 characters?
Because C uses null-terminated strings, which means that the end of any string is marked by
null character which is represented in C as '\0'. C has to count the characters until it finds '\0'.
C provides no explicit support for strings in the language itself, all of the string-handling
functions are implemented in libraries. The string I/0 operations (gets, puts, and so on) are
implemented in <stdio.h>, and a set of fairly simple string manipulation functions are
implemented in <string.h> (on some systems, <strings.h> ).
The string library (<string.h> contains string library function called. Here is an extremely
common piece of code to find in a normal C program:
C String functions:
String.h header file supports all the string functions in C language. All the string
functions are given below.
Each string function name below for detail description and example programs.
strcpy ( ) : Copies str2 into str1
strcpy( ) function copies contents of one string into another string. Syntax for strcpy
function is given as : strcpy (destination, source );
Example:
strcpy ( str1, str2) – It copies contents of str2 into str1. strcpy ( str2, str1). It copies
contents of str1 into str2.
If destination string length is less than source string, entire source string value won’t be
copied into destination string.
For example, consider destination string length is 20 and source string length is 30. Then,
only 20 characters from source string will be copied into destination string and remaining
10 characters won’t be copied and will be truncated.
Example program for strcpy( ) function in C:
#include <stdio.h>
#include <string.h>
void main( )
{
char source[ ] = "fresh2refresh" ;
char target[20]= "" ;
printf ( "\nsource string = %s", source ) ;
printf ( "\ntarget string = %s", target ) ;
strcpy ( target, source ) ;
printf ( "\ntarget string after strcpy( ) = %s", target ) ;
}
strncpy ( ) : Copies given number of characters of one string to another
strncpy( ) function copies portion of contents of one string into another string. Syntax for
strncpy( ) function is given as. strncpy ( destination, source, size_t num );
Example:
strncpy ( str1, str2, 4). It copies first 4 characters of str2 into str1.
If destination string length is less than source string, entire source string value won’t be
copied into destination string.
For example, consider destination string length is 20 and source string length is 30.
If you want to copy 25 characters from source string using strncpy( ) function, only 20
characters from source string will be copied into destination string and remaining 5
characters won’t be copied and will be truncated.
Example program for strncpy( ) function in C:
#include <stdio.h>
#include <string.h>
void main( )
{
char source[ ] = "fresh2refresh" ;
char target[20]= "" ;
printf ( "\nsource string = %s", source ) ;
printf ( "\ntarget string = %s", target ) ;
strncpy ( target, source, 5 ) ;
printf ( "\ntarget string after strcpy( ) = %s", target ) ;
}
strcmp ( ) : Returns 0 if str1 is same as str2. Returns <0 if strl < str2. Returns >0 if str1 >
str2
strcmp( ) function in C compares two given strings and returns zero if they are same.
If length of string1 < string2, it returns < 0 value. If length of string1 > string2, it returns
> 0 value. Syntax for strcmp( ) function is given as. strcmp (str1,str2 );
strcmp( ) function is case sensitive. i.e, “A” and “a” are treated as different characters.
You use the strcmp function in the string library to compare two strings. It returns an integer
that indicates the result of the comparison. Zero means the two strings are equal, a negative value
means that s1 is less than s2, and a positive value means s1 is greater than s2.
C Math Example
Let's see a simple example of math functions found in math.h header file.
#include<stdio.h>
#include <math.h>
void main(){
printf("\n%f",ceil(3.6));
printf("\n%f",ceil(3.3));
printf("\n%f",floor(3.6));
printf("\n%f",floor(3.2));
printf("\n%f",sqrt(16));
printf("\n%f",sqrt(7));
printf("\n%f",pow(2,4));
printf("\n%f",pow(3,3));
printf("\n%d",abs(-12));
}
Output:
4.000000
4.000000
3.000000
3.000000
4.000000
2.645751
16.000000
27.000000
12
C Preprocessor directives:
Before a C program is compiled in a compiler, source code is processed by a program called
preprocessor. This process is called preprocessing. The preprocessor will process directives
that are inserted into the C source code. These directives allow additional actions to be taken on
the C source code before it is compiled into object code. Directives are not part of the C
language itself. Preprocessor directives are lines included in a program that begin with the
character #, which make them different from a typical source code text. They are invoked by the
compiler to process some programs before compilation.
#define – This macro defines constant value and can be any of the basic data types.
Example : #define height 100
#include <file_name> – The source code of the file “file_name” is included in the main C
program where “#include <file_name>” is mentioned.
Example : #include <stdio.h>
exit()
exit() is a standard library function, which terminates program execution when it is called.
stdlib.h needs to be included in order to use exit().It returns the control to the operating system
or another program that uses this one as a sub-process.
#include<stdio.h>
#include<stdlib.h>
int i=0;
for(i=0; i<5; i++)
{
if(i==2)
exit(0);
printf("%d ", i);
}
In the above example, the loop continues normally for i=0, i=1 and when it reaches i=2, it
executes line exit(0); statement and your program is over. The output will be: 0,1.