Hello
I am new to C langauge
I have written a simple code to do addition of two numbers in C .
I was trying to check if gcc compiler catches type mismatch error.
Here is the code
#include <stdio.h>
char myadd ( char source , char dest)
{
int ret;
ret = source + dest;
return ret;
}
int main()
{
int j=5;
int k=4;
int m;
m=myadd(j,k);
printf("the sum is %d \n" , m);
return 0;
}
Compiling using gcc 4.1.1 on ubuntu 7.1 linux i get this output
gcc mystrcpy.c
./a.out
the sum is 9. Does C does not distinguish between int and char type ??
is it gcc problem or in C char and int are interchangeable
thanks
5 1778 Savage 1,764
Recognized Expert Top Contributor
Hello
I am new to C langauge
I have written a simple code to do addition of two numbers in C .
I was trying to check if gcc compiler catches type mismatch error.
Here is the code
#include <stdio.h>
char myadd ( char source , char dest)
{
int ret;
ret = source + dest;
return ret;
}
int main()
{
int j=5;
int k=4;
int m;
m=myadd(j,k);
printf("the sum is %d \n" , m);
return 0;
}
Compiling using gcc 4.1.1 on ubuntu 7.1 linux i get this output
gcc mystrcpy.c
./a.out
the sum is 9. Does C does not distinguish between int and char type ??
is it gcc problem or in C char and int are interchangeable
thanks
It does distinguishes,but char is automatically casted to int..
This enables you to easily convert between char and int
Savage
The rule for aritmetic operators uses what are called Arithmetic Conversions.
Here's the rule:
If either operand is a long double, then the other is converted to a long double.
Next, if either operand is double, then the other is converted to double.
Next, if either operand is float, then the other is converted to float.
Next, integer promotions are performed on both operands:
If the value can be put in an int, then it is converted to an int
otherwise, it is converted to an unsigned int.
Now, if either operand is unsigned long, then the other is converted to unsigned long.
Now, if either operand is long, then the other is converted to long.
Now, if either operand is an unsigned int, then the other is converted to unsigned int.
Otherwise, both are ints.
Then the operator is performed and the type of the result is the type of the operands.
Keep these rules in mind when you mix types in calculations. Some unexplainable results will suddenly become explainable.
thank you for your prompt reply
I also want to ask if there is any good book to learn C and pointers in details
thanks
Sorry guys, I had to rename this one.
thank you for your prompt reply
I also want to ask if there is any good book to learn C and pointers in details
thanks
The best way to learn C is to practice. One well known book is:
The C (ansi C) Programming Language by Brian W. Kernighan.
If you want to learn more specific topic, such as pointers, I would suggest Google. Here is the first search I found with "C Pointers" Pointers
It seems pretty detailed at a quick glance.
Another book I found was "The C Puzzle Book" by Alan Feuer. It's fun and interesting.
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Dan Stromberg |
last post by:
Is there already a pure python module that can do modular-arithmetic unit
conversions, like converting a huge number of seconds into months,
weeks... or a bandwidth measure into megabits/s or...
|
by: Christian Brechbühler |
last post by:
Let me use complex numbers as a familiar example. The following is
taken verbatim from Bjarne Stroustrup, "The C++ Programming Language",
except the include line and the "no match" comments. It...
|
by: Niels Dekker (no reply address) |
last post by:
Is it possible for a standard compliant C++ compiler to have
( sizeof(short) < sizeof(int) )
and
( sizeof(short) == sizeof((short)0 + (short)0) )
?
Regards,
Niels Dekker...
|
by: Alex Fraser |
last post by:
From searching Google Groups, I understand that void pointer arithmetic is a
constraint violation, which is understandable. However, generic functions
like qsort() and bsearch() must in essence do...
|
by: Massimiliano Alberti |
last post by:
Are there rules on how should the C behave with the arithmetic operations?
Are the overflows always ignored? And are they simply truncated? (if I have
a 16-bits unsigned short ints
unsigned short...
| |
by: TTroy |
last post by:
Hello, I'm relatively new to C and have gone through more than 4 books
on it. None mentioned anything about integral promotion, arithmetic
conversion, value preserving and unsigned preserving. ...
|
by: Frederick Gotham |
last post by:
I just want to clarify my understanding of arithmetic and comparison
between two different integer types.
Phase (1): Integer Promotion
----------
All of the following types always get...
|
by: Bill Reid |
last post by:
Bear with me, as I am not a "professional" programmer, but I was
working on part of program that reads parts of four text files into
a buffer which I re-allocate the size as I read each file. I...
|
by: sarathy |
last post by:
Hi,
What is integer promotion? How is it different from
arithmetic conversion?
Regards,
Sarathy
|
by: Joe Cool |
last post by:
Hello, I am using VS2005. I am trying to convert a VB.NET app to
C#.NET. The VB app uses a progress meter to indiate how far a file has
been read. I use the following assignment statement in VB:
...
|
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,...
| |
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: 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: 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...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
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,...
|
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: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
| |