0% found this document useful (0 votes)
33 views

Basic Types

This document discusses basic data types in C programming. It covers integer, floating point, character and string types. It includes examples of type conversions between integer and floating point, and casts between different data types. It prints the results of programs demonstrating type conversions and casts to illustrate C's type conversion rules.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views

Basic Types

This document discusses basic data types in C programming. It covers integer, floating point, character and string types. It includes examples of type conversions between integer and floating point, and casts between different data types. It prints the results of programs demonstrating type conversions and casts to illustrate C's type conversion rules.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Basic Types

Taken from: Feuer, A. R., (1999). The C Puzzle Book, 3rd Printing.
USA. Addison Wesley Longman Inc. Used for educational
purposes.

Basic Types
Basic Types 1: Character, String and Integer Types
Explain
Basic Types 2: Integer and Floating Point Casts
Explain
Basic Types 3: More Casts
Explain

Basic Types
C has a comparatively small set of built-in types. The arithmemtic
types may be blindly mixed in expressions, the results governed by

Basic Types 1
a simple hierarchy of conversions. This hierarchy is ilustrarted in
Appendix 4.

For some of the puzzles in this sectionsyou will need to knowt the
corresponding integer values of some chatracters. Appendix 3
show the values for the ASCII charater set. A few of the puzzles
yield a diferent result on the intel 8088 than motorola 68000. For
those puzzles, output from both machines is given.

Basic Types 1: Character, String and Integer Types


What does the following program print?

#include <stdio.h>

#define PRINT(format,x) printf(#x " = %" #format "\n",x)

int integer = 5;
char character = '5';
char *string = "5";

int main()
{
PRINT(d, string); PRINT(d, character); PRINT(d, integer);
PRINT(s, string); PRINT(c, character); PRINT(c, integer=53);
PRINT(d, ('5' > 5)); // Basic Types 1.1

{
int x = -2;
unsigned int ux = -2;

PRINT(o, x); PRINT(o, ux);


PRINT(d, x / 2); PRINT(d, ux / 2);
PRINT(o, x >> 1); PRINT(o, ux >> 1);
PRINT(d, x >> 1); PRINT(d, ux >> 1 ); // Basic Types 2
}

system("pause");
}

Basic Types 2
string = una dirección de memoria cualquiera
character = 53
integer = 5
string = 5
character = 5
integer=53 = 5
('5' > 5) = 1

x = 37777777776
ux = 37777777776
x / 2 = -1
ux / 2 = 2147483647
x >> 1 = 37777777777
ux >> 1 = 17777777777
x >> 1 = -1
ux >> 1 = 2147483647

Explain

Basic Types 2: Integer and Floating Point Casts


What does the following program print?

#include <stdio.h>

#define PR(x) printf(#x " = %.8g\t",(double)x)


#define NL putchar('\n')
#define PRINT4(x1,x2,x3,x4) PR(x1); PR(x2); PR(x3); PR(x4); NL

int main()
{
double d;
float f;
long l;
int i;

i = l = f = d = 100 / 3; PRINT4(i,l,f,d); // Basic Types 2.1


d = f = l = i = 100 / 3; PRINT4(i,l,f,d); // Basic Types 2.2
i = l = f = d = 100 / 3.; PRINT4(i,l,f,d); // Basic Types 2.3
d = f = l = i = (float)100 / 3; PRINT4(i,l,f,d); // Basic Types 2.4

i = l = f = d = (double) (100000 / 3); PRINT4(i,l,f,d);


// Basic Types 2.5
d = f = l = i = 100000 / 3; PRINT4(i,l,f,d); // Basic Types 2.6

Basic Types 3
}

i = 33 l = 33 f = 33 d = 33
i = 33 l = 33 f = 33 d = 33
i = 33 l = 33 f = 33.333332 d = 33.333333
i = 33 l = 33 f = 33 d = 33
i = 33333 l = 33333 f = 33333 d = 33333
i = 33333 l = 33333 f = 33333 d = 33333

Explain

Basic Types 3: More Casts


What does the following program print?

#include <stdio.h>

#define PR(x) printf(#x " = %g\t",(double)(x))


#define NL putchar('\n')
#define PRINT1(x1) PR(x1); NL
#define PRINT2(x1,x2) PR(x1); PRINT1(x2)

int main()
{
double d = 3.2, x;
int i = 2, y;

x = (y = d / i) * 2; PRINT2(x,y); x = 2 y = 1
x = 1.6 y = 3
y = 2
x = 0 y = 0x = 2 y = 1
x = 1.6 y = 3
y = 2
x = 0 y =// Basic Types 3.1
y = (x = d / i) * 2; PRINT2(x,y); // Basic Types 3.2

y = d * (x = 2.5 / d); PRINT1(y); // Basic Types 3.3


x = d * (y = ((int) 2.9 + 1.1)/d); PRINT2(x,y); // Basic Types 3.4

Basic Types 4
x = 2 y = 1
x = 1.6 y = 3
y = 2
x = 0 y = 0

Explain

Basic Types 5

You might also like