Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / NewStats: 3,150,437 members, 7,808,561 topics. Date: Thursday, 25 April 2024 at 01:28 PM |
Nairaland Forum / Science/Technology / Programming / Please,Help Explain This Code In C (1460 Views)
Learn How To Code In Ibadan / Please Help Me Solve This Problem In C++ / Can Someone Help Me Explain This Code Line? (2) (3) (4)
Please,Help Explain This Code In C by octaflav: 4:56pm On Mar 27, 2019 |
Good evening to you, please I need a line by line explanation on these lines of Code. Got it from you tube, but the explanation given was not clear. Thanks in advance Input file in C #include<stdio.h> #include<conio.h> void main() { int i,j=1,k=2,flag; for(i=0;<=4;i++) { k=i+j*k; } flag=55; if(flag==10) printf(“Hello All”); getch(); } |
Re: Please,Help Explain This Code In C by octaflav: 7:00pm On Mar 28, 2019 |
Please, I need help on the explanation of this code in C language |
Re: Please,Help Explain This Code In C by valzey(m): 8:18pm On Mar 28, 2019 |
From a functional perspective, the code does nothing. It does some processing on some variables (i, j, k) which it never uses. Now 'Hello All' will never be displayed because flag can never be 10. Finally getchar() just keeps the program on till you enter a character and press enter at which the program terminates. So it does nothing. 1 Like |
Re: Please,Help Explain This Code In C by airsaylongcon: 9:42am On Mar 29, 2019 |
I disagree with the post above that it does nothing. The fact that it doesn't display any output given the state of the variables doesn't mean it does nothing. The code assigns a new value to variable k, checks if the flag variable is equal to 10 (and since it doesn't, nothing is displayed) and waits for input from user to terminate the program execution 1 Like |
Re: Please,Help Explain This Code In C by octaflav: 12:44pm On Mar 29, 2019 |
valzey: Ok, I appreciate your opinion. I actually got the code from you tube. I am to do a presentation on the implementation of a Lexical Analyzer in C Language so the code is meant to be my working code. But I couldn't really decode what's all about. Thanks for your insight. |
Re: Please,Help Explain This Code In C by octaflav: 12:56pm On Mar 29, 2019 |
airsaylongcon: Ok. Another good insight. So, what name can you give the above program? Or do you have any small program in C that could be used to show how a Lexical Analyzer works. That will be fine |
Re: Please,Help Explain This Code In C by airsaylongcon: 1:29pm On Mar 29, 2019 |
Of a quick Google search this is what I got. And it's a bit intuitive to analeand understand it. 60 #include<stdio.h> #include<stdlib.h> #include<string.h> #include<ctype.h> int isKeyword(char buffer[]){ char keywords[32][10] ={"auto","break","case","char","const","continue","default", "do","double","else","enum","extern","float","for","goto", "if","int","long","register","return","short","signed", "sizeof","static","struct","switch","typedef","union", "unsigned","void","volatile","while"}; int i, flag = 0; for(i = 0; i < 32; ++i){ if(strcmp(keywords[i], buffer) == 0){ flag = 1; break; } } return flag; } int main(){ char ch, buffer[15], operators[] = "+-*/%="; FILE *fp; int i,j=0; fp = fopen("program.txt","r" ; if(fp == NULL){ printf("error while opening the file\n" ; exit(0); } while((ch = fgetc(fp)) != EOF){ for(i = 0; i < 6; ++i){ if(ch == operators[i]) printf("%c is operator\n", ch) ; } if(isalnum(ch)){ buffer[j++] = ch; } else if((ch == ' ' || ch == '\n') && (j != 0)){ buffer[j] = '\0'; j = 0; if(isKeyword(buffer) == 1) printf("%s is keyword\n", buffer) ; else printf("%s is indentifier\n", buffer) ; } } fclose(fp) ; return 0; } |
Re: Please,Help Explain This Code In C by octaflav: 2:55pm On Mar 29, 2019 |
airsaylongcon:? |
Re: Please,Help Explain This Code In C by valzey(m): 4:54pm On Mar 29, 2019 |
octaflav:Don't be afraid. Its a simple program that: Opens a file called "program.txt" Goes through every string in the file: If it finds an operator (+,-,/,*) it prints its an operator. If it finds a keyword it prints it is a keyword If it is none it prints it is an identifier. |
Re: Please,Help Explain This Code In C by airsaylongcon: 5:28pm On Mar 29, 2019 |
octaflav: Some questions for you. 1) Can you program? Can you program in C (or C++) 2) Do you know what a lexical analyser does? 3) If you can program, do you actually have a C compiler and IDE installed on your system where you actively write, compile and debug programs? I haven't touched programming in a while but I can understand what the bit of code I shared with you is doing. It takes an external file called PROGRAM.TXT and analyses the content of the file to identify tokens within the file. The tokens are identified as either constants, keyword (C language keywords) or operators (such as +, -, ÷, x). Its a very straightforward program despite its seemingly complicated look |
Re: Please,Help Explain This Code In C by airsaylongcon: 5:31pm On Mar 29, 2019 |
valzey: Thank you my brother... I guess its the way NL has scattered the code that is scaring him. In a proper IDE it's very easy to "parse" with the human eye. PS: The keywords being identified are C language keywords which have been stored in the KEYWORDS array. |
Re: Please,Help Explain This Code In C by octaflav: 10:16pm On Mar 29, 2019 |
valzey: Thanks for your time in shedding more light. I am to explain or teach how a lexical analzer works in C. I can explain the concepts and its rudiments but I need a short program like [b]sum of the first fifty odd numbers [/b]to be used as my working example within the time allotted to me |
Re: Please,Help Explain This Code In C by octaflav: 10:25pm On Mar 29, 2019 |
airsaylongcon: I'm learning C++ and python. And yes I know how a lexical analyzer works because it's part of the contents in one of my courses Compiler Construction. Thanks for your input |
Re: Please,Help Explain This Code In C by valzey(m): 1:02am On Mar 30, 2019 |
octaflav: That program is good for the use case, just provide a valid simple C code in 'program.txt' and watch the results. Then modify as you see fit. Remember anything more than this is entering syntax analysis. I wonder if you've even run it yet. |
Re: Please,Help Explain This Code In C by asalimpo(m): 1:45am On Apr 01, 2019 |
octaflav:Are u writing a compiler? Or is it a final project ? |
Re: Please,Help Explain This Code In C by asalimpo(m): 1:54am On Apr 01, 2019 |
octaflav: Many open source programs show how [b]lexers [\b] work. I assume you mean parsing a program into tokens and executing based on the interpretation of the tokens. But simpler examples could have been used for the case study e.g a simple calculator. The parser (or lexers) would break the script into numbers and operators (functions). It's easier to digest than a full blown language |
(1) (Reply)
HNG Internship Or Google Africa Developer Scholarship? / Help, In Love With Programming But In Need Of A Laptop / Please Help! How Do I Open My Visual Basic Program In Visual Studio 2008?
(Go Up)
Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health religion celebs tv-movies music-radio literature webmasters programming techmarket Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10) Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 41 |