Jump to content
xisto Community
Ahsaniqbalkmc

Understanding Errors Presented By Gcc

Recommended Posts

So while going a bit further ahead with learning C, I have finally created a program that is displaying errors while compiling. However, the problem is that I don't understand what (and where) these errors are and consequently I cannot correct them.. So I thought why not get help from folks at Xisto...

The compiler I am using is GCC on a Lubuntu VM.

So the little program I have written looks something like this...

#include <stdio.h>#define PI 22/7#define FRACTION 4.0f/3.0fint main(void) {  printf "Program to calculate the volume of a sphere from radius \n\n\n");  float radius, volume;  printf("Radius: ");  scanf("%f", &radius);  volume = FRACTION * PI * radius * radius * radius;  printf ("Volume = %.1f\n", volume);  return 0;}

And when I try to compile it by using GCC, I get the following message.. (filename is q2.c)

q2.c: In function ‘main’:q2.c:5:10: error: expected ‘;’ before string constantq2.c:5:74: error: expected statement before ‘)’ token

First of all I need to know how is the "WHERE" part of the error presented in the above lines. I am assuming that it is in the "5:10:" and "5:74:" part of the statement but I am unable to understand what does it mean..

Secondly I need to know the "WHAT" part. that is what exactly is the error. I have gone through this little program 3 times but I haven't find the error.

Share this post


Link to post
Share on other sites

Try printf ("Program

Seems that that you forgot the leading parenthesis. The compiler could not imagine that you did such a mistake, and tries to imagine something more complicated.

Share this post


Link to post
Share on other sites

Oh..... how could I not see that. It so very basic..... Sometimes you are thinking that something hugely bad has happened and you focus your mind on advanced stuff (I am still thinking what could be advanced in this little program :unsure: ) and originally the fault is very basic one.... Perfect example is the above one.Availing the opportunity here, why don't you tell me exactly what the error reporting by GCC said. And was there any reference to where the error exists.. ???

Share this post


Link to post
Share on other sites

Availing the opportunity here, why don't you tell me exactly what the error reporting by GCC said. And was there any reference to where the error exists.. ???

 

First of all, the error was false, and secondly the place of the errors could be also false.

The compiler did not see the end of your instruction and told you that a ";" was missing. This is the problem with this compiler, very obvious errors are not detected correctly.

Another exemple is a missing quote, or a missing bracket.

Usually compilers come with their documentation, so you should find the (book) file with the error codes and diagnostics for gcc for your version of linux compilers.

You know, these books are paid by you so you receive them when you buy a profession very expensive compiler like microsoft C compiler or IBM C compiler, you have to find them by yourself for the free compilers. :D

Share this post


Link to post
Share on other sites

@Yordan, Is it the right way of learning new programming language by copy & paste code from some examples without knowing the basics of it.Like what printf function or functions used in example would do? or Why we use #include #define or why we start our program with main(void) or without knowing the right syntax and use particular function or what are header files etc..

Edited by agyat (see edit history)

Share this post


Link to post
Share on other sites

A good Unix course stats with an explanation, what is a computer, what is a filesystem with it's inodes, what is a device, with the major and minor of the special character devices.

And then introduces the standard libraries for handling such devices, for instance the standard input/output library stdio.h

When you want to read something on the keyboard you to include these libraries inside your program, introducting the #include syntax.

And then showing the "hello, world" printf

For instance in The C Programming Language by Brian W. Kernighan and Dennis M. Ritchie :

 

 

 

#include <stdio.h>main(){    printf("hello, world\n");}
Of course, everybody knows the Ritchie manual, still a good reference. Please note that there is no return instruction in that program, useless here.

Share this post


Link to post
Share on other sites

First of all, the error was false, and secondly the place of the errors could be also false.

The compiler did not see the end of your instruction and told you that a ";" was missing. This is the problem with this compiler, very obvious errors are not detected correctly.

Another exemple is a missing quote, or a missing bracket.

Usually compilers come with their documentation, so you should find the (book) file with the error codes and diagnostics for gcc for your version of linux compilers.

You know, these books are paid by you so you receive them when you buy a profession very expensive compiler like microsoft C compiler or IBM C compiler, you have to find them by yourself for the free compilers. :D

 

I understand the error was false and that the compiler couldn't locate where exactly the error was so the place it mentioned might also be false. The thing I want to know is what place did it mention.

 

To put my question in another way:

Can you explain the following lines of code in simple english that a newbie like me can understand.

q2.c: In function ‘main’:q2.c:5:10: error: expected ‘;’ before string constantq2.c:5:74: error: expected statement before ‘)’ token
I am not interested in whether the compiler was able to correctly find the error and correctly mention it. I am just interested in knowing what does it say in the above few lines. If someone can translate the above code into simple english that I can understand, I am assuming that it would prove to be extremely helpful for me.

 

