0% found this document useful (0 votes)
27 views16 pages

Computer Programming: Instructions in C Console Input/output Type Conversion, Operator Precedence

This document discusses various topics related to computer programming in C including comments, data types, arithmetic instructions, input/output functions, and operator precedence. It explains how to write comments in C, declare variables of different data types, write arithmetic statements, and use printf() and scanf() for console input and output. It also covers type conversions that can occur during arithmetic operations and the precedence of different operators in an expression.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views16 pages

Computer Programming: Instructions in C Console Input/output Type Conversion, Operator Precedence

This document discusses various topics related to computer programming in C including comments, data types, arithmetic instructions, input/output functions, and operator precedence. It explains how to write comments in C, declare variables of different data types, write arithmetic statements, and use printf() and scanf() for console input and output. It also covers type conversions that can occur during arithmetic operations and the precedence of different operators in an expression.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 16

Computer Programming

Lecture 7
Instructions in C
Console input/output
Type Conversion, Operator Precedence
1
Agenda
• Comments
• Instructions in C
– Type Declaration
– Arithmetic
• Programs
– Print “Hello World”
• Console input/output
– printf() / scanf()

2
Comments
• Describes the program code
• Written within /* …. */ or // ….
• Helps in understanding the code
• Comments can be split into multiple lines

3
Learning - Step 3

4
Instructions
• Each instruction is called a statement
• Statements MUST appear sequentially in the same
order in which we intend to execute them
• Blank space can be placed between words to
improve readability
• All statements are entered in lowercase
• No specific rule for positioning, free form language
• Each statement MUST end with semicolon (;)

5
Types of instructions
• Type Declaration instruction
– Used to declare (announce) type of variable
Examples:-
int i; #declares variable ‘i’ of integer type
float f; #declares variable ‘f’ of float point type
char ch; #declares variable ‘ch’ of character type
int n=10; #declares variable ‘n’ of integer type and assigns an initial value
int n1,n2; #declares two variables of integer type
float f1=10.3, f2; #declares two floating point variable and initializes one of them
int a,b,c;
a=b=c=10; #declaration and initialization all to same value
int a=10,b=a+1; #declaration and initialization (NOTE: Order is important)

6
Types of instructions contd.
• Arithmetic instruction
– Consist of variable name on LHS of ‘=‘
– Consist of variables/constants on RHS of ‘=‘
– Operators could be +, -, *, /, %
• Modes of operation:-
1. Integer mode
2. Real mode
3. Mixed mode

7
Arithmetic Instructions
• Only one variable on LHS
• Modular division operator (%)
– Works only for integer operands
– Sign of remainder is same as sign of numerator
eg. 10%2=0, 5%3=2, -4%3=-1, 4%-3=1
• Arithmetic with characters is possible
• No operator is assumed to be present
eg. 2i+3 is wrong, should write 2*i+3
• No operator for exponentiation
– Can you write a program to find exponent?

8
Learning – Step 4

9
First C program – “Hello World”

10
Console Input / Output
• printf()
– Prints the output text on the screen
• scanf()
– Reads input from user

11
scanf() Example, reading input

12
Type Conversions
• Arithmetic operation between int and int yields an int
• Arithmetic operation between float and float yields an float
• Arithmetic operation between int and float yields a float

• If type of LHS and RHS are NOT equal, then value of


expression might get promoted or demoted
eg. int k; k = 0.2/9, k=?

13
Operator Precedence
• Order in which operators would get executed
– 1st : * / %
– 2nd : + -
– 3rd : =
eg.
4*5–3=?
4*5/4+5–7*2=?

14
Associativity
• When an expression contains two operators
of equal priority, then tie is settled by using
associativity of operator
• Types:
– Left to Right, left operand must be unambiguous
eg. /,*,%,+,- have L to R associativity
– Right to Left, right operand must be unambiguous
eg. ++,-- have R to L associativity

15
Questions

16

You might also like