By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,731 Members | 1,104 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,731 IT Pros & Developers. It's quick & easy.

A simple question about input a constant in C

P: 17
I want to input (scanf) an integer, then treat (or define) it as a constant. How do I do?
Jun 12 '07 #1
Share this Question
Share on Google+
7 Replies


weaknessforcats
Expert Mod 5K+
P: 9,197
You typecast the variable to a const:

Expand|Select|Wrap|Line Numbers
  1. int x;
  2. scanf("%d", &x);
  3. const int y = (const int)x;
  4. y = 10; /* error! */
  5.  
Then use the typecast variable in your code instread of the original.
Jun 12 '07 #2

P: 17
You typecast the variable to a const:

Expand|Select|Wrap|Line Numbers
  1. int x;
  2. scanf("%d", &x);
  3. const int y = (const int)x;
  4. y = 10; /* error! */
  5.  
Then use the typecast variable in your code instread of the original.

I tried. It does not work...
Jun 12 '07 #3

100+
P: 208
I tried. It does not work...
Which part doesn't work....where are you getting errors. The code above should work perfectly.
Jun 12 '07 #4

P: 17
Which part doesn't work....where are you getting errors. The code above should work perfectly.
I think this may be the problem:
I define all the functions; then a number is inputted and it is defined as a constant (say, y, using the code that weaknessforcats posted); after that, the functions are called.
I guess the functions should be defined after the constant is defined, because come of them use the constant (y).
I'll try this soon...
Jun 12 '07 #5

100+
P: 208
I think this may be the problem:
I define all the functions; then a number is inputted and it is defined as a constant (say, y, using the code that weaknessforcats posted); after that, the functions are called.
I guess the functions should be defined after the constant is defined, because come of them use the constant (y).
I'll try this soon...
When you declare the functions are you declaring the const variable constant?

e.g
function prototype
Expand|Select|Wrap|Line Numbers
  1. void functionOne(const int y);
call
Expand|Select|Wrap|Line Numbers
  1. functionOne(y);
I sometimes forget to put it as a constant in the function.
Jun 12 '07 #6

P: 17
Which part doesn't work....where are you getting errors. The code above should work perfectly.
I tested. It is nothing to do with where functions declared. I think I did exactly as
the code provided. Here are first part of my code and the error message:

Code: ( c )
#include <stdio.h>
void main ()
{
int max;
int a[10], i;
printf ("enter total number: ");
scanf ("%d", &max);
const int maxmium = (const int) max;
printf ("enter some integers: ");
for (i=0; i<maximum; i++)
scanf ("%d", &a[i]);
...
errors:
line 8: parse error before `const'
line 10: `maximum' undeclared (first use in this function)
Jun 12 '07 #7

ilikepython
Expert 100+
P: 844
I tested. It is nothing to do with where functions declared. I think I did exactly as
the code provided. Here are first part of my code and the error message:

Code: ( c )
#include <stdio.h>
void main ()
{
int max;
int a[10], i;
printf ("enter total number: ");
scanf ("%d", &max);
const int maxmium = (const int) max;
printf ("enter some integers: ");
for (i=0; i<maximum; i++)
scanf ("%d", &a[i]);
...
errors:
line 8: parse error before `const'
line 10: `maximum' undeclared (first use in this function)
You misspelt maximum("maxmium") and when I fixed that it works fine.
Jun 12 '07 #8

Post your reply

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