473,385 Members | 1,324 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

void means function doesn't return any value?the program below return value of c?how

void main()
{
int a=5, b=6,c;
c=a+b;
printf("%d",c);
getch();
}
Oct 27 '09 #1
3 3095
Banfa
9,065 Expert Mod 8TB
void main()

is undefined behaviour, it should be

int main()

Undefined behaviour means just that, the behaviour of the program is undefined it may do anything including what you describe.

You should avoid undefined behaviour.
Oct 27 '09 #2
weaknessforcats
9,208 Expert Mod 8TB
This program does not return a value. c is a local variable and the function displays the local variable. There is no return statement in the function. If there were, the function would not compile with a void return type.

BTW: Banfa is correct in not using void main().
Oct 27 '09 #3
donbock
2,426 Expert 2GB
How does the OP know the return value of main is character c? Its not like some other function called main and received a return value.

The return value for function main is a special case. It isn't returned to the C function that called main; instead it is returned to the operating system environment that ran the program. I suspect the OP noticed that the operating system was getting a return value from the program that matched the value of c.

Function main is a special case for another reason. You don't write a function prototype for it. Instead, the function prototype is provided implicitly by the compiler. Regardless of whether your implementation of main returns a value, the C run-time environment knows there is a return value because of this implicit prototype.

Integer return values are frequently implemented by returning the value in a particular register. Thus, the C run-time environment is confident that the value of that register is whatever is in that register. Since you coded main without a return statement, the register holds some intermediate value ... as luck would have it, that intermediate value happens to be the value of c.
Oct 27 '09 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

192
by: Kwan Ting | last post by:
The_Sage, I see you've gotten yourself a twin asking for program in comp.lang.c++ . http://groups.google.co.uk/groups?hl=en&lr=&ie=UTF-8&oe=UTF-8&th=45cd1b289c71c33c&rnum=1 If you the oh so mighty...
27
by: Maximus | last post by:
Hi, I was just wondering, is it good to use return without arguments in a void function as following: void SetMapLayer() { if( !Map ) return; layer = LAYER_MAP; }
52
by: Douglas Garstang | last post by:
I can't believe I've been trying to work this out for hours now, and I can't believe I couldn't find someone asking for a similar solution in the newsgroups. No wonder I hate C so much, and every...
188
by: infobahn | last post by:
printf("%p\n", (void *)0); /* UB, or not? Please explain your answer. */
29
by: whatluo | last post by:
Hi, c.l.cs I noticed that someone like add (void) before the printf call, like: (void) printf("Timeout\n"); while the others does't. So can someone tell me whether there any gains in adding...
56
by: maadhuu | last post by:
hello, this is a piece of code ,which is giving an error. #include<stdio.h> int main() { int a =10; void *p = &a; printf("%d ", *p ); //error....why should it //be an error ?can't the...
16
by: Abhishek | last post by:
why do I see that in most C programs, pointers in functions are accepted as: int func(int i,(void *)p) where p is a pointer or an address which is passed from the place where it is called. what...
12
by: Abhishek | last post by:
now suppose I have declared an integer value inside a function as int x; now if the return type of the function is of type (void *) then can I write return((void *)x) in side the function? I...
28
by: Yevgen Muntyan | last post by:
Hey, Consider the following code: void func (void) { } void func2 (void) {
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.