You are on page 1of 4

SEN119 / SEN121 Assoc. Prof. Yakup ÇELİKBİLEK, PhD. 27.09.

2021

Variables and Data Types


Numeric Data

Data Type/Definition Format Minimum Value Maximum Value


short %hd -215 (-32768) 215-1 (32767)
short int %hd -215 (-32768) 215-1 (32767)
int %d / %ld -231 231-1
Integer long %d / %ld -231 231-1
long int %d / %ld -231 231-1
long long %lld -263 263-1
long long int %lld -263 263-1
float %f 3.4E-38 3.4E+38
Float
double %lf 1.7E-308 1.7E+308
long double %Lf 3.4E-4932 3.4E+4932

String Data

Data Type Definition Format Description


char char name %c A single character
char[n] char name[n] %s (String) A sequence of characters (text)

Logic Data

Data Type Format Description


_Bool %d (Boolean) 1 (true) – 0 (false)
bool %d (Boolean) 1 (true) – 0 (false) (with <stdbool.h> )

Constant (Final) Variables

If a variable defined as constant (“const”), this means that the variable cannot be
modified or reassigned in the future. Therefore, the value of the variable is constant (final). This
feature also allows us to control the variable not to change incorrectly by ourselves or during
the run of the whole code. Some examples about using the “const” statement is given below.

const short a = 100; const long c = 32780; const char cc = ‘C’;

const int b = 200; const double d = 5.2;


Data Conversion
While assigning one data type variable to another type of variable or doing a
mathematical operation between different types of variables, some or all information of the
variable can be loosen because of incompatible type of these variables. To avoid significant
losses in such cases, the data types of the variable must be made compatible each other. There
are three main data type conversions; numeric-numeric, numeric-String, String-numeric.

Numeric – Numeric Type Data Conversion


For numeric data types, when we assign a smaller data type to a bigger data type,
automatic type conversion works and there will be no problem with the compatibility.
Unfortunately, the opposite is a significant problem. For example, 5 can be used as 5.0 in
operations without any loss. However, 5.4 cannot be used as 5 as integer in operations. 0.4 of
it is lost during this operation. Similarly, 50 as a short type variable can assign to an int or long
type variable without any loss. On the other hand, 33000 as int type variable cannot assign to a
short type variable without any loss because of its size. This situation also happen while the
final value sizes of the variables are bigger than the limits of the defined data types at the
beginning. Besides, bigger data types can assign to smaller data types without any loss if the
value is in the limits of smaller data types.

short int long float double

short a1=200; short b1; b1=(short) a2; b3=(long) a4;


int a2=32780; int b2; b1=(short) a3; b4=(float) a2;
long a3=35000 long b3; b2=(short) a5; b5=(double) a3;
float a4=234567.89; float b4; b2=(int) a3; b5=(double) a4;
double a5=5.21; double b5; b3=(long) a1; b4=(float) a5;

✓ check the differences among the following statements:


b2=a5; b2=a5*10; b2=a5*100; b2=(int)a5*100; b2=(int)(a5*100);
String – Numeric Type Data Conversion (atoi / atol / atoll / atof)

char x[7]="123.45 "; float y=atoi(x); double y=atoi(x); 123.000000

int y=atoi(x); short y=atoi(x); long long y=atoi(x); 123

char x[7]="123.45 "; float y=atof(x); double y=atof(x); 123.450000

int y=atof(x); short y=atof(x); long long y=atof(x); 123

Numeric - String Type Data Conversion (sprintf)

char x[10];

int a1 = 123; sprintf(x, " %d", a1);

long long a2 = 123; sprintf(x, " %lld", a2);

double a3 = 123.456 sprintf(x, " %lf", a3);

_Bool a4 = true; sprintf(x, " %d", a4);

Operators

Operator Function Operator Function


= Assignment ++ Increment (+1)
+ Addition –– Decrement (–1)
– Subtraction || Or (logic)
* Multiplication && And (logic)
/ Division ! Is not
% Mod (Division Remainder) == Is equal to
+= Add and then assign != Is not equal to
–= Subtract and then assign > Greater than
*= Multiply and then assign < Less than
/= Divide and then assign >= Greater than or equal to
%= Take the mod and then assign <= Less than or equal to
“ printf ” special formats and examples

int num=1234567; Output .


printf (‘‘%d’’, num); 1234567
printf (‘‘%10d’’, num); _ _ _1234567
printf (‘‘%-10d’’, num); 1234567_ _ _
printf (‘‘%+10d’’, num); _ _ +1234567
printf (‘‘%-+10d’’, num); +1234567 _ _
printf (‘‘%010d’’, num); 0001234567
printf (‘‘%+010d’’, num); +001234567
printf (‘‘%-010d’’, num); 1234567 _ _ _
printf (‘‘%-+010d’’, num); +1234567 _ _
printf (‘‘%-+10d’’, num); +1234567 _ _

double doub=123.456789; Output .


printf (‘‘%lf’’, doub); 123,456789
printf (‘‘%12lf’’, doub); _ _123,456789
printf (‘‘%-12lf’’, doub); 123,456789_ _
printf (‘‘%12.3lf’’, doub); _ _ _ _ _123,457
printf (‘‘%-12.3lf’’, doub); 123,457_ _ _ _ _
printf (‘‘%012.3lf’’, doub); 00000123,457

char str [7] = ‘‘test’’; Output .


printf (‘‘%s’’, str); test
printf (‘‘%10s’’, str); _ _ _ _ _ _test
printf (‘‘%-10s’’, str); test_ _ _ _ _ _
printf (‘‘%10.2s’’, str); _ _ _ _ _ _ _ _te
printf (‘‘%.2s’’, str); te
printf (‘‘%-10.2s’’, str); te_ _ _ _ _ _ _ _

You might also like