AR23 REC IP Unit-IV
AR23 REC IP Unit-IV
ENGINEERING COLLEGE
(AUTONOMOUS | VISAKHAPATNAM)
#include<stdio.h>
#include<math.h>
int main()
{
double n, s;
printf("Enter a number ");
scanf("%lf", &n) ;
s=sqrt(n);
printf("The square root of %. 2lf is %.2lf\n", n, s );
}
For example:
int sum (int x, int y) /*Function definition*/
{
int s;
s=x+y;
return s;
}
• In the above program, main() is the calling function, sum() is the called function, “a, b” are actual
arguments, and “x, y” are formal arguments. The function call is written on the right-hand side of the
assignment operator as, s = sum(a, b);
In function definition (called function),variables declared In function definition (called function),variables declared
as formal arguments are normal variables. as formal arguments are pointer variables.
Changes made in function copy are not reflected in Changes made in function copy are reflected in original
original copy of function copy of function
• When factorial( ) is called with n=0 then the condition inside the if statement
becomes true, so now the recursion stops and control returns to factorial(l)
• Now every called function will return the value to the previous function. These
values are returned in the reverse order of function calls.
• Recursive functions work in two phases. First one is the winding phase and the
next one is unwinding phase. In winding phase the function keeps on calling
itself. The winding phase stops when the terminating condition arrives in a call,
now the unwinding phase starts and the called functions return values in reverse
order.
Introduction to Programming Raghu Engineering College (A) 46
Example: Program to print the Fibonacci series
• Now we'll write a recursive definition for finding Fibonacci numbers
• Each character occupies one byte and compiler automatically inserts the
null character at the end. The string constant "Taj Mahal" is actually a
pointer to the character 'T'. So whenever a string constant. used in the
program it is replaced by a pointer pointing to the string.
• If we have a pointer variable of type char *, then we can assign the
address of this string constant to it as-
char *p = "Taj Mahal";
Introduction to Programming Raghu Engineering College (A) 51
String Variables:
• To create a string variable we need to declare a character array with
sufficient size to hold all the characters of the string including null
character.
char str[ ] = {'N' , 'e' , 'w' , ' ', 'Y' , 'o' , 'r' , 'k' , '\0‟};
• We may also initialize it as
char str[ ] = "New York";
• This initialization is same as the previous one and in this case the
compiler automatically inserts the null character at the end. The array
str will be stored in memory as-
For example: Find the length of the string without Library function.
#include <stdio.h>
int main()
Output:
{
Length of string Raghu is 5
char s[]="Raghu";
int i, len=0;
for(i=0; s[i] != '\0'; i++)
len++;
printf("Length of string %s is %d", s, len);
}
#include <stdio.h>
#include <string.h>
int main()
{
char s1[]="Raghu ", s2[]="Engg College";
printf ("Before concatenation \n\tfirst string is %s \n\tSecond string is %s\n", s1, s2) ;
strcat(s1,s2);
printf ("After concatenation \n\tfirst string is %s \n\tSecond string is %s\n", s1, s2) ;
}
#include <stdio.h>
#include <string.h>
int main()
{
char s1[30]="Raghu ", s2[]="Engg College";
int len=0, i, j;
printf ("Before concatenation \n\tfirst string is %s \n\tSecond string is %s\n", s1, s2) ;
for(i=0; s1[i] != '\0'; i++)
len++;
for(j=0; s2[j] != '\0'; j++)
s1[len++] = s2[j];
s1[len]='\0';
printf ("After concatenation \n\tfirst string is %s \n\tSecond string is %s\n", s1, s2) ;
}
#include<stdio.h> Output:
Hello
int main()
{
char str[6] = "Hello";
int i;
return 0;
}
str1[3]='P';
str3[3]='L';
printf("str1=%s\n", str1);
printf("str2=%s\n", str2);
printf("str3=%s\n", str3);
}
int main(int argc, char *argv[]) int main(int argc, char **argv)
{ {
/* ... */ or /* ... */
} }