Data Types and Input Output in C
Size of a Character in C #include int main() { char ch; ch = 'A'; printf("ch = %c\n",ch); printf("size of char = %d bytes", sizeof(ch)); return 0; }
Size of a Character in C #include int main() { char ch; ch = 'A'; printf("ch = %c\n",ch); printf("size of char = %d bytes", sizeof(ch)); getch(); return 0; }
Using ASCII Values #include int main() { char ch; ch = 65; printf("ch = %c\n",ch); printf("size of char = %d bytes", sizeof(ch)); return 0; }
Short int #include int main() { short sh; sh = 6; printf("sh = %d\n",sh); printf("size of short = %d bytes", sizeof(sh)); return 0; }
int #include int main() { int i; i = 6000; printf("i = %d\n",i); printf("size of int = %d bytes", sizeof(i)); return 0; }
long #include int main() { long l; l = ; printf("l = %ld\n",l); printf("size of long = %d bytes", sizeof(l)); return 0; }
Float #include int main() { float flt; flt = ; printf("flt = %f\n",flt); printf("size of float = %d bytes", sizeof(flt)); return 0; }
double #include int main() { double dbl; dbl = ; printf("dbl = %f\n",dbl); printf("size of double = %d bytes", sizeof(dbl)); return 0; }
Long double #include int main() { long double ldbl; ldbl = ; printf("ldbl = %Lf\n",ldbl); printf("size of long double = %d bytes", sizeof(ldbl)); return 0; }
i
Input/Output Operations and Functions
printf function
Printing expressions of variables
scanf function
,
A First Book of ANSI C, Fourth Edition23 Interactive Input (continued) This statement produces a prompt Address operator (&)
A First Book of ANSI C, Fourth Edition24 Interactive Input (continued) scanf() can be used to enter many values scanf(“%f %f",&num1,&num2); //"%f%f" is the same A space can affect what the value being entered is when scanf() is expecting a character data type – scanf(“%c%c%c",&ch1,&ch2,&ch3); stores the next three characters typed in the variables ch1, ch2, and ch3 ; if you type x y z, then x is stored in ch1, a blank is stored in ch2, and y is stored in ch3 – scanf (“%c %c %c",&ch1,&ch2,&ch3); causes scanf() to look for three characters, each character separated by one space
A First Book of ANSI C, Fourth Edition25 Interactive Input (continued) In printing a double-precision number using printf(), the conversion control sequence for a single-precision variable, %f, can be used When using scanf(), if a double-precision number is to be entered, you must use the %lf conversion control sequence scanf() does not test the data type of the values being entered In scanf(“%d %f", &num1, &num2), if user enters 22.87, 22 is stored in num1 and.87 in num2
scanf #include int main() { char ch, ch1, ch2; scanf("%c%c%c", &ch, &ch1, &ch2); printf("ch = %c\n", ch); printf("ch1 = %c\n", ch1); printf("ch2 = %c\n", ch2); return 0; }
scanf #include int main() { char ch, ch1, ch2; scanf("%c %c %c", &ch, &ch1, &ch2); printf("ch = %c\n", ch); printf("ch1 = %c\n", ch1); printf("ch2 = %c\n", ch2); return 0; }