Exforsys

Online Training

Variable Declaration

This is a discussion on Variable Declaration within the C and C++ forums, part of the Programming Talk category; I am confused with the declaration of variable passed as argument to a function.I tried a sample C program ...


Go Back   Exforsys > Programming Talk > C and C++

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 06-05-2007, 03:59 AM
Senior Member
 
Join Date: Apr 2006
Posts: 125
ashlee is on a distinguished road
Variable Declaration

I am confused with the declaration of variable passed as argument to a function.I tried a sample C program given below:
#include <stdio.h>
main()
{
int a=500,c;
c=sampel(a);
printf("c=%d",c);
}
sample(t)
{
int t;
t=t+10;
return(t);
}

The above program gave me error. Is it that I have to declare t before the opening braces of defining the function sample? What is the reason for this?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 06-06-2007, 04:37 AM
Junior Member
 
Join Date: May 2007
Posts: 12
Mukhtar Ahmad is on a distinguished road
int sample(int t)
{
int t;
t=t+10;
return(t);
}


look at the "int sample(int t)" as compared to sample(t)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 06-06-2007, 07:46 AM
Senior Member
 
Join Date: Apr 2006
Posts: 162
sammy is on a distinguished road
The error you would have got is Re-declaration of t in function sample. Am I right? The reason for this is you have not declared the variable t before the opening brace of function sample().So by default it is assumed as integer variable. But again after the opening brace you have declared again variable t which causes the compiler to throw the error message as re-declaration of
variable t. Remove the declaration and try out and always it is best to declare variables before the opening braces of function calling.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads

Thread Thread Starter Forum Replies Last Post
comp.lang.c Answers (Abridged) to Frequently Asked Questions (FAQ) Steve Summit Tech FAQ 0 06-15-2004 07:00 AM
comp.lang.c Answers (Abridged) to Frequently Asked Questions (FAQ) Steve Summit Tech FAQ 0 06-01-2004 07:01 AM
comp.lang.c Answers to Frequently Asked Questions (FAQ List) Steve Summit Tech FAQ 0 06-01-2004 07:00 AM
comp.lang.c Answers (Abridged) to Frequently Asked Questions (FAQ) Steve Summit Tech FAQ 0 05-15-2004 07:00 AM
Smalltalk FAQ (v.1.0) Vikas Malik Tech FAQ 0 04-17-2004 08:27 AM


All times are GMT -4. The time now is 03:01 PM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.1.0
Copyright 2004 - 2007 Exforsys Inc. All rights reserved.