473,387 Members | 1,575 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,387 software developers and data experts.

A simple question about input a constant in C

17
I want to input (scanf) an integer, then treat (or define) it as a constant. How do I do?
Jun 12 '07 #1
7 2596
weaknessforcats
9,208 Expert Mod 8TB
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
runsun
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
Silent1Mezzo
208 100+
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
runsun
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
Silent1Mezzo
208 100+
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
runsun
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
844 Expert 512MB
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

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

Similar topics

1
by: Me | last post by:
Any of you guys got a clue of how to assign a value from one input box to another hidden input? What I'm trying to do is take inputs for names on a registration page. You've got first, middle,...
2
by: Mrs Howl | last post by:
I have a query that just reads one table and appends to an output table, one-for-one. No criteria. It's not a Total query (i.e. no group by). It normally run run in minutes, but gets horribly...
51
by: Alan | last post by:
hi all, I want to define a constant length string, say 4 then in a function at some time, I want to set the string to a constant value, say a below is my code but it fails what is the correct...
8
by: Ross A. Finlayson | last post by:
I'm trying to write some C code, but I want to use C++'s std::vector. Indeed, if the code is compiled as C++, I want the container to actually be std::vector, in this case of a collection of value...
27
by: karan.shashi | last post by:
Hey all, I was asked this question in an interview recently: Suppose you have the method signature bool MyPairSum(int array, int sum) the array has all unique values (no repeats), your...
23
by: AndersWang | last post by:
Hi, dose anybody here explain to me why memset would be faster than a simple loop. I doubt about it! In an int array scenario: int array; for(int i=0;i<10;i++) //ten loops
0
by: shineyang | last post by:
Dear all: Who is kind to help me about the following problem. Why cannot log the remote node by using Net::Telent #################################### The following is normal to the process...
1
by: jerry | last post by:
i have written a simple phonebook program,i'll show you some of the codes,the program's head file is member.h . i suppose the head file works well.so i don't post it. here's the clips of main...
8
by: Jaco Naude | last post by:
Hi I've been struggling with something that should be very simple to solve... Basically, I get a const Obj* from a function and I need to send a pointer to this object to a function accepting...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.