Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Explain me this program in C/C++

Status
Not open for further replies.

Help

Advanced Member level 2
Joined
Feb 15, 2005
Messages
617
Helped
7
Reputation
14
Reaction score
3
Trophy points
1,298
Activity points
7,065
Program in C/C++

Hai...

Anyone can help me to explain the program...

1) The basic mian function...

a) main()
{
}

b) void main()
{
}

c) void main(void)
{
}

d) int main()
{
}

2) what the different between this 3 function...

void DELAY (int);
void DELAY (unsigned int);
void DELAY (const unsigned int);

Thank You
 

Program in C/C++

I'm not quite sure what you are asking.

1. For ANSI standard C, those are all incorrect. Here are the two correct methods:

int main(void)
{
return 0;
}

int main(int argc, char *argv[])
{
return 0;
}

2. Those are not functions, they are function prototypes. The difference is each one passes a different type of argument to the function.

C and C++ are different languages. Say which one you mean. There is no such thing as C/C++.
 

Re: Program in C/C++

echo47 said:
1. For ANSI C, those are all incorrect. Here are the two correct methods:

int main(void)
{
return 0;
}

int main(int argc, char *argv[])
{
return 0;
}

2. Those are not functions, they are function prototypes. The difference is each one passes a different type of argument to the function.

Hai...

Thanks for your correction,
1) why some time in front of main function they put int izit in front of main has int so the main function must carry something (like int main(int argc, char *argv[])), what the different main() and void main(), can you explain more....

Thank You...
 

Re: Program in C/C++

The reason is quite subtle and is has to do with the way the compiler works. In C, after compiling the source file that has "main" in it, you get an object. But any information about the type of 'main' is gone in this object. That is, the linker (that must be called in order to get the final executable) sees only the symbol 'main' exported in the object, it doesn't have any information about the number and types of parameres and its return value. It also sees that 'main' is referenced from the startup file (this must be linked also in the final executable) and it just resolve the symbol, it doesn't care about its type. Things are a bit different in C++, but NOT for 'main'. In C++, 'main' is declared as "external C" by default, thus exhibiting the same behaviour as in C. The other functions (the ones that are not prefixed with "extern C") use a scheme called "name mangling" than encodes the type information with the function name to get a unique "function signature". This way, the type ambiguities are resolved. As stated before, the standard form for "main" is:

Code:
int main(int argc, char *argv[])
 

Program in C/C++

In C, main() has been obsolete for over 15 years. It's from the early days before function prototypes, before the ANSI standard.

void main() is simply wrong. If you see a book that teaches this mistake, please throw the book into the trash. You don't want to learn C from a sloppy teacher.

https://www.eskimo.com/~scs/C-faq/q11.15.html

That C FAQ is full of tasty tidbits.


Here is one section from the 1999 C standard:


5.1.2.2.1 Program startup

1 The function called at program startup is named main. The implementation declares no prototype for this function. It shall be defined with a return type of int and with no parameters:

int main(void) { /* ... */ }

or with two parameters (referred to here as argc and argv, though any names may be used, as they are local to the function in which they are declared):

int main(int argc, char *argv[]) { /* ... */ }

or equivalent; or in some other implementation-defined manner.

2 If they are declared, the parameters to the main function shall obey the following
constraints:

- The value of argc shall be nonnegative.

- argv[argc] shall be a null pointer.

- If the value of argc is greater than zero, the array members argv[0] through argv[argc-1] inclusive shall contain pointers to strings, which are given implementation-defined values by the host environment prior to program startup. The intent is to supply to the program information determined prior to program startup from elsewhere in the hosted environment. If the host environment is not capable of supplying strings with letters in both uppercase and lowercase, the implementation shall ensure that the strings are received in lowercase.

- If the value of argc is greater than zero, the string pointed to by argv[0] represents the program name; argv[0][0] shall be the null character if the program name is not available from the host environment. If the value of argc is greater than one, the strings pointed to by argv[1] through argv[argc-1] represent the program parameters.

- The parameters argc and argv and the strings pointed to by the argv array shall be modifiable by the program, and retain their last-stored values between program startup and program termination.
 

Re: Program in C/C++

bogdanm said:
The reason is quite subtle and is has to do with the way the compiler works. In C, after compiling the source file that has "main" in it, you get an object. But any information about the type of 'main' is gone in this object. That is, the linker (that must be called in order to get the final executable) sees only the symbol 'main' exported in the object, it doesn't have any information about the number and types of parameres and its return value. It also sees that 'main' is referenced from the startup file (this must be linked also in the final executable) and it just resolve the symbol, it doesn't care about its type. Things are a bit different in C++, but NOT for 'main'. In C++, 'main' is declared as "external C" by default, thus exhibiting the same behaviour as in C. The other functions (the ones that are not prefixed with "extern C") use a scheme called "name mangling" than encodes the type information with the function name to get a unique "function signature". This way, the type ambiguities are resolved. As stated before, the standard form for "main" is:

Code:
int main(int argc, char *argv[])


Hai...

Sorry.... i still not understand...... :oops:

Thank You
 

Re: Program in C/C++

It might be possible that void main(void) compiles with your compiler but it is wrong!
The int is used to return a value to the process which starts the program you've just written.

It is just written like this in the ANSI C standard so you should use it like that.
It the same as writing a text. When you make spelling errors, it might be possible that one reader understands what you mean and the other one doesn't. But it just ain't right to make those errors!

