#include"stdio.h"
#include"conio.h"
#include"string.h"
void main(){
char str[20],key[][32]= {"auto","double","int","struct",
"break","else","long","switch","case","enum","register",
"typedef","char","extern","return","union","const","float",
"short","unsigned","continue","for","signed","void","default",
"goto","sizeof","volatile","do","if","static","while" } ;
int i,j=0,flag;
clrscr();
while(j<=6){
flag=0;
printf("\nEnter string : ");
gets(str);
for(i=0;i<=31;i++){
if(flag==strcmpi(str,key[i]))
flag = 1;
}
if(flag==0){
for(i=0;i<strlen(str);i++){
if(str[i]=='_' || isalpha(str[i]) || isdigit(str[i]) && !isspace(str[i]) && !isdigit(str[0]))
flag = 0;
else{
flag = 1;
break;
}
}
}
if(flag==0)
printf("Valid Identifier\n");
else
printf("Invalid Identifier\n");
j++;
}
getch();
}---------------------------------------------------------------------------------------------------
In this Program one array is defined to store keyword to check input is keyword or not using 'strcmpi()' function. In other loop it checks for '_'(checks all 'str[i]') , alphabets(using 'isalpha()') and whitespace (using 'issapce()') constrains and if alphabet(not) or space(detected) then it means input might be containt ant numeric value or special symbol which will go to else part and break out of loop for preventing resetting flag variable.
Showing posts with label c++. Show all posts
Showing posts with label c++. Show all posts
Wednesday, July 13, 2016
Thursday, January 21, 2016
Calling main() from main() in c/c++
Many people think that when the main() function call inside the main(), recursion goes on infinite times. But it is not true as such so is it possible to call main() from main inside.?
Yes, we can call the main() within the main() function. The process of calling a function by the function itself is known as Recursion. Well, you can call a main() within the main() function .
Yes, we can call the main() within the main() function. The process of calling a function by the function itself is known as Recursion. Well, you can call a main() within the main() function .
The function calls take place until the stack overflow occurs, that is until the stack is completely filled with the functions. This implies that the recursion without a suitable condition takes place not infinite times but stack size times.So that particular experiment when conducted, compiles successfully but it suffers from a run time error or illegal instruction. So you should have a condition that does not call the main() function to terminate the program. Otherwise,the program will never return and run infinitely.
In first example global variable c=0 declared (bcz when main() called is should not reintialized) and after print statement first time it will print 1 (c+1)and condition will true so after calling main it will print 1 now condition will false so program will exit.
#include"stdio.h"
#include"conio.h"
int c=0;
void main(){
clrscr();
printf("Before main : %d",c);
c++;
if(c<2){
main();
exit(0);
}
getch();
}
--------------------------------------------------------------------------------------
int main() {
cout << "Hello World" << endl;
system("pause");//for pause program execution
return main();
}
Sunday, September 20, 2015
Unable to Open COS.OBJ File In C
Linker Error:Unable to open File COS.OBJ
This error is also due to the incorrect path of Library Directories. Default path for library is “C:/TC/LIB”. But if you are not installed Turbo C on another location or if you moved the installed TC folder to another, then you have two choices to correct the path. One is move your installed TC folder to you C drive without making a new folder. or change the path of library directory to path of your (installed) library folder (Example: D:/Software/Turbo C/ TC/LIB). Be ensure the LIB folder contains all library files like ‘COS.OBJ’, ‘COT.OBJ’ etc.For set path Follow Steps:
1.Open Turbo c.
2.GoTo Options.
3.Click on Directeries.
In Library Directories set the Default path'C:/TC/LIB' or your location(may be not same in all pc, so first you need to check) where LIB folder is located.
Subscribe to:
Posts
(
Atom
)