473,387 Members | 1,863 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.

Conversions in C

dianagaby2002
I don't understand these conversions in C I have for homework. Could you explain me or give some links?

1)Given int n= 200, m= 200;
long r;
And supposing sizeof(int) is 2, what is the result of: r = n*m; ?

Answer is -25536

Why?

2)Given static int i, t[10]; i and t are not explicit initialised

Which will be the result of the expression:
(i=0) || (t[i] <0)

Result is 1

3) a) Given char c = 130;

c will become -126

b) What will the following sequence return for this input: 20?

#include<stdio.h>
void main(){
char a;
scanf("%c", &a);
printf("%c", a);
}

Result is: 2

Thank you!
Jul 13 '12 #1

✓ answered by donbock

Please refer to posting coursework questions and answers.

I'll give you some hints...

Question 1 not only assumes sizeof(int) is 2, but it also assumes that negative numbers are represented in two's-complement format (this is nearly always the case).

Question 2 says the variables "are not explicitly initialized". What are the non-explicit ways for variables to be initialized? By the way, the expression should be:
(i==0) || (t[i] <0)

Question 3a assumes that type char is signed (it needn't be) and that negative numbers are represented in two's-complement format.

Question 3b; I suppose you mean that the input is the two characters '2' and '0'. Review the manpages for scanf and printf to see precisely how each interprets the %c format specifier. This question probably assumes that stdin and stdout are unbuffered.

You should look up the following terms if you are not familiar with them:
  • two's-complement
  • buffered I/O
  • nonbuffered I/O or unbuffered I/O

3 1644
donbock
2,426 Expert 2GB
Please refer to posting coursework questions and answers.

I'll give you some hints...

Question 1 not only assumes sizeof(int) is 2, but it also assumes that negative numbers are represented in two's-complement format (this is nearly always the case).

Question 2 says the variables "are not explicitly initialized". What are the non-explicit ways for variables to be initialized? By the way, the expression should be:
(i==0) || (t[i] <0)

Question 3a assumes that type char is signed (it needn't be) and that negative numbers are represented in two's-complement format.

Question 3b; I suppose you mean that the input is the two characters '2' and '0'. Review the manpages for scanf and printf to see precisely how each interprets the %c format specifier. This question probably assumes that stdin and stdout are unbuffered.

You should look up the following terms if you are not familiar with them:
  • two's-complement
  • buffered I/O
  • nonbuffered I/O or unbuffered I/O
Jul 13 '12 #2
weaknessforcats
9,208 Expert Mod 8TB
Question 1 assumes sizeof(long) is 2. long is guaranteed to be the sizeof an int but may be longer. So is the answer here is indeterminate.

Question 2 is 1. static int is initialized to 0 by default.

Question 3a is bad code. 130 is larger than a char, unless the char is unsigned. Your compiler should give a warning here.

Question 3b, scanf only reads one byte into a char. You get the 2 of 20.
Jul 13 '12 #3
donbock
2,426 Expert 2GB
Regarding the assumptions for question 1...

The C Standard guarantees that sizeof(int) is no smaller than 2; but it might be larger. The Standard goes on to guarantee that sizeof(long) is no smaller than 4 or sizeof(int), whichever is bigger; but it might be larger.

I believe that the Question 1 answer reported by the OP is fully explained by:
> the assumption that sizeof(int) is 2;
> the assumption that negative numbers are represented by two's complement format; and
> the fact [not assumption] that sizeof(long) > 2.

Another hint for Q1... the following code snippet has a different value for r than reported by the OP for Q1.
Expand|Select|Wrap|Line Numbers
  1. int n = 200;
  2. long m = 200;
  3. long r;
  4. r = n * m;
  5. printf("%ld\n",r);
Jul 15 '12 #4

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

Similar topics

3
by: Reneé | last post by:
I wanted to know the order of implicit conversions and which sort of values allow them. From searching around in books and the archive of this mailing list, it seems to be that only numbers are...
15
by: buda | last post by:
Let me see if I got this :) 1. I know the rules for type conversions in arithmetic expressions 2. I know that an implicit type conversion is done at assignment, so float x = 1.23; int t = (int)...
10
by: vb | last post by:
Hi all, I am a newbie in C and i want to know what all pointer conversions are "legal" according to ANSI C standard. For Example, int* to char*, some_struct* to char* and so on .. According to...
4
by: O. Zimmermann | last post by:
Hi all, A variable with an enumerated type can be set with a value from any other generic "enum" type in the program, or with an integer value, without notice to the user, neither at compilation...
31
by: Michael C | last post by:
If a class inherits from another class, say Form inherits from control, then I can assign the Form to a variable of type Control without needing an explicit conversion, eg Form1 f = new Form1();...
2
by: Brian Henry | last post by:
is there info about data type conversions between access to ado.net? I have tried a lot of the ones that say oledb data types should work for and get a lot of "data type mismatch" errors using...
36
by: Chad Z. Hower aka Kudzu | last post by:
I have an implicit conversion set up in an assembly from a Stream to something else. In C#, it works. In VB it does not. Does VB support implicit conversions? And if so any idea why it would work...
5
by: Daz | last post by:
Hi everyone! I am very new to casting and converting, so much in fact then when I think I have it figured out, something proves me wrong. I was wondering if there is any logic in 'what types can...
2
by: adsci | last post by:
Hello! Im posting this to c.l.c++ AND win32 because i dont know if this is a MS Compiler Issue or not. here we go: <code> class MyCString
3
by: Wayne | last post by:
Are user-defined conversions chosen at compile time, or are there ways to make sure that they are chosen at run-time, based on the actual type of the object? Here is a simplified example of what...
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: 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: 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
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
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,...

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.