473,398 Members | 2,389 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,398 software developers and data experts.

void

Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2. int main(void){
  3. printf("hello");
  4. }
why is void included in this code?
Oct 18 '08 #1
3 1053
Ganon11
3,652 Expert 2GB
It is unnecessarily included. You can safely remove the void from the parentheses.

My guess is that it is included to explicitly state that main() takes no arguments: thus, its argument list is void. However, as I said before, you can safely edit this program to

Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2. int main(){
  3. printf("hello");
  4. return 0;
  5. }
(Note I added the return 0 line to the end - when you declare main() with int before it, you are promising you will return an integer - so fulfill your promise!)
Oct 18 '08 #2
whodgson
542 512MB
I respect what you say..but my book always writes:
Expand|Select|Wrap|Line Numbers
  1. int main(int nNumberOfArgs, char* pszArgs[ ])
I have searched the book for some comment in explanation but could not find one.
Oct 19 '08 #3
Banfa
9,065 Expert Mod 8TB
C++ and C99 (but not C98) allow main to be declared as

int main(int argc, char *argp[])

or

int main(void)

The void keyword is not required in C++ or C if defining a function but is required in C if you wish to declare a function with no parameters as opposed to declare a function with unspecified parameters where when declaring a function in C++ the void keyword is again optional you can not declare a function with unspecified parameters.

It is most common for book examples to use the first declaration of main as that is most compatible with most versions of compilers available including ones that perhaps were written before the standards were ratified.

NOTE: C98 only allows the first version of main given here

NOTE: That is all standard C and C++, some platforms allow non-standard declarations of main, I know a few micro-controllers whose compilers allow the non-standard

void main(void)

but in general it is better to stick to standard declarations.
Oct 19 '08 #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...
15
by: Stig Brautaset | last post by:
Hi group, I'm playing with a little generic linked list/stack library, and have a little problem with the interface of the pop() function. If I used a struct like this it would be simple: ...
188
by: infobahn | last post by:
printf("%p\n", (void *)0); /* UB, or not? Please explain your answer. */
7
by: sunglo | last post by:
My doubt comes from trying to understand how thread return values work (I know, it's off topic here), and I'm wondering about the meaning of the "void **" parameter that pthread_join expects (I...
9
by: Juggernaut | last post by:
I am trying to create a p_thread pthread_create(&threads, &attr, Teste, (void *)var); where var is a char variable. But this doesnt't work, I get this message: test.c:58: warning: cast to pointer...
5
by: Stijn van Dongen | last post by:
A question about void*. I have a hash library where the hash create function accepts functions unsigned (*hash)(const void *a) int (*cmp) (const void *a, const void *b) The insert function...
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...
27
by: Erik de Castro Lopo | last post by:
Hi all, The GNU C compiler allows a void pointer to be incremented and the behaviour is equivalent to incrementing a char pointer. Is this legal C99 or is this a GNU C extention? Thanks in...
18
by: Giannis Papadopoulos | last post by:
According to the standard (ISO C99 draft WG14/N1124), void is the incomplete type that cannot be completed and comprises the empty set of values. Since it declares the absense of a value can it be...
4
by: brianhray | last post by:
Hello: I am writing a C interface and was curious how/why a void* can be used as a reference parameter. //////////////////////////////////////////////////////////// // WORKS: // Client1.h...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.