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

what is phylogeny
Odigie Reply
evolutionary history and relationship of an organism or group of organisms
AI-Robot
ok
Deng
what is biology
Hajah Reply
the study of living organisms and their interactions with one another and their environments
AI-Robot
what is biology
Victoria Reply
HOW CAN MAN ORGAN FUNCTION
Alfred Reply
the diagram of the digestive system
Assiatu Reply
allimentary cannel
Ogenrwot
How does twins formed
William Reply
They formed in two ways first when one sperm and one egg are splited by mitosis or two sperm and two eggs join together
Oluwatobi
what is genetics
Josephine Reply
Genetics is the study of heredity
Misack
how does twins formed?
Misack
What is manual
Hassan Reply
discuss biological phenomenon and provide pieces of evidence to show that it was responsible for the formation of eukaryotic organelles
Joseph Reply
what is biology
Yousuf Reply
the study of living organisms and their interactions with one another and their environment.
Wine
discuss the biological phenomenon and provide pieces of evidence to show that it was responsible for the formation of eukaryotic organelles in an essay form
Joseph Reply
what is the blood cells
Shaker Reply
list any five characteristics of the blood cells
Shaker
lack electricity and its more savely than electronic microscope because its naturally by using of light
Abdullahi Reply
advantage of electronic microscope is easily and clearly while disadvantage is dangerous because its electronic. advantage of light microscope is savely and naturally by sun while disadvantage is not easily,means its not sharp and not clear
Abdullahi
cell theory state that every organisms composed of one or more cell,cell is the basic unit of life
Abdullahi
is like gone fail us
DENG
cells is the basic structure and functions of all living things
Ramadan
What is classification
ISCONT Reply
is organisms that are similar into groups called tara
Yamosa
in what situation (s) would be the use of a scanning electron microscope be ideal and why?
Kenna Reply
A scanning electron microscope (SEM) is ideal for situations requiring high-resolution imaging of surfaces. It is commonly used in materials science, biology, and geology to examine the topography and composition of samples at a nanoscale level. SEM is particularly useful for studying fine details,
Hilary
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