Regarding your second question
They are different in the type of parameter you pass to the function.
the first is a signed value (eg. -32767 to 32767)
the second is an unsigned value (eg. 0 to 65535)
the third is a constant unsigned value (meaning it cannot be altered in the function)
some compilers for microcontrollers with Harvard architecture (split program and data bus) use this to specify whether the parameter is a value in RAM or in ROM.

In C you can only have one of these functions in your program at once. In C++ you can have all of them at the same time (it supports overloading)

Antharax
 

    Help

    Points: 2
    Helpful Answer Positive Rating
Re: Program in C/C++

int main(int argc, char *argv[])
{
return 0;
}


if you save to file : test.c and result executable file is test.exe

if you run test.exe with parameter like:

test.exe parameter1 parameter2

result int argc is 2, char *argv[0] is parameter1, char *argv[1] is parameter2
 

Re: Program in C/C++

Antharax said:
It might be possible that void main(void) compiles with your compiler but it is wrong!
The int is used to return a value to the process which starts the program you've just written.

It is just written like this in the ANSI C standard so you should use it like that.
It the same as writing a text. When you make spelling errors, it might be possible that one reader understands what you mean and the other one doesn't. But it just ain't right to make those errors!

Regarding your second question
They are different in the type of parameter you pass to the function.
the first is a signed value (eg. -32767 to 32767)
the second is an unsigned value (eg. 0 to 65535)
the third is a constant unsigned value (meaning it cannot be altered in the function)
some compilers for microcontrollers with Harvard architecture (split program and data bus) use this to specify whether the parameter is a value in RAM or in ROM.

In C you can only have one of these functions in your program at once. In C++ you can have all of them at the same time (it supports overloading)

Antharax

Thanks Antharx.......... :), i already understand the 3 parameter.

Can you explain this --> int main(int argc, char *argv[]) for me...

Thanks....

Added after 1 minutes:

suromenggolo said:
int main(int argc, char *argv[])
{
return 0;
}


if you save to file : test.c and result executable file is test.exe

if you run test.exe with parameter like:

test.exe parameter1 parameter2

result int argc is 2, char *argv[0] is parameter1, char *argv[1] is parameter2

Hai....

Can you give simple program for example...?? i still not understand....

Thank you..
 


Re: Program in C/C++

echo47 said:
Please get youself a good C book and start reading!

"The C Programming Language", second edition, by Kernighan and Ritchie. It's a small efficient book, just like the language. Widely referred to as simply "K&R".
h**p://www.amazon.com/exec/obidos/asin/0131103628/

If you prefer long-winded tutorial books, I've thumbed through this one and it looks good: "C Programming - A Modern Approach" by K. N. King.
h**p://www.amazon.com/exec/obidos/asin/0393969452/


Hai...

The books that you recommend can be download???
 

Program in C/C++

I haven't seen the King book anywhere, but K&R is pretty easy to find on P2P file sharing networks. I'll look around a bit and if I find a link, I'll PM you.

Although K&R is a great book, it was published in 1988 before the 1999 ANSI standard. You'll see some old things like main() without an explicit int or return value.
 

    Help

    Points: 2
    Helpful Answer Positive Rating
Re: Program in C/C++

echo47 said:
I haven't seen the King book anywhere, but K&R is pretty easy to find on P2P file sharing networks. I'll look around a bit and if I find a link, I'll PM you.

Although K&R is a great book, it was published in 1988 before the 1999 ANSI standard. You'll see some old things like main() without an explicit int or return value.

Hai....

So, i waiting you.......

Thanks Echo47...
 

Re: Program in C/C++

echo47 said:
Although K&R is a great book, it was published in 1988 before the 1999 ANSI standard. You'll see some old things like main() without an explicit int or return value.

Look here:
 

    Help

    Points: 2
    Helpful Answer Positive Rating
Re: Program in C/C++

silvio said:
echo47 said:
Although K&R is a great book, it was published in 1988 before the 1999 ANSI standard. You'll see some old things like main() without an explicit int or return value.

Look here:
h**p://

Hi.... Silvio,

Thanks.. for your information and your help, if i have time i will go too all the information....

Thanks...
 

Re: Program in C/C++

Question 1:

a) is whole wrong
b) & c) is the same for the main programme to non return value
d) is used for return cetain value if u called this progemme from another one

Question 2:

first it's just a prototypes of funtions

second
a)if u write int only u can pass negative value
and in function of delay in DOC.h it doesn't get negative value

b) it 's correct to wite it in this form

c)i don't know exactly what it means.
 

    Help

    Points: 2
    Helpful Answer Positive Rating
Re: Program in C/C++

suromenggolo wrote:
int main(int argc, char *argv[])
{
return 0;
}


if you save to file : test.c and result executable file is test.exe

if you run test.exe with parameter like:

test.exe parameter1 parameter2

result int argc is 2, char *argv[0] is parameter1, char *argv[1] is parameter2

Hai....

Can you give simple program for example...?? i still not understand....

Thank you..

this is my example program. i wrote with visual c++

// test1.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

int main(int argc, char* argv[])
{
unsigned char i;

i = 0;
printf("argc is %d \n",argc);
for (i=0;i<argc;i++) {
printf("argv %d is %s \n", i, argv);
}
return 0;
}
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top