Basic Programming Lab
— — 0x07
Operator Precedence
Operator Meaning of operator Associativity
() Functional call
[] Array element reference
Left to right
-> Indirect member selection
. Direct member selection
Logical negation
!
Bitwise(1 's) complement
~
Unary plus
+
Unary minus
-
Increment
++
Decrement Right to left
--
Dereference (Address)
&
Pointer reference
*
Returns the size of an
sizeof
object
(type)
Typecast (conversion)
* Multiply
/ Divide Left to right
% Remainder
+ Binary plus(Addition)
Left to right
- Binary minus(subtraction)
<< Left shift
Left to right
>> Right shift
< Less than
<= Less than or equal
Left to right
> Greater than
>= Greater than or equal
Operator Precedence
Operator Meaning of operator Associativity
= Equal to
Left to right
!= Not equal to
& Bitwise AND Left to right
^ Bitwise exclusive OR Left to right
| Bitwise OR Left to right
&& Logical AND Left to right
|| Logical OR Left to right
?: Conditional Operator Right to left
= Simple assignment
*= Assign product
/= Assign quotient
%= Assign remainder
+= Assign sum
-= Assign difference Right to left
&= Assign bitwise AND
^= Assign bitwise XOR
|= Assign bitwise OR
<<= Assign left shift
>>= Assign right shift
, Separator of expressions Left to right
Formatting output in printf
Description Code Result
Width 6 printf("\n|%6d|", 73); | 73|
Width 6, left-justified printf("\n|%-6d|", 73); |73 |
Width 6, zero-filled printf("\n|%06d|", 73); |000073|
Width 6, with sign printf("\n|%+6d|", 73); | +73|
Description Code Result
%f usage printf("\n|%f|",
|12.345600|
12.3456);
%e usage printf("\n|%e|",
|1.234560e+01|
12.3456);
Print one position after the printf("\n|%.1f|",
|12.3|
decimal 12.3456);
printf("\n|%.2f|",
Two positions after the decimal |12.35|
12.3456);
Width 10, two positions after printf("\n|%10.2f|",
| 12.35|
the decimal 12.3456);
Width 10, four positions after printf("\n|%10.4f|",
| 12.3456|
the decimal 12.3456);
Width 10, two positions after printf("\n|%010.2f|",
|0000012.35|
the decimal, zero-filled 12.3456);
Width 10, two positions after printf("\n|%-10.2f|",
|12.35 |
the decimal, left-justified 12.3456);
break & continue & goto
// Program to check working of break, continue & goto
#include <stdio.h>
int main()
{
int i;
for (i = 1; i < 20; ++i)
{
printf("\n%d ", i);
if ( i % 3 == 0)
{
printf("\n%d is divisible by 3", i);
continue;
}
if ( i % 7 == 0)
{
printf("\n%d is divisible by 7", i);
break;
printf("\nNot visible to console");
}
}
printf("\nOut of loop");
goto label1;
if ( 7 % 2 == 0)
{
label1: printf("\nHere");
}
return 0;
}
Assignment
//0x06
//Use scanf for input in Every Program
1. Write a program to print formatted integers.
Input : 123
Output:
a) |1|2|3| | | | | | | | //Width = 10
b) | | | | | | | |1|2|3| //Width = 10
2. Write a program to print formatted floating point
numbers.
Input : 1234.56
Output:
a) |1|2|3|4|.|5|6|0|0|0|0|
b) |1|.|2|3|4|5|6|0|e|+|0|3|
c) |1|2|3|5|
d) |1|2|3|4|.|6|
e) |1|2|3|4|.|5|6|0| | |
f) | | |1|2|3|4|.|5|6|0|
g) | |1|.|2|3|5|e|+|0|3|
3. Write a program to print factorial of a number.
4. Write a program to print HCF of two numbers.
5. Write a program to find the sum of series
1 + (1+2) + (1+2+3) + (1+2+3+4) ...
6. Write a program to count the no of digits from the
input no.
7. Write a program to check whether a no is
palindrome or not.
Points to Remember
1. Filetype: .c
2. Naming Convention for File: Question_Y.c
where Y = Question No in that Assignment
example: Question_1.c
3. Write your details in every program
/*
--------------------------------------------------
|Author : Your_Name |
|Roll No: Your_Roll_No |
|Department: Your_Department |
--------------------------------------------------
*/