Type Conversion in C
Type Conversion in C
char ch;
for(ch = 'A' ; ch <= 'Z' ; ch++)
{
putchar(ch);
}
return(0);
}
function - gets()
• 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.
#include <stdio.h>
Output:
int main () Enter a string :hai what doing
{ You entered: hai what doing
char str[50];
printf("Enter a string : ");
gets(str);
printf("You entered: %s", str);
return(0);
}
function - puts()
• writes a string to stdout up to but not including the null character. A
newline character is appended to the output.
#include <stdio.h>
#include <string.h>
int main () { Output:
char str1[15]; HAI TO ALL
HOW ARE YOU
char str2[15];
strcpy(str1, "HAI TO ALL");
strcpy(str2, "HOW ARE YOU");
puts(str1);
puts(str2);
return(0);
}
Arrays
• An array is a collection of data that holds fixed
number of values of same type.
• For example:
– if you want to store marks of 100 students, you can
create an array for it.
– The size and type
of arrays cannot be
changed after its
declaration.
Arrays
• arrays are reffered to as structured data types.
An array is defined as finite ordered
collection of homogenous data, stored in
contiguous memory locations.
– finite means data range must be defined.
– ordered means data must be stored in continuous
memory addresses.
– homogenous means data must be of similar data
type.
Arrays
• Example where arrays are used,
– to store list of Employee or Student names,
– to store marks of students,
– or to store list of numbers or characters etc.
How to declare an array in C?
• Syntax:
– data_type array_name[array_size];
• Example :
– float mark[5];
• we declared an array, mark, of floating-point
type and size 5. Meaning, it can hold 5
floating-point values.
How to access them Elements of an
Array ?
• You can access elements of an array by indices.
• Arrays have 0 as the first index not 1. In this
example, mark[0]
• If the size of an array is n, to access the last
element, (n-1) index is used. In this example, mark[4]
• Suppose the starting address of mark[0] is 2120d.
Then, the next address, a[1], will be 2124d, address
of a[2] will be 2128d and so on.
• It's because the size of a float is 4 bytes.
How to initialize an array in C
programming?
• It's possible to initialize an array during
declaration.
• For example
int mark[5] = {19, 10, 8, 17, 9};
• Another method to initialize array during
declaration:
int mark[ ] = {19, 10, 8, 17, 9};
Compile time Array initialization
#include<stdio.h>
void main()
{
int i;
int arr[]={2,3,4}; // Compile time array initialization
for(i=0;i<3;i++)
{
printf("%d\t",arr[i]);
}
}
Runtime Array initialization
#include<stdio.h>
void main()
{
int arr[4];
int i,j;
printf("Enter array element");
for(i=0;i<4;i++)
{
scanf("%d",&arr[i]); //Run time array initialization
}
for(j=0;j<4;j++)
{
printf("%d\n",arr[j]);
}
}
How to insert and print array elements?
• int mark[5] = {19, 10, 8, 17, 9}
low high
mid
2
0 1 2 3 4 5 6 7 8
5 12 17 23 38 44 77 84 90
0 1 2 3 4 5 6 7 8
5 12 17 23 38 44 77 84 90
0 1 2 3 4 5 6 7 8
5 12 17 23 38 44 77 84 90
0 1 2 3 4 5 6 7 8
5 12 17 23 38 44 77 84 90
Unsuccessful Search
high low
no more elements to search low > high
Strings
• Strings are actually one-dimensional array of
characters terminated by a null character '\0'.
Thus a null-terminated string contains the
characters that comprise the string followed by
a null.
S.N. Function & Purpose
strcpy(s1, s2);
1 Copies string s2 into string s1.
strcat(s1, s2);
2 Concatenates string s2 onto the end of string s1.
strlen(s1);
3 Returns the length of string s1.
strcmp(s1, s2);
4 Returns 0 if s1 and s2 are the same; less than 0 if s1<s2; greater than 0 if
s1>s2.
strlen() function
• strlen( ) function counts the number of
characters in a given string and returns the
integer value.
• It stops counting the character when null
character is found. Because, null character
indicates the end of the string in C.
• Example: length of the string “freshers” is
determined by strlen( ) function as below.
Length of this string 8 is displayed as output.
strlen() function
Calculate Length of String without Using
strlen() Function
strcat() function
• strcat( ) function in C language concatenates
two given strings.
• It concatenates source string at the end of
destination string.
• Syntax.
char * strcat (char * destination, const char * source );
• Example:
strcat ( str2, str1 ); – str1 is concatenated at the end of str2.
strcat ( str1, str2 ); – str2 is concatenated at the end of str1.
strcat() function
Concatenate Two Strings Without Using
strcat()
strcpy() function
• 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.
strcpy() function
Copy String Manually Without Using
strcpy()
strcmp() function
• 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.
• Syntaxstrcmp()
int strcmp ( const char * str1, const char * str2 );
• strcmp( ) function is case sensitive. i.e, “A” and
“a” are treated as different characters.
strcmp() function
• The strcmp() compares two strings character by character.
• If the first character of two strings are equal, next character of
two strings are compared.
• This continues until the corresponding characters of two
strings are different or a null character '\0' is reached.
strcmp() function
Compare two String Manually Without
Using strcmp()
Passing strings to function:
• As strings are character arrays, so we can pass
strings to function in a same way we pass an
array to a function.
#include<stdio.h>
void printStr(char str[])
{
printf("String is:%s",str);
}
int main()
{
// declare and initialize string
char str[]="jntukucev";
// print string by passing string
// to a different function
printStr(str);
return 0;
}
Dynamic memory allocation
• The process of allocating memory during program
execution is called dynamic memory allocation.
• The process of allocating memory at runtime is known
as dynamic memory allocation. Library routines known
as memory management functions are used for allocating
and freeing memory during execution of a program. These
functions are defined in stdlib.h header file.
• Dynamic memory management refers to manual memory
management.
• This allows you to obtain more memory when required and
release it when not necessary.
Dynamic memory allocation
• C language offers 4 dynamic memory
allocation functions. They are,
– malloc()
– calloc()
– realloc()
– free()
Dynamic memory allocation
Function Description
malloc() allocates requested size of bytes and returns a void pointer
pointing to the first byte of the allocated space