<< 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.

Other input and output functions

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>.

Expressions

Operators

C contains the following operator groups.

  • Arithmetic
  • Assignment
  • Logical/relational
  • Bitwise

Arithmetic operators

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)

Assignment operators

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

Questions & Answers

if three forces F1.f2 .f3 act at a point on a Cartesian plane in the daigram .....so if the question says write down the x and y components ..... I really don't understand
Syamthanda Reply
hey , can you please explain oxidation reaction & redox ?
Boitumelo Reply
hey , can you please explain oxidation reaction and redox ?
Boitumelo
for grade 12 or grade 11?
Sibulele
the value of V1 and V2
Tumelo Reply
advantages of electrons in a circuit
Rethabile Reply
we're do you find electromagnetism past papers
Ntombifuthi
what a normal force
Tholulwazi Reply
it is the force or component of the force that the surface exert on an object incontact with it and which acts perpendicular to the surface
Sihle
what is physics?
Petrus Reply
what is the half reaction of Potassium and chlorine
Anna Reply
how to calculate coefficient of static friction
Lisa Reply
how to calculate static friction
Lisa
How to calculate a current
Tumelo
how to calculate the magnitude of horizontal component of the applied force
Mogano
How to calculate force
Monambi
a structure of a thermocouple used to measure inner temperature
Anna Reply
a fixed gas of a mass is held at standard pressure temperature of 15 degrees Celsius .Calculate the temperature of the gas in Celsius if the pressure is changed to 2×10 to the power 4
Amahle Reply
How is energy being used in bonding?
Raymond Reply
what is acceleration
Syamthanda Reply
a rate of change in velocity of an object whith respect to time
Khuthadzo
how can we find the moment of torque of a circular object
Kidist
Acceleration is a rate of change in velocity.
Justice
t =r×f
Khuthadzo
how to calculate tension by substitution
Precious Reply
hi
Shongi
hi
Leago
use fnet method. how many obects are being calculated ?
Khuthadzo
khuthadzo hii
Hulisani
how to calculate acceleration and tension force
Lungile Reply
you use Fnet equals ma , newtoms second law formula
Masego
please help me with vectors in two dimensions
Mulaudzi Reply
how to calculate normal force
Mulaudzi
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Introduction to computer science. OpenStax CNX. Jul 29, 2009 Download for free at http://cnx.org/content/col10776/1.1
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Introduction to computer science' conversation and receive update notifications?

Ask