<< Chapter < Page | Chapter >> Page > |
For Example
scanf("%l0d",&i)
would use at most the first ten digits typed as the new value for i.
Here is an example to demonstrate the usage of printf and scanf functions
#include<conio.h>#include<stdio.h>void main()
{// variable declaration
int a;float x;
char ch;char* str;
// Enter dataprintf(“Input an integer”);
scanf(“%d”,&a);
printf(“\n Input a real number”);scanf(“%f”,&x);
printf(“\n Input a character”);fflush(stdin); scanf(“%c”,&ch);
printf(“\n Input a string” );fflush(stdin); scanf(“%s”,str);
// Output the dataprintf(“\n Your data”);
printf(“\n Integer: %d”,a);printf(“\n Float : %.2f”,x);
printf(“\n Char: %c:,ch);printf(“\n String : %s”,str);
}
(Function fflush are used to avoid stopping the reading process when meet one or more spaces)
There is one main problem with scanf function which can make it unreliable in certain cases. The reason being is that scanf tends to ignore white spaces, i.e. the space character. If you require your input to contain spaces this can cause a problem.
Scanf will skip over white space such as blanks, tabs and new lines in the input stream. The exception is when trying to read single characters with the conversion specifiers %c. In this case, white space is read in. So, it is more difficult to use scanf for single characters. An alternate technique, using getchar, will be described later.
getch
The computer asks the user press a key. This key does not appear on screen. The syntax is:
getch();
getch() is used to terminate a program when the user press a key.
gets
gets reads a line of input into a character array. The syntax is:
gets(name_of_string);
puts
puts writes a line of output to standard output. The syntax is:
puts(name of string);
It terminates the line with a new line, '\n'. It will return EOF is an error occurred. It will return a positive number on success.
For using the statements mentioned above, you must declare the library<conio.h>.
C contains the following operator groups.
The arithmetic operators are +, -, /, * and the modulus operator %. Integer division truncates any fractional part. The expression x%y produces the remainder when x is divided by y, and thus is zero when y divide x exactly.The % operator cannot be applied to a float or double.
The binary + and – operators have the same precedence, which is lower than the precedence of *, / and %, which is turn lower than unary + and - . Arithmetic operators associate left to right.
Operator | Meaning | Data type of the operands | Examples |
- | opposite | integer, float |
int a, b;
-12; -a; -25.6; |
+ | addition | integer, float |
float x, y;
5 + 8; a + x;3.6 + 2.9; |
- | subtraction | integer, float |
3 – 1.6; a – 5; |
* | multiplication | integer, float |
a * b; b * y;
2.6 * 1.7; |
/ | division | integer, float |
10.0/3.0; (= 3.33…)
10/3.0; (= 3.33…)10.0/3; (= 3.33…) |
/ | integer division | integer |
10/3; (= 3) |
% | modulus | integer |
10%3; (=1) |
These all perform an arithmetic operation on the lvalue and assign the result to the lvalue. So what does this mean in English? Here is an Example
Notification Switch
Would you like to follow the 'Introduction to computer science' conversation and receive update notifications?