About the manuals: I really hate reading them. Instead I prefer searching online for the exact thing I need to know. But in this case, I think this you can provide me with exactly what I need as our discussion has progressed from the very beginning and you may have some idea of my level of understanding the code...

 

@Yordan, Is it the right way of learning new programming language by copy & paste code from some examples without knowing the basics of it.

 

Like what printf function or functions used in example would do? or Why we use #include #define or why we start our program with main(void) or without knowing the right syntax and use particular function or what are header files etc..

 

My opinion is a bit different. I think that the concept behind a code and the code itself are tightly inter-related to each other. Elaborating my previous statement, if you have written the code but you don't know the concept behind it, OR, if you know the concepts but you have never written code, you are not in good position to move forward. But at the same time, there are certain concepts that involve many things at a time, some of which are beyond the understanding of a beginner. It is so because the concept itself involves certain other concepts that need to be understood first.

 

For example a C program starts with:

 

# include <stdio.h>

 

As a beginner I just know that this statement is actually a directive that directs the preprocessor to include the standard input/output library into the program. Now there are many things about this statement that I do not understand yet.. For instance what is a library?, what is a standard input/output library?, Why is it necessary for C programs to include this library into the program (while in PHP there was nothing like this)? Why is there .h extension at the end?

 

So I have two options now. I can either go forward and try to answer all my questions that I have about my statement and not proceed further until I have answered them all. Secondly, I can just leave these questions for a later time when I have enough basic knowledge of the language and proceed with learning the basic stuff.

 

I think the second approach is much better because in that way I would proceed in a more systemic manner and gain concepts at their optimal time. You cannot just go on looking to gain all the concepts while not having written a single line of code. This won't help you much. Instead if you start with simple programs (even if you copy them from somewhere) and then see what each line does in the program by making changes to it, deleting it, or adding other lines, you would be heading a much simpler path..

A good Unix course stats with an explanation, what is a computer, what is a filesystem with it's inodes, what is a device, with the major and minor of the special character devices.

And then introduces the standard libraries for handling such devices, for instance the standard input/output library stdio.h

When you want to read something on the keyboard you to include these libraries inside your program, introducting the #include syntax.

And then showing the "hello, world" printf

 

I agree completely. But as I mentioned above, you cannot just go and grab all the concepts before doing any practical work. This would be just a waste of time. Instead you should start with simple concepts and simple practical work (even if you don't understand all that is in the little practical work you have done). At least you would get full understanding of some of it..;...

Share this post


Link to post
Share on other sites

As a beginner I just know that this statement is actually a directive that directs the preprocessor to include the standard input/output library into the program. Now there are many things about this statement that I do not understand yet.. For instance what is a library?, what is a standard input/output library?, Why is it necessary for C programs to include this library into the program (while in PHP there was nothing like this)? Why is there .h extension at the end?

This is explained in the user manual and in the reference manual.Each statement (like printf) has a syntax and has a list of needed libraries.
For instance if you want to manipulate day-time things, you need times.h if not, you don't need them.
If you manipulate strings, you need the strings.h library. If not, you don't need it.

Instead you should start with simple concepts and simple practical work

This is another good approach. Simply, with this latter approach, you almost never know the fundamentals of what you are writing, you could survive very long time before needing to understand a concept which would have been told you immediately in the first approach.It's exactly like learning music, some teacher start the hard way, teaching you how to read the notes before having fun. Other teachers show you how to play funny music, and afterwards show you the books helping you to learn more indepth.

Share this post


Link to post
Share on other sites

It's exactly like learning music, some teacher start the hard way, teaching you how to read the notes before having fun. Other teachers show you how to play funny music, and afterwards show you the books helping you to learn more indepth.

I guess if you are not bound to learn something (for example you are not a student who has to learn a subject to pass and get his degree), and you are just learning something for fun, that FUN is the most important thing you would seek. For instance I am not bound to learn C. I am doing it just to fulfill my appetite for computers and programming, so the most important thing to keep me investing my time in it would be fun. And fun would be the thing I would seek from the beginning. So in this scenario, the approach with practical work from the beginning would be best as you would start having fun from the beginning.

Share this post


Link to post
Share on other sites

You're almost right, but only partly. I learned C at school, with the hard method, learning definitions before applying them. This is the safest way for people needing to learn a language.However, I understand the programming-by-example way, which could people feel it more attractive.

Share this post


Link to post
Share on other sites

However, I understand the programming-by-example way, which could people feel it more attractive.

And for people like me who don't have a forcing sword behind their necks, being attractive and entertaining is the most important factor in sticking to work with a new computer language....

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.