<< 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 mutation
Janga Reply
what is a cell
Sifune Reply
how is urine form
Sifune
what is antagonism?
mahase Reply
classification of plants, gymnosperm features.
Linsy Reply
what is the features of gymnosperm
Linsy
how many types of solid did we have
Samuel Reply
what is an ionic bond
Samuel
What is Atoms
Daprince Reply
what is fallopian tube
Merolyn
what is bladder
Merolyn
what's bulbourethral gland
Eduek Reply
urine is formed in the nephron of the renal medulla in the kidney. It starts from filtration, then selective reabsorption and finally secretion
onuoha Reply
State the evolution relation and relevance between endoplasmic reticulum and cytoskeleton as it relates to cell.
Jeremiah
what is heart
Konadu Reply
how is urine formed in human
Konadu
how is urine formed in human
Rahma
what is the diference between a cavity and a canal
Pelagie Reply
what is the causative agent of malaria
Diamond
malaria is caused by an insect called mosquito.
Naomi
Malaria is cause by female anopheles mosquito
Isaac
Malaria is caused by plasmodium Female anopheles mosquitoe is d carrier
Olalekan
a canal is more needed in a root but a cavity is a bad effect
Commander
what are pathogens
Don Reply
In biology, a pathogen (Greek: πάθος pathos "suffering", "passion" and -γενής -genēs "producer of") in the oldest and broadest sense, is anything that can produce disease. A pathogen may also be referred to as an infectious agent, or simply a germ. The term pathogen came into use in the 1880s.[1][2
Zainab
A virus
Commander
Definition of respiration
Muhsin Reply
respiration is the process in which we breath in oxygen and breath out carbon dioxide
Achor
how are lungs work
Commander
where does digestion begins
Achiri Reply
in the mouth
EZEKIEL
what are the functions of follicle stimulating harmones?
Rashima Reply
stimulates the follicle to release the mature ovum into the oviduct
Davonte
what are the functions of Endocrine and pituitary gland
Chinaza
endocrine secrete hormone and regulate body process
Achor
while pituitary gland is an example of endocrine system and it's found in the Brain
Achor
what's biology?
Egbodo Reply
Biology is the study of living organisms, divided into many specialized field that cover their morphology, physiology,anatomy, behaviour,origin and distribution.
Lisah
biology is the study of life.
Alfreda
Biology is the study of how living organisms live and survive in a specific environment
Sifune
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