ECE - C Unit-III
ECE - C Unit-III
EC-405 Programming in C
UNIT - III
Strings, Functions & pointers in C
Course Contents: Define string - Reading and writing strings –String manipulation
functions –function call - Return statement, passing parameters to function- Function
calls - Pointer declaration, address and dereferencing operators.
Syntax:
Here, it declares name as a character array (string) variable that can hold a maximum of
10 characters.
Initialization of strings:
A string can be initialized in different ways.
Ex:
A string is always terminated by a NULL character ‘\0’ , which represents the end of
the string.
s s n \0
We can also declare the size much larger than the string size in the initializer.
Ex:
1.Using scanf():
The familiar input function ‘scanf()’ can be used with ‘%s’ format specification
to read in a string of characters.
Ex: char a[10];
scanf(“%s”,a);
Program:
#include<stdio.h>
main()
{
Char a[40],b[40],c[40],d[40];
printf(“Enter text:\n”);
scanf(“%s%s%s%s”,a,b,c,d);
printf(“word1=%s\n word2=%s\n word3=%s\n word4=%s”,a,b,c,d);
}
2. Using getchar() function:
To read a single character from the terminal, using the function getchar().
Thegeneral form for getchar() function is as follows:
Example: char ch;
ch=getchar(); //Note: that getchar() has no parameters.
3. Using gets() function:
gets() function is available in the <stdio.h> header file.
This is a simple function withone string parameter and called as follows:
Example: char name[20];
gets(str);
printf(“%s”,name);
The printf() function with %s format to print strings to the screen.The format %s
canbe used to display an array of characters that is terminated by the null
character.
Example: printf(“%s”, name);
char ch=‘A’;
puts(variable name);
Example:
char name[20];
gets(name);
puts(name);
These are the unformatted I/O functions in which it works only with
characterdatatype(char).
The unformatted Input functions used in C are getch(), getche(), getchar(), gets().
1. getchar():
Syntax: Variable_name=value;
2. getch():
Syntax: variable_name=getch();
Department of Computer Engineering
Aditya Polytechnic Colleges (255, 249, 404), Surampalem Page 3
LECTURE NOTES
EC-405 Programming in C
3. getche():
Like gtech(),getche() also accepts only single character, but unlike getch(),
getche()displays the entered character in the screen.
Syntax: variable_name=getche();
4. putchar():
#include<stdio.h>
void main()
{
char ch;
for(ch='A';ch<='Z';ch++)
{
putchar(ch);
}
return 0;
}
1. strcat()
2. strncat()
3. strcpy()
4. strncpy()
5. strcmp()
6. strlen()
1.strcat() function:
String 1 A R R A Y S \0
String 2 S T R I N G S \0
A R R A Y S S T R I N G S \0
#include<stdio.h>
#include<string.h>
void main()
{
char x[15]="Good";
char y[10]="Morning";
strcat(x, y);
printf("%s" , x); // Result :GoodMorning
}
2.strncat() function:
Syntax: strncat(string1,string2,number);
Syntax: strcpy(string2,string1);
#include<stdio.h>
#include<string.h>
4.strncpy() function:
If you want to copy only part of the string 2 to the string 1 we use strncpy() function.
Syntax: strncpy(string2,string1,number);
#include<stdio.h>
#include<string.h>
void main()
{
char x[10]="WELCOME TO C CLASS";
char y[10];
strncpy(y, x,3);
printf("%s" , y); // Result: WEL
}
5.strcmp() function:
This function is used to compare two strings. It return 0 if the strings are
sameotherwise it return non zero value.
Syntax: strcmp(string1,string2);
Example:
Strcmp(name1,name2);
#include<stdio.h>
#include<string.h>
void main()
char x[10]="WELCOME";
char y[10]="WELCOME";
int n;
if(n == 0 )
else
{
printf("Strings are different");
6.strlen() function:
Syntax: n=strlen(string);
Where n is an integer variable,which receives the value of the length of the string.
#include<stdio.h>
#include<string.h>
void main()
{
char x[10]="Welcome";
int n;
n=strlen(x);
printf("%d" , n); // Result: 7
}
3.4 Functions:
* It is also used other library function such as sqrt, cos, strcat, etc.,
Example :
main( )
int a=30,b=10,c;
c= a+b;
The program may become too large and complex and as a result the task of debugging,
testing and maintaining becomes difficult.
Function Calls :
A function can be called by simply using the function name by a list of actual parameters
or arguments.
Example :
main()
{
int y;
y = mul (10,5); function call
printf(“%d\n”,y);
}
A function may or not send back any value to the calling function. It is done throughthe
return statement.
return;
or return(expression);
In call by value, when you passed value to the function it is locally stored by the
function parameter in stack memory location.
If you change the value of function parameter, it is changed for the current function
only but it not change the value of variable inside the caller method such as main().
EXAMPLE PROGRAM:
#include<stdio.h>
#include<conio.h>
int temp;
temp=a;
a=b;
void main()
clrscr();
printf("\nValue of a: %d",a);
printf("\nValue of b: %d",b);
getch();
Output
Value of a: 200
Value of b: 100
2.Call by reference:
Here, address of the value is passed in the function, so actual and formal arguments
shares the same address space.
Hence, any value changed inside the function, is reflected inside as well as outside
the function.
EXAMPLE PROGRAM:
#include<stdio.h>
#include<conio.h>
int temp;
temp=*a;
*a=*b;
void main()
clrscr();
printf("\nValue of a: %d",a);
printf("\nValue of b: %d",b);
getch();
Output
Value of a: 200
Value of b: 100
Function Call:
A function cab be called by simply using the function name followed by a list of
actual parameters (or arguments), enclosed in parentheses.
Example:
main()
int a;
A=add(10,15);
/*Function call*/
printf(“%d\n”,a);
}
int add(int x,int y)
A function call is a postfix expression. The operator (..) is a very high level
of sprecedence.
In a function call, the function name is the operand and the parentheses set(..) which
contains the actual parameters is the operator.
The actual parameters must match the function formal parameters in type, order
andnumber. Multiple actual parameters are separated by commas.
Function prototype:
All functions in C program, must be declared, before they are invoked. A function
declaration (also known as function prototype) consists of four parts.
Function name
Parameter list
Terminating semicolon
Such declarations are available for all the functions in the program.
When we place it in a function definition (in the local declaration section), the
prototype is called a local prototype.
Elements of Function:
All the six elements are grouped into two parts, namely
Statement1;
Statement2;
………….
………..
…………….
return statement;
printf(“sum=%d”,a+b+c);
return;
}
b) void display(void)
}
Storage classes:
Storage Classes Storage class tells :
Scope is local to the block where it is declared Life is, with in the block in where it is
declared.
Automatic variable can be declared using the keyword auto. Eg: auto int a;
By default all variables are automatic int a; same as auto int a;
Example 1:
main()
{
auto int i=10;
printf(“%d”,i);
}
Output: 10
Example 2:
main()
{
auto int i;
printf(“%d”,i);
}
Output: 1002
Scope is local to the block where it is declared Life is, with in the block in where it is
declared.
Department of Computer Engineering
Aditya Polytechnic Colleges (255, 249, 404), Surampalem Page 14
LECTURE NOTES
EC-405 Programming in C
When a variable stored in register, it access high speed.
The no of registers are limited in a processor, if the registers are not free it will
convert to automatic variable.
The mostly length of the registers are 2 bytes, so we cannot store a value greater
than its size, if it so it will covert to automatic variable.
Scope local to the block. Life is, value of the variable persists between different function
calls.
This inside a function static variable retains its value during various calls.
Example
main()
void show();
show();
show();
void show()
printf(“%d”,a);
a++;
1. Output will be 10 11
Life is, with in the block in end of the program external variable can be declared
using the keyword extern.
extern is used to give a reference of a global variable that is visible to ALL the
program files.
Example
void main()
void show();
Output:
printf(“a= %d”,a);
show(); a= 10 10
void show()
printf(“%d”,a);
}
• The extern keyword is used before a variable to inform the compiler that the
variable is declared some where else.
Recursion :
* Recursion is a special case of this process, where a function calls itself. A very simple
example of recursion is present in the function.
* A called function in turn calls another function a process of ‘chaining’ occurs it used
factorial function.
Example :
#include<stdio.h>
• A variable that holds a memory address. This address is the location of another object in
the memory.
2) A NULL pointer.
(If p does not hold a valid value, it can crash the program)
– int *p
But make sure if the memory is deleted using delete or if original variable goes out
of scope.
Declaring pointer:
Data-type *name;
When you write int *, compiler assumes that any address that it holds points to an integer type.
m= &count;
Suppose, count uses memory Address 2000 to store its value 100. so,
m=&count means m has 2000 address.
count=100
2000
q= *m
Accessing the address of a variable can be done with the help of & operator
available in C. We have already seen the use of this address operator in the
scanf function.
The operator & immediately preceding a variable returns the address of the
variable associated with it.
Example: p=&a;
Accessing value of a variable through pointer can be done by using unary operator
*(asterisk), usually known as the indirection operator.
volume = 259;
p= &volume;
n=*p;
#include<stdio.h>
void main()
int x=10,y;
int *ptr;
ptr=&x;
y=*ptr;
printf("value of x is:%d",x);
*ptr=20;
printf("Now x=%d",x);
Pointer Arithmetic:
Pointer variables can be used in expressions. If prt1 and prt2 are declared
Department of Computer Engineering
Aditya Polytechnic Colleges (255, 249, 404), Surampalem Page 19
LECTURE NOTES
EC-405 Programming in C
andinitialized pointers, then the following statements are valid.
y=*ptr1 * *ptr2;
same as (*ptr1)*(*ptr2)
sum=sum+*ptr1;
*ptr2=*ptr2+40;
2. The & operator can be called as The * operator can be called as “value at
“address of”. address”.
4. The & operator can be used only The * operator can be used by means of
with a simple variable are an array symbolic names.
element.
a=&number; number=200;
ptr=&number;
n=*ptr;