Pages
The greatest mistake you can make in life is to be continually fearing you will make one
Tuesday, 23 December 2014
Wednesday, 2 May 2012
Introduction to C++
What is c++
- C++ is a high level programming language that allows a software engineer to efficiently communicate with a computer.
- c++ is a highly flexible and adaptable language used for variety of programs including firmware for micro controllers,operating systems, applications and graphics programming.
- In 1970 , Brian Kernighan and Dennis Ritchie, created C language. C was designed for writing Operating systems.
- C language was extremely simple and flexible and soon It was became very popular programming language.
- C had one major problem, it was a procedure oriented language (ie) computer program consists of 2 main parts data and instruction. C program would start by describing the data first and then write procedures to manipulate that data.
- Programmers discovered that a program should be clear and easy if they were able to take a bunch of data and group it together with the operation that worked on that data. such a grouping is called an object or class.
- Designing programs by designing classes is known as object-oriented design.
- In 1980,Bjarne Stroustrup worked on a new language called "C with classes". That language was improved and became c++.
Wednesday, 15 June 2011
C program for Evaluating a Postfix Expression
The Algorithm for Evaluating a Postfix Expression is given here
Program:
Program:
#include<stdio.h>
#include<ctype.h>
#include<stdlib.h>
#define SIZE 40
int stack[SIZE];
int top=-1;
void push(int n)
{
if(top==SIZE-1)
{
printf("Stack is full\n");
return;
}
else
{
top=top+1;
stack[top]=n;
printf("Pushed element is %d\n",n);
}
}
int pop()
{
int n;
if(top==-1)
{
printf("Stack is empty\n");
return;
}
else
{
n=stack[top];
top=top-1;
printf("The poped element is %d\n",n);
return(n);
}
}
int evaluate(int op1, int op2,char ch)
{
printf("op1=%d op2=%d ch=%c\n",op1,op2,ch);
int n;
if (op1<op2)
{
n=op1;
op1=op2;
op2=n;
}
if(ch=='+')
n=op1+op2;
else if(ch=='-')
n=op1-op2;
else if(ch=='*')
n=op1*op2;
else if(ch=='/')
n=op1/op2;
else if(ch=='%')
n=op1%op2;
else
{
printf("The operator is not identified\n");
exit(0);
}
printf("n=%d\n",n);
return(n);
}
int main()
{
char str[50],ch,ch1;
int i=0,n,op1,op2;
printf("Enter the Postfix string\n");
scanf("%s",str);
ch=str[i];
while(ch!='\0')
{
printf("The char is=%c\n",ch);
//if(ch=='1' || ch=='2' || ch=='3' || ch=='4' || ch=='5')//
if(isdigit(ch))
{
n=ch-'0';
push(n);
}
else
{
op1=pop();
op2=pop();
n=evaluate(op1,op2,ch);
push(n);
}
ch=str[++i];
}
printf("The value of the arithmetic expression is=%d\n",pop());
return;
}Wednesday, 16 March 2011
C program for converting Infix expression to postfix expression
The Algorithm for converting Infix expression to postfix expression is given here
Program:
Output1:
Enter the infix string
((a+b)*c-(d-e))%(f+g)
Pushed element is (
Pushed element is (
Pushed element is +
poped element is+
poped element is(
Pushed element is *
poped element is*
Pushed element is -
Pushed element is (
Pushed element is -
poped element is-
poped element is(
poped element is-
poped element is(
Pushed element is %
Pushed element is (
Pushed element is +
poped element is+
poped element is(
poped element is%
Postfix string=ab+c*de--fg+%
Output 2:
Enter the infix string
(5*(((9+8)*(4*6))+7))
Pushed element is (
Pushed element is *
Pushed element is (
Pushed element is (
Pushed element is (
Pushed element is +
poped element is+
poped element is(
Pushed element is *
Pushed element is (
Pushed element is *
poped element is*
poped element is(
poped element is*
poped element is(
Pushed element is +
poped element is+
poped element is(
poped element is*
poped element is(
Postfix string=598+46**7+*
Program:
#include<stdio.h>
#define SIZE 40
char stack[SIZE];
int top=-1;
void push(char data)
{
if(top==SIZE-1)
{
printf("Stack is full\n");
return;
}
else
{
top=top+1;
stack[top]=data;
printf("Pushed element is %c\n",data);
}
}
char pop()
{
char ch;
if(top<0)
{
printf("stack is empty\n");
return;
}
else
{
ch=stack[top];
printf("poped element is%c\n",ch);
top=top-1;
return(ch);
}
}
int check_pre(char a ,char b)
{
//operators are arranged in the array based
//on their priority. from low to high
char op[]={'-','+','%','/','*','(',')'};
int i,c1=0,c2=0;
for(i=0;i<7;i++)
{
if(a==op[i])
c1=i+1;
else if(b==op[i])
c2=i+1;
}
if(c1>c2)
return(1);
else if(c1<c2)
return(-1);
else
return(0);
}
int main()
{
char in_str[50],out_str[50];
char ch,temp;
int x=0,y=0,pre;
printf("Enter the infix string\n");
scanf("%s",in_str);
ch=in_str[x];
while(ch!='\0')
{
//for operand
if((ch>='a' && ch<='z') ||
(ch<='A' && ch>='Z') ||
(ch>='0' && ch<='9'))
out_str[y++]=ch;
//for '(' paranthesis
else if(ch=='(')
push(ch);
//for ')' parenthesis
else if(ch==')')
{
temp=pop();
while(temp!='(')
{
out_str[y++]=temp;
temp=pop();
}
// if(temp=='(')
// pop();
}
//for operator
else
{
//if the stack is empty or
// the stack top element is '('
//just push the operator in to the stack
if (top==-1 || stack[top]=='(')
push(ch);
else
{
temp=stack[top];
//check the precedence
pre=check_pre(ch,temp);
if(pre<0 )
{
do{
out_str[y++]=pop();
temp=stack[top];
}while(top!=-1 && temp!='(' && (check_pre(ch,temp)<0));
push(ch);
}
else
{
push(ch);
}
}
}
x++;
ch=in_str[x];
}
while(top!=-1)
{
out_str[y++]=pop();
}
out_str[y]='\0';
printf("Postfix string=%s\n",out_str);
return;
}Output1:
Enter the infix string
((a+b)*c-(d-e))%(f+g)
Pushed element is (
Pushed element is (
Pushed element is +
poped element is+
poped element is(
Pushed element is *
poped element is*
Pushed element is -
Pushed element is (
Pushed element is -
poped element is-
poped element is(
poped element is-
poped element is(
Pushed element is %
Pushed element is (
Pushed element is +
poped element is+
poped element is(
poped element is%
Postfix string=ab+c*de--fg+%
Output 2:
Enter the infix string
(5*(((9+8)*(4*6))+7))
Pushed element is (
Pushed element is *
Pushed element is (
Pushed element is (
Pushed element is (
Pushed element is +
poped element is+
poped element is(
Pushed element is *
Pushed element is (
Pushed element is *
poped element is*
poped element is(
poped element is*
poped element is(
Pushed element is +
poped element is+
poped element is(
poped element is*
poped element is(
Postfix string=598+46**7+*
Wednesday, 26 January 2011
Daily 5 C programs-2
- Write a program to print the number of characters entered?Count the Input characters
solution:
Output:#include<stdio.h> int main() { int x=0; while(getchar()!=EOF) ++x; printf("\nNumber of characters X=%d\n",x); return 0; }
hello world
Number of characters entered is X=11
- Write a program to count the number of words entered
Solution:
Output1:#include<stdio.h> int main() { int x=0,ch; while((ch=getchar())!=EOF ) { if(ch && x==0)//for the i/p xxxEOF x=1; if(ch=='\n' || ch==' ' || ch=='\t') ++x; } printf("\nNumber of words entered is X=%d\n",x); return 0; }
zccc
Number of words entered is X=1
Output2:
xcvvx cxcbsf
bbb
Number of words entered is X=3 - Write a program to print all input lines that are longer than 80 characters
- Write a program to remove all trailing blanks and tabs from each line of input and to delete entirely blank lines.
- Write a function reverse(s) that reverses the character string s
Wednesday, 19 January 2011
EOF[End Of File]
- End of file[EOF] is a condition in computer operating system where no more data can be read from a data source(file or stream).
- EOF is actually a macro defined as an integer
with a negative value(normally -1). - EOF is normally returned by functions that perform read operations to denote either an error or end of input. It is important to ensure you use an integer to store the return code from these functions, even if the function appears to be returning a char, such as
getchar()orfgetc(). - We cannot stop the program by directly giving -1. when you write '-1', you do not write the VALUE -1, but the character string { '-', '1' } your program will not stop, except if you type ctrl+D under linux, or ctrl+Z+enter under windows.
- Not a character
- A value that exists at the end of a file
- A value that could exist at the middle of a file
Example Program to find the EOF value:
/*************Program1***********/
#include<stdio.h>
int main()
{
int ch=getchar();
printf("%d",ch);
return;
}
---------------------------------------
/*************Program2***********/
#include<stdio.h>
int main()
{
printf("%d",EOF);
return;
}In Windows if you enter the keys Control + Z you get EOF, in Linux it is Control + D
Output: -1
Daily 5 C programs-1
- Write a program to print the value of
EOF.?
Solution:/*************Program1***********/ #include<stdio.h> int main() { int ch=getchar(); printf("%d",ch); return; } --------------------------------------- /*************Program2***********/ #include<stdio.h> int main() { printf("%d",EOF); return; }
In Windows if you enter the keys Control + Z you get EOF, in Linux it is Control + D
Output: -1 - Write a program to read input from user and store it in a string until EOF(ctrl+d pressed in linux)
Solution1
Solution2#include<stdio.h> int main() { char str[100]; printf("Enter the string with maximum 100 characters\n"); scanf("%[^EOF]",str); printf("%s",str); return; }#include<stdio.h> int main() { char buf[512]; int ch; int i=0,tab=0,line=0,blank=0; printf("Enter the line of text. Press ctrl+d to finish\n"); while((ch=getchar())!=EOF) { buf[i++]=ch; } printf("The entered string is %s\n",buf); return; }
Output:
Enter the string with maximum 100 characters
Hello world
Welcome to my blog
Hello world
Welcome to my blog
Write a Program to count blanks,tabs,newline?
Solution:
Output:/*Program to read input from user and store it in a string until EOF(ctrl+d pressed)*/ #include<stdio.h> int main() { char buf[512]; int ch; int i=0,tab=0,line=0,blank=0; printf("Enter the line of text. Press ctrl+d to finish\n"); while((ch=getchar())!=EOF) { buf[i++]=ch; if(ch=='\t') tab++; else if(ch=='\n') line++; else if(ch==' ') blank++; } printf("The entered string is %s\n",buf); printf("No of blanks=%d\n",blank); printf("No of tab=%d\n",tab); printf("No of Lines=%d\n",line); return; }
Enter the line of text.Press ctrl+d to finish
Hello world Welcome
How are you?
Did you Like my Blog
The entered string is Hello world Welcome
How are you?
Did you Like my Blog
No of blanks=7
No of tab=2
No of Lines=3
- Write a program to copy its input to its output, replacing each string of one or more blanks by a single blank?
Solution:
Output:#include<stdio.h> int main() { char txt[100],out[100],ch; int i=0,j=0; printf("Enter the string\n"); scanf("%[^EOF]",txt); while(txt[i+1]!=EOF) { ch=txt[i]; if(ch!=' ' || (ch==' ' && txt[i+1]!=' ')) out[j++]=ch; i++; } printf("\nOutput String=%s\n",out); return; }
./a.out
Enter the string
a s d a
Output String=a s d a
- Write a program to copy its input to its output, replacing each tab by
\t, each backspace by\b, and each backslash by\\. This makes tabs and backspaces visible in an unambiguous way.
Solution:
Output:#include<stdio.h> int main() { char buf[512]; int ch,i=0; printf("Enter the line of text. Press ctrl+d to finish\n"); while((ch=getchar())!=EOF) { if(ch=='\t' || ch=='\n' || ch==' ') buf[i++]='\\';/*to print \*/ if(ch=='\t') buf[i++]='t'; else if(ch=='\n') buf[i++]='n'; else if(ch==' ') buf[i++]='b'; else/*for all other char*/ buf[i++]=ch; } buf[i++]='\0'; printf("The entered string is %s\n",buf); return; }
Enter the line of text.Press ctrl+d to finish
Hello World Welcome
C Programming
The entered string is Hello\bWorld\t\tWelcome\b\nC\b\bProgramming\n
- Write a program that prints its input one word per line?
Solution1:
Output:#include<stdio.h> int main() { char txt[100],out[100],ch; int i=0,j=0; printf("Enter the string\n"); scanf("%[^EOF]",txt); while(txt[i]!=EOF) { ch=txt[i]; if(ch==' ') out[j++]='\n'; else out[j++]=ch; i++; } printf("\nOutput String=%s\n",out); return; }
Enter the string
hello World Welcome
Output String=hello
World
Welcome
Tuesday, 18 January 2011
Some questions about Arrays with answers
- How will you define an array?
Example:Storage class datatype array_name[size of array] - static int sum[8];
- auto float fun[20]; (or) float fun[20];
- Is it necessary to specify the storage class in array definition? what is the default storage class for arrays?
Answer:
Storage class is optional. If the array is defined with in a function or a block the default storage class is auto. If the array is defined outside of function then the default storage class is extern. - If the array elements are not initialized then what is the default initial value?for example: int x[5]; what is the value of x[1]?
Answer:
If the array is not initialized then the array has garbage value. If the array is a external or global array then the default initial value is 0
Example:
#include<stdio.h>
int main()
{
int a[5];
printf("%d",a[2]);
return;
}
Output:
./a.out 195392
#include<stdio.h> int e[3]; int main() { int i,a[3]; for(i=0;i<3;i++) { printf("a[%d]=%d\t",i,a[i]); printf("e[%d]=%d\n",i,e[i]); } return; } output a[0]=-1530409104 e[0]=0 a[1]=32767 e[1]=0 a[2]=0 e[2]=0
- Consider we have a 10 element array what happen if we try to access the 12th element?
There is no bound checking in array. So if we try to access the array element out of its size then it will return garbage value.
#include<stdio.h> int e[10]; int main() { int i,a[10]; for(i=10;i<15;i++) { printf("a[%d]=%d\t",i,a[i]); printf("e[%d]=%d\n",i,e[i]); } return; } output ./a.out a[10]=0 e[10]=0 a[11]=11 e[11]=0 a[12]=0 e[12]=0 a[13]=0 e[13]=0 a[14]=366693453 e[14]=0
- Can I declare the array without specifying the size? int a[]; is it allowed?
Answer:
No its not allowed.
error: array size missing in ‘a’ - int a[]={1,2,3,4,5}; Is it allowed?
Answer:Yes. The number of array element is optional when initial values are present
- Consider int a[5]={10,20,30,40,50}; printf("a=%d",*a); what is the output?
#include<stdio.h> int main() { int a[5]={10,20,30,40,50}; printf("a=%d",*a); return; }
Answer:
a=10 - Consider int a[5]={10,20,30,40,50}; printf("a=%d",a); what is the output?
We cannot print the entire array elements using only the array name#include<stdio.h>
int main()
{
int a[5]={10,20,30,40,50};
printf("a=%d",a);
return;
}
Answer:
warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘int *’
sunitha@sunitha-laptop:~/cpgm$ ./a.out
a=425449152 - The last character in a array must be a NULL(\0) character what happen if I missed the '\0' character? char vowels[6]={'a','e','i','o','u'}; Is it correct?
int main()
{
char vowels[6]={ 'a','e','i','o','u'};
printf("char=%c",vowels[5]);
return;
}
Output
./a.out
char= - What happen If I am not giving space for '\0' character? char flag[4]={'T','R','U','E'}; is it correct?
Answer
This type of declaration is incorrect but the compiler wont show any error. but flag[5]={'T','R','U','E'}; is the correct form (or) flag[]={'T','R','U','E'}; - What happen If I am not giving space for '\0' character in string? char str[5]="Welco"; is it correct?
Answer
Some unwanted character will be printed at the end of the string.
#include<stdio.h>
int main()
{
char str[5]="Welco";
printf("string=%s to india",str); return;
}
output
./a.out
string=Welco to india - x is a array pointer. what is the value for *x++?
#include<stdio.h>
int main()
{ int x[5]={10,20,30,40,50};
printf("ans=%d",*x++);
return;
}
output
error: lvalue required as increment operand - Is there any way to find the size of the array?
We can find the size of the array using sizeof operator.#include<stdio.h>int main() { int x[15]; printf("ans=%d",sizeof(x)); return; } Output: ./a.out ans=60
#include<stdio.h> int main() { char x[]={'a','e','i','o','u','\0'}; printf("ans=%d",sizeof(x)); return; } Output: ./a.out ans=6 - Can I print the characters given in single quotes as string?
#include<stdio.h> int main() { char x[]={'a','e','i','o','u','\0'}; printf("ans=%s",x); return; } Output: ./a.out ans=aeiou - What is the output for the following program?
#include<stdio.h> int main() { char x[]={'a','e','i','o','u'}; printf("ans=%s",x); return; }
Answer:
ans=aeiou
Some unwanted character is printed at the end of the string - What is the output for the following program?
./a.out#includeint main(){ int x[5]={10,20,30,40,50};printf("ans=%d",(*x)++);return;} Output:
ans=10 - What is the output for the following program?
#includeint main(){int x[5]={10,20,30,40,50};printf("ans=%d",++*x);return;} Output./a.outans=11 - Do we have any minimum or maximum size restrictions in array index?
- What is the maximum index(sixe) number an array can have?
The maximum size of an indexed array is 232 - 1 or 4,294,967,295. An attempt to create an array that is larger than the maximum size results in a run-time error. - Why array index is starting from zero? (or) Why arrays not store values from 1?
a[n] has to implemented internally as *(a+n)so it is easy if it starts from 0; otherwise it has to be *(a+n-1) - Can I declare an array with 0 size? int a[0]; is it allowed?
#include<stdio.h> int main() { int x[0]; *x=20; printf("ans=%d",x[0]); return; } Output ./a.out ans=20 - Can I declare an array with negative size? int a[-5]; is it allowed?
#include<stdio.h> int main() { int x[-5]; *x=20; printf("ans=%d",x[0]); return; } Output compilation error: size of array ‘x’ is negative - Can I compare 2 arrays with single operator?
Single operations which involve entire arrays are not permitted in c. Thus if a and b are similar arrays (ie same datatype, same dimension and same size) assignment operations, comparison operations must be carried out on element by element basis. - Can I have comma(,) alone in the array list? int x[5]={,,,,}; Is it allowed?
error: expected expression before ‘,’ token - What happen if the array size is less than the number of initialized values?int a[5]={1,2,3,4,5,6,7,8,9,}; is it allowed?
Answer
It will give warning as "excess elements in array initializer" but it will produce the correct output for the array size and the remaining elements are garbage value. So the total output is incorrect#include<stdio.h> int main() { int i; char x[3]={'a','e','i','o','u','\0'}; printf("String=%s\n",x); return; } Output ./a.out String=aeiQ�
#include<stdio.h> int main() { int i,a[3]={1,2,3,4,5,6,7,8,9}; for(i=0;i<9;i++) printf("a[%d]=%d\n",i,a[i]); return; } output ./a.out a[0]=1 a[1]=2 a[2]=3 a[3]=3 a[4]=0 a[5]=0 a[6]=-1332024243 a[7]=32532 a[8]=0 - How to pass and access the array values to the function using call by value method?
- How to pass and access the array values to the function using call by reference method?
- Array name is a pointer. If an array is passed to a function and several of its elements are altered with in the function, are these changes recognized in the calling portion of the program?
Answer:If the array element is altered within the function, the alteration will be recognized in the calling portion of the program.
output#include<stdio.h> void fun(char x[],int ); int main() { char x[]={'a','e','i','o','u','\0'}; printf("before=%s\n",x); fun(x,5); printf("after=%s",x); return; } void fun(char x[],int n) { int i; for(i=0;i<n;i++) { x[i]=x[i]+i; } }
./a.out
before=aeiou
after=afkry
- Can an array can be passed from a function to the calling portion of the program via return statement?
Sunday, 19 December 2010
Multifile Program in C
1.What is a Multifile program?
- If a program contains many functions then it is difficult to read and maintain because when the number of functions increases the size of the program/file will also increase. To avoid this some functions can be written in separate file
- The individual files will be compiled separetely and then linked together to form one executable object program.
- This will help to editing and debugging because each file can be maintained at a manageable size.
2.How functions are defined in Multifile Program?
- Within a multi file program a function definition may be either external (or) static
- An external function will be recognized throughout the entire program(ie all the files)
- Static function will be recognized only with in the file in which it is defined.
- The default storage class for the function is external
- Syntax for Function definition
Storage class datatype name(type1 arg1......typen,argn) { } - Example:
extern int calculate(int x) { }
3.How functions are declared in Multifile Program?
- When a function is defined in one file and accessed in another one file, the 2nd file must include a function declaration.
- This declaration identifies the function as an external function whose definition appears else where.
- The function declarations are usually placed at the beginning of the file before any function definition.
- Syntax for external function declaration is same as function definition with additional one semicolon at the end
Storage class datatype name(type1 arg1...typen,argn); - Example:
extern int calculate(int x); - To execute a multifile program, each individual file must be compiled separetely then the main program file is compiled and executed.
4.A simple Multifile program in c
/****************File1*********/
#include<stdio.h>
//Include file test.c
#include "test.c"
//function declaration
extern void result(void);
int main()
{
printf("In file1 main function\n");
result();
return;
}/*****************File2*********/ extern void result(void) { printf("Welcome to file2\n"); return; }output:
./a.out
In file1 main function
Welcome to file2
5.Accessing external variables in Multifile function:
- Within a multifile program, external variables defined in one file and accessed in another file.
- An external variable definition can appear in only one file.
- Its location within the file must be external to any function definition. Usually it will appear at the beginning of the file.
- External variable may include initial values.
- If no initial value is assigned then it will automatically initialized to zero.
- To access an external variable in another file,the variable must first be declared within that file .This declaration may appear anywhere within the file usually at the beginning of the file.
- The declaration must begin with the storage class specifier extern. Initial values cannot be included in external variable declaration
- Example:
/*********file1**********/ #include<stdio.h> //Include file test.c #include "test.c" //External variable definition int x=1,y=2,z=3; //function declaration extern void result(void); int main() { printf("In file1 main function\n"); x=y+z; printf("x=%d y=%d z=%d\n",x,y,z); result(); return; }/*****************File2*********/ extern int x,y,z;/*external var declaration*/ extern void result(void) { printf("Welcome to file2\n"); printf("x=%d y=%d z=%d\n",x,y,z); return; }
Output:./a.out
In file1 main function
x=5 y=2 z=3
Welcome to file2
x=5 y=2 z=3 - The value assigned to an external variable can be altered within any file in which the variable is recognized
- The changes will be recognized in all other files. Thus external variables provide a convenient means of transferring information between files.
- Example:
/*********file1**********/ #include<stdio.h> //Include file test.c #include "test.c" //External variable definition int x=10,y=20,z=30; //function declaration extern void result(void); int main() { printf("In file1 main function\n"); x=y+z; printf("x=%d y=%d z=%d\n",x,y,z); result(); return; }/*****************File2*********/ extern int x,y,z;/*external var declaration*/ extern void result(void) { printf("Welcome to file2\n"); y=y+10; z=z+10; printf("x=%d y=%d z=%d\n",x,y,z); return; }
Output:./a.out
In file1 main function
x=50 y=20 z=30
Welcome to file2
x=50 y=30 z=40
Sunday, 12 December 2010
Static Variables in C
- Static variable use the keyword static.
- In a single file program static variable is similar to auto variables.
- Like auto variables, static variables are also local to the block in which they are declared.It cannot be accessed outside of their defining function.
- The difference between auto and static variable is that static variables dont disappear when the function is no longer active. their values persist.
- If the control comes back to the same function again the static variables have the same values they had last time.
- Example
The output of the above program is ./a.out#include<stdio.h> void fun1(); int main() { fun1(); fun1(); fun1(); return; } void fun1() { int x=1; printf("X=%d\n",x); x=x+1; }
X=1
X=1
X=1
If the variable is static then
The output of the above program is ./a.out#include<stdio.h> void fun1(); int main() { fun1(); fun1(); fun1(); return; } void fun1() { static int x=1; printf("X=%d\n",x); x=x+1; }
X=1
X=2
X=3 - If the automatic or static variables having the same names as external variables then the local variables will take precedence over the external variable and the changes done to the local variable will not affect the external variable.
Output#include<stdio.h>void fun1(); int x=1,y=2,z=3; int main() { static float x=5.0; printf("float x=%f\n",x); x=x+1; printf("float x=%f\n",x); fun1(); printf("x=%d\n",x); //try to print external var printf("x=%f y=%d z=%d\n",x,y,z); return; } void fun1() { char z; z='A'; printf("int z=%d char z=%c\n",z,z); x=x+10; printf("x=%d\n",x); }
float x=5.000000
float x=6.000000
int z=65 char z=A
x=11
x=-1 //This will show an warning
x=6.000000 y=2 z=3
Here x,y,z are external integer variable. But x is redefined as a static floating point variable within main. so in main the external variable x will not be recognized. In function fun1() ,z is redefined as a char variable so the external variable z will not be recognized in fun1().
Example 2
Output:#include< stdio.h>int x=1; int main() { static int x=5; printf("x=%d\n",x); x=x+1; printf("x=%d\n",x); return; }
x=5
x=6
Initializing Static Variable:
- Initial values can be included in the static variable declaration.
- Example: static int x=10;
- The initial values must be constants not expressions.
Example#include<stdio.h> int main() { static int x=5,y=10; static int a=x; static int z=x+y; printf("a=%d\n",a); printf("z=%d\n",z); return; }
While compiling it shows error as " initializer is not a constant"
- The initial values are assigned at the beginning of program execution. The variables retain these values throughout the life of the program unless different values are assigned.
- The default initial value is zero.
Thursday, 9 December 2010
Some Questions about Arrays
- How will you define an array?
- Is it necessary to specify the storage class in array definition? what is the default storage class for arrays?
- If the array elements are not initialized then what is the default initial value?for example: int x[5]; what is the value of x[1]?
- Consider we have a 10 element array what happen if we try to access the 12th element?
- Can I declare the array without specifying the size? int a[]; is it allowed?
- int a[]={1,2,3,4,5}; Is it allowed?
- Consider int a[5]={1,2,3,4,5}; printf("a=%d",*a); what is the output?
- Consider int a[5]={10,20,30,40,50}; printf("a=%d",a); what is the output?
- The last character in a array must be a NULL(\0) character what happen if I missed the '\0' character? char vowels[6]={'a','e','i','o','u'}; Is it correct?
- What happen If I am not giving space for '\0' character? char flag[4]={'T','R','U','E'}; is it correct?
- What happen If I am not giving space for '\0' character in string? char str[5]="Welco"; is it correct?
- x is a array pointer. what is the value for *x++;
- Is there any way to find the size of the array?
- Can I print the characters given in single quotes as string?
- What is the output for the following program?
#include<stdio.h> int main() { char x[]={'a','e','i','o','u'}; printf("ans=%s",x); return; } - What is the output for the following program?
#include <stdio.h>int main(){ int x[5]={10,20,30,40,50};printf("ans=%d",(*x)++);return;} - What is the output for the following program?
#include <stdio.h>int main(){int x[5]={10,20,30,40,50};printf("ans=%d",++*x);return;} - Do we have any minimum or maximum size restrictions in array index?
- What is the maximum index(sixe) number an array can have?
- Why array index is starting from zero? (or) Why arrays not store values from 1?
- Can I declare an array with 0 size? int a[0]; is it allowed?
- Can I declare an array with negative size? int a[-5]; is it allowed?
- Can I compare 2 arrays with single operator?
- Can I have comma(,) alone in the array list? int x[5]={,,,,}; Is it allowed?
- What happen if the array size is less than the number of initialized values?int a[5]={1,2,3,4,5,6,7,8,9,}; is it allowed?
- How to pass and access the array values to the function using call by value method?
- How to pass and access the array values to the function using call by reference method?
- Array name is a pointer. If an array is passed to a function and several of its elements are altered with in the function, are these changes recognized in the calling portion of the program?
- Can an array can be passed from a function to the calling portion of the program via return statement?
- Is there is any difference in storing 1 dimensional array and multi dimensional array?
- How the elements of a 2 dimensional array is stored? row major form (or) column major form?
- what is the size of a 2 dimensional array, 3 dimensional array?
- In 2 dimensional array number of rows specified first or number of columns specified first?
- Is it necessary to specify both number of rows and number of columns in 2 dimensional array?
- Can I leave the number of columns in 2 dimensional array? int a[5][]; is it allowed?
- Can I initialize a[][]={1,2,3,4,5,6,7,8,9}; (or) a[5][]={1,2,3,4,5,6,7,8,9};
Thursday, 2 December 2010
Storage Classes in C(Extern)
- The scope of a external variables remains from the point of the definition through the remainder of the program.
- External variables are recognized globally, that can be accessed from any function
- Using external variables we can transfer information in to a function without using arguments.
- The key word for external variable is extern.
- External variable definition is written in the same manner as an ordinary variable declaration.
- It must appear outside of ,and usually before the functions that access the external variables
- Example:
int x; //External variable definitions
fun1(){}
fun2(){} - An external variable definition will automatically allocate the required storage space for the external variables with in the computer memory.
- The assignment of initial values can be included.
Example:int x=10; //initializing external variable in definition
fun1(){}
fun2(){} - The keyword extern is not required in external variable definition.
- If a function requires an external variable, that has been defined earlier in the program, then the function may access the external variable freely.
Example:#include<stdio.h>
int x=10;
fun1
{
}
fun2
{
int a,b=10;
a=b+x; //no need to declare x here
}
- If the program is a multi file program then the external variable must be declared where ever its needed.
- An external variable declaration begins with the keyword extern.
- The name and the datatype of the external variable must agree with the corresponding external variable definition that appear outside of the function.
- Example:
file1 #include<stdio.h>
int x=10; //external var defined here
fun1()
{
}
fun2()
{
}
In file 2,x should be of type integer because in file 1, x is defined as integer.file2 #include<stdio.h>
fun3()
{
extern int x; //declaration
} - The storage space for external variables will not be allocated because the value is already defined.
- External variable declaration cannot include the assignment of initial value.
Initialization of External variable:
- External variables can be assigned initial value during variable definition.
- The initial values will be assigned only once, at the beginning of the program.
- The initial values must be expressed as constants not expressions or statements
Example:
while compiling this program It shows an error "error: initializer element is not constant"#include<stdio.h>
#define z 10
/*definition of external variable x and Y
int x=z;
int y=z+x; //error
fun1()
{
}
fun2()
{
int a,b=10;
a=b+x;
printf("x=%d y=%d\n",x,y);
}
int main()
{
fun1();
fun2();
return;
}
- The external variables will then retain these values, unless they are altered during the execution of the program.
- If the value of the external variable is changed with in a function, then the changed value will be carried over in to other parts of the program.
Example
The output of the above program is#include<stdio.h>
#define z 10
int x=z;
fun1()
{
x=30;
printf("In fun1, x=%d \n",x);
}
fun2()
{
x=20;
printf("In fun2, x=%d \n",x);
}
int main()
{
printf("In main, x=%d \n",x);
fun1();
fun2();
return;
}
In main, x=10
In fun1, x=30
In fun2, x=20 - If the initial value is not assigned , the variable will automatically be assigned a value of zero.
Example#include<stdio.h>
int x; //Not initialized
fun1()
{
x=30;
printf("In fun1, x=%d \n",x);
}
fun2()
{
x=20;
printf("In fun2, x=%d \n",x);
}
int main()
{
printf("In main, x=%d \n",x);
fun1();
fun2();
return;
}
The output of the above program is
In main, x=0
In fun1, x=30
In fun2, x=20
Subscribe to:
Posts (Atom)