UNIVERSITY OF DAR ES SALAAM
PROGRAMMING IN C
Input – Output and Operators
Masoud H. Mahundi
+255 713832252
[email protected]
Input / Output
These are C Language functionalities
The program, uses the operating system to communicates with the I/O devices and external
files
The use of these functions requires to include the stdio.h file
Common input is through the keyboard and output is through the console, the monitor.
Could also be an external file – to file or from file
Through these functions we read from the user deliver the message to the user.
Basic of these functions are the printf() and scanf()
printf() for output and scanf() for input
The printf() function
Comes from print formatted format Description
%d print an int argument in decimal
Generates output under the control
%ld print a long int argument in decimal
of some format string %c print a character
Prints on to the monitor not any other %s print a string
destination %f print a float or double argument
%e same as %f, but use exponential notation
It has two arguments as shown
%g use %e or %f, whichever is better
printf (“format string”, variable list); %o print an int argument in octal (base 8)
Wherever there is a % the computer %x print an int argument in hexadecimal (base 16)
goes out of the quote marks %% print a single %
The printf() function
Example printing statements
1. printf("Hello, world!\n");
This one prints a constant string Hello, world
2. printf("i is %d\n", i);
it replaced the character %d with the value of the variable i.
3. printf(“%f”, salary);
it replaced the character %f with the value of the value in the variable salary.
4. printf(“%0.3f”, salary);
it replaced the character %f with the value of the value in the variable salary but puts it to three (3) decimal places.
5. printf(“%e”, salary);
it replaced the character %f with the value of the value in the variable salary but in exponential form.
The printf() function
The printf() function uses its first argument
To determine the number of arguments that will follow and their data types
1. #include<stdio.h>
2. main(){
3. int age;
4. age=45;
5. float midsalary = 10000;
6. char username[15] = “Makazi Dodoma";
7. printf("\n");
8. printf("*********TRYING THE C OUTPUT*********\n");
9. printf("Mr %s of age %d is paid %f\n",username, age, midsalary);
10. }
The scanf() function
Similar to the printf()
Allows the program to accept input from the keyboard
Uses the same format strings as printf()
In this case the program instructs what data is expected in a particular field
The data type of the declared variable
Uses the unary address operator ampersand (&).
It also has two arguments as shown
scanf(“format string”, arguments list);
arguments list is the list which has already been declared
scanf("%d", &b);// it means b is the variable which was declared above
The scanf() function
1. #include<stdio.h>
2. main()
3. {
4. int age;
5. char lname[15];
6. printf("Your Name: ");//prints to the monitor - output
7. scanf("%s",lname); //takes in from the keyboard - input
8. printf("Your Age: ");
9. scanf("%d",&age);
10. printf("\n\n");
11. printf("Your Name is %s and you are %d years old\n", lname, age);
12. }
Wake Up
#include<stdio.h>
Given the code fragment
main()
{
float a = 1.618;
float b = 57000;
What will be the output in the following lines
1. printf("%f",a);
2. printf("%0.1f",a);
3. printf("%0.2f",a);
4. printf("%e %e",a,b);
5. printf("%0.4f - %s - %e - %d - %0.2f",a,"friday",b,5,3.142);
Wake Up
Write a small program that
1. Through a console (screen), prompt the user on to enter
their age
2. Accept their ages as will be entered through the keyboard
3. Print on the screen their age like;
Thank you, you are 24 years old
Operators
symbols used for computations and manipulation of data
it is the operators which make the data processing possible
There are six basic categories of operators in programming
1. Arithmetic operators,
2. Relational operators,
3. Logical operators,
4. Increment and decrement operators
5. Conditional or ternary operators
6. Assignment Operators
7. Others
Arithmetic Operators
Operator Description
+ Addition e.g. result = var1 + var2;
Subtraction
Used to subtract number
Also used to negate numbers
* Multiplication e.g. result = var1 * var2;
/ Division e.g. result = var1 / var2
When applied to integers, the division operator / discards any remainder, so 1 / 2 is 0 and 7 / 4 is 1.
When either operand is a floating-point quantity (type float or double), the division operator yields a floating-
point result, with a potentially nonzero fractional part. So 1 / 2.0 is 0.5, and 7.0 / 4.0 is 1.75
% Modulus (remainder)
Gives the remainder when two integers are divided
Can only be applied to integers
E.g. 5 % 2 = 1
Arithmetic Operators
1. #include<stdio.h>
2. main()
3. {
4. int age, byear;
5. printf("YEAR OF BIRTH\t");
6. scanf("%d",&byear);
7. age = 2023-byear;
8. printf("YOU ARE %d YEARS OLD\n",age);
9. }
Relational Operators
Sometimes are referred to as comparison operator
Meant for comparison between operands
They are often used in the situations where we need to do some decisions
The results of the relational operators are always TRUE or FALSE
Operator Description
< Is less than
<= Is less than or equal
> Is greater than
>= Is greater than or equal
== Is equal to
!= Is not equal to
Logical Operators
Often thought of as secondary operators as they are often used in conjunction with other operators
They combine two or more relational operations
Operate in the same mathematical logics – as in logical AND, logical OR
The results of the logical operators are always TRUE or FALSE
Operator Description
&& Logical AND: only TRUE AND TRUE results to TRUE, any other combination – FALSE
|| Logical OR: only FALSE OR FALSE results to FALSE, any other combination – TRUE
! Logical NOT: Negate every relation
Increment and Decrement
These are unary operators, as they work on one operand only
They work in two ways; prefix and postfix
1. int var1 = 9;
Operand Description 2. int var2 = ++var1;
In this case the value of var1
Z = ++ X Increment X first and then assign the result to Z
and var2 are both 10
Z = X ++ Assign X to Z first then increment X
1. int var1 = 9;
Z = --X Decrement X first and then assign the result to Z
2. int var2 = var1++;
Z = X-- Assign X to Z first and then Decrement X In this case the value of var1 is
10 While var2 is 9
x++ and ++x have the same meaning when they each stand alone
Ternary Operators
Combining some symbols to test if a certain condition is met
It has three parts,
The condition -- Instruction when the condition is TRUE -- Instruction when the condition is FALSE.
<<condition>> ? <<expression when TRUE>> : <<expression when FALSE>>;
1. #include<stdio.h>
2. main() {
3. int grage, brage;
4. printf("BRIDE'S AGE\t");
5. scanf("%d",&brage);
6. printf("GROOM'S AGE\t");
7. scanf("%d",&grage);
8. (grage>=brage)?printf("NORMAL COMBINATION\n"):printf("NOT COMMON IN AFRICA\n");
9. }
Wake Up
Write a program that
1. prompt a user for scores for test 1
2. Accept the scores through the keyboard
3. prompt a user for scores for test 1
4. Accept the scores through the keyboard
5. Compute the students course work
6. Tell the student something like
Your coursework is: 35