15jan2019 C-1
15jan2019 C-1
15jan2019 C-1
@ NIST
National Institute of Science & Technology
.
SECTION L3-B 15.JAN.2019 [ TUE ] [1]
BACKGROUND
National Institute of Science & Technology
[2]
1983 ANSI (American National Standards Institute) began the
National Institute of Science & Technology
[3]
Why C?
National Institute of Science & Technology
• C is the common
• Originated at AT&T Bell Labs,
denominator of many of
Dennis Ritchie – implemented
today‟s languages 1972
• C‟s strong points • Development language for
– very efficient UNIX operating system
– weakly typed language
– Small structured language with
many operators
– Small set of keywords (ONLY
32!)
– Has no input/output statements!
It uses function calls.
[4]
Creating C Programs
National Institute of Science & Technology
•Editing
•Compiling
•Linking
•Executing
[5]
National Institute of Science & Technology
The compiler converts your source code into machine language and
detects and reports errors in the compilation process.
The output from the compiler is known as object code and it is stored in
files called object files,
cc myprog.c
The linker combines the object modules generated by the compiler from
source code files, adds required code modules from the standard library
supplied as part of C, and welds everything into an executable whole.
[6]
National Institute of Science & Technology
The execution stage is where you run your program, having completed
all the previous processes successfully.
When the compiler has successfully translated the program the compiler
version or the executable program code is stored in a file called a.out,
or if the –o option is used the executable program code is put in the file
listed after the –o specified in the compilation command.e.g.
cc myprog.c –o objectfile
The above command puts the compiled program into the file called
objectfile( name given by the user)
[7]
The two methods of compilation and execution is
National Institute of Science & Technology
given below.
cc myprog.c
./a.out
cc myprog.c –o objectfile
./objectfile
[8]
National Institute of Science & Technology
.
.
[9]
National Institute of Science & Technology
[10]
National Institute of Science & Technology
.
.
[11]
The C Character Set
A to Z
National Institute of Science & Technology
a to z
0 to 9
Special Character
+ - * / = % & #
! ? ^ “ ‘ ~ \ |
< > ( ) { } [ ]
: ; . , _ @ $
blank space (Handled Specially)
Combination of characters
\n \t \b etc used instead of controller characters.
[12]
Identifiers: Name given to various programming element like
National Institute of Science & Technology
[13]
Valid Identifiers:
X y12 sum_1 _temp
National Institute of Science & Technology
InValid Identifiers:
4th “x” order-on order flag
[14]
Key Words:
certain reserved words that have standard predefine meaning in C
National Institute of Science & Technology
programming.
can not be used as an Identifier ( But uppercase names are allowed)
[15]
Data Types in C Language
National Institute of Science & Technology
Data type:
Primitive /basic data type: char, int, float, double
Derived data type: array, function, pointer
User defined data types: structure, union, enum
Valueless: void.
[16]
New Data Types of C99
National Institute of Science & Technology
[17]
National Institute of Science & Technology
.
.
[18]
Name Range Storage Size
National Institute of Science & Technology
[19]
Floating-Point
. Types
C provides representations of real numbers and those
National Institute of Science & Technology
[20]
AN EXAMPLE …
National Institute of Science & Technology
[21]
National Institute of Science & Technology
[22]
ASCII may refer to any of the following:
Short for American Standard Code for Information
National Institute of Science & Technology
Lower ASCII, between 32 and 127. This table originates from the older,
American systems, which worked on 7-bit character tables.
[23]
. Decimal Binary character
National Institute of Science & Technology
48 110000 0
57 111001 9
97 1100001 a
122 1111010 z
65 1000001 A
90 1011010 Z
Example
. : ASCII VALUES
[24]
The
. void Type
National Institute of Science & Technology
The void type specifies that no value is available. It is used in three kinds of situations:
[25]
Tokens in C
A C program consists of various tokens and a token is either a
National Institute of Science & Technology
[26]
Keywords
National Institute of Science & Technology
Points to remember
[27]
auto else long switch
National Institute of Science & Technology
Key Words : 32
[28]
Whitespace in C
National Institute of Science & Technology
[29]
Variable and Its Declaration
A variable is an Identifier for a memory location In which
National Institute of Science & Technology
[30]
Variable Declaration
DataType VarableList;
National Institute of Science & Technology
Examples:
int a, test, check;
float takeFirst, takeSecond;
main()
{
int x, z, k;
k = x + z;
printf(“ Value of k is %d”,k);
}
[32]
Variable Initialization
National Institute of Science & Technology
Example:
int x=23,y=2;
float k;
k = x+y;
[33]
Operators in C
National Institute of Science & Technology
What is Operator?
An operator is a symbol that specifies the arithmetic,
logical or relational operation to be performed. C
language supports following type of operators.
[34]
National Institute of Science & Technology
.
.
[35]
Arithmetic Operators in C
National Institute of Science & Technology
Unary Operators:
Operate on one operand
Binary Operators:
Operate on two operands
Ternary Operators:
Operates on three operands
[36]
Unary Arithmetic Operator
National Institute of Science & Technology
[37]
Binary Arithmetic Operator
National Institute of Science & Technology
• If the data type of both the operands are same than operation will be
performed in the same data type and result will be in the same data
type.
• But the minimum data type should be integer (char and short are
converted into integer before actual operation)
• If they are different than the data type of one operand will be changed to
match the other (called automatic type conversion) and the operation is
performed
• The data type of the least comprehensive variable changes to the data
type of the most comprehensive variable. Type Promotion
[39]
Automatic Type Conversion
National Institute of Science & Technology
Char double
[40]
Multiple Assignment Operators ( =)
National Institute of Science & Technology
Equivalent to
z = 5;
y = z;
x = y;
[41]
Type Conversion During Assignment
National Institute of Science & Technology
Variable = Expression;
What if data type of the variable is not equal to the data type
of the expression;
An internal Automatic (Implicit) Type conversion
is done.
If the Lvalue is of higher data type than it can easily
accommodate the value of the expression.
But What if the case is reverse
In that case we are going to loose the higher order bit or
some information
[42]
Data Loss During Conversion
National Institute of Science & Technology
[43]
More on precedence for arithmetic operators
1 Unary operators has the highest precedence
National Institute of Science & Technology
[44]
Increment (++) & Decrement (--) Operators
National Institute of Science & Technology
[45]
Assignment Operators ( =)
National Institute of Science & Technology
variable_name = expression;
Lvalue = Rvalue
I = 6;
X = m * 23 + k;
Lvalue should be a memory location which can store a
value; a variable
where Rvalue can be a variable or a constant or an
expression.
A + B = 5 +2;
Assignment operator has got the least precedence
[46]
Compound Assignment Operators ( =)
National Institute of Science & Technology
x = x+5;
x += 5;
Similarly
-=, *= , /=, %= etc are different compound
assignment statements
[47]
Comparison Operators
National Institute of Science & Technology
Equality Operator
== equal to operator (checks for equality)
!= not equal to operator (checks for in equality)
Relational Operators
> Greater than operator
< Less than operator
<= Less than equal to operator
>= Greater than equal to operator
[48]
Table of Relational Operators
National Institute of Science & Technology
Operator Meaning
A == B is A equal to B ?
A < B is A less than B ?
A <= B is A less than or equal to B ?
A > B is A Greater than B ?
A >= B is A Greater than or equal to B ?
A != B is A not equal to B ?
All these operators take two operand and compare them. And return 1 for
true and 0 for false
[49]
Boolean Expressions
National Institute of Science & Technology
25 >= 25 true 25 = 25
??
[51]
• Logical Operators
National Institute of Science & Technology
[54]
Declaration of a variable in C hints the compiler about the type
and size of the variable in compile time. Similarly,
National Institute of Science & Technology
[55]
Precedence of C Operators:
National Institute of Science & Technology
Associativity
Their associativity indicates in what order operators of
equal precedence in an expression are applied
[56]
Precedence and Associativity are two characteristics of operators
National Institute of Science & Technology
Comma has the least precedence among all operators and should be
used carefully
[57]
An Example:
National Institute of Science & Technology
#include<stdio.h>
int main()
{
int a;
a = 1, 2, 3; //Evaluated as (a = 1), 2, 3
printf("%d", a);
return 0;
}
[58]
National Institute of Science & Technology
.
.
[59]
National Institute of Science & Technology
Precedence Examples
a*b+c (a*b)+c
a-b+c (a-b)+c
a++b a+(+b)
a+++b (a++)+b not a+(++b)
a++++b (a++)+(+b)
[60]
Associativity of operators
National Institute of Science & Technology
[61]
.
National Institute of Science & Technology
[62]
Type Conversion and Type casting in C
National Institute of Science & Technology
Type conversion occurs when the expression has data of mixed data
types.
double>float>long>int>short>char
[63]
National Institute of Science & Technology
.
Long double
Double
Float
Unsigned long int
Long int
Unsigned int
Int
Short
Char
.
[64]
Q1: What is the output of the following code
segment?
National Institute of Science & Technology
int main()
{
int var1=1,var2=12,var3=12;
var1=var2==var3;
printf("%d ", var1);
return 0;
}
Options:
1: 1
2: 0
3: blank space
4: Error
[65]
Answer for Q1:
National Institute of Science & Technology
Option 1: 1
Explanation:
[66]
Q2: What is the output of the following code
segment?
National Institute of Science & Technology
int main()
{
int c= - -10;
printf("%d",c);
return 0;
}
Options:
1: -10
2: - - 10
3: 10
4: Error
[67]
Answer for Q2:
National Institute of Science & Technology
Option 3: 10
Explanation:
Output: 10
-ve(of -ve) is +ve if we write c=--10 it gives error lvalue
required as decrement operand
Hence we have to write - - i.e. a space in between.
Associativity of – (unary minus operator) is from right to
left.
[68]
National Institute of Science & Technology
[69]
Answer for Q3:
#include<stdio.h>
National Institute of Science & Technology
int main(){
if(printf("Hello world")){
}
return 0;
}
---------------------------------------
#include<stdio.h>
int main(){
while(!printf("Hello world")){
}
return 0;
}
[70]
National Institute of Science & Technology
[71]
National Institute of Science & Technology
yy = yyyy % 100;
printf("Last two digits of year is: %02d",yy);
[72]
National Institute of Science & Technology
Int m=1;
Int n;
N=(m=m+3, m%3);
Options:
1: O
2: 1
3: 0 1
4: Error
[73]
National Institute of Science & Technology
Option 2: N=1
Answer for Q5:
[74]
Q6: What is the output of the following code
segment?
National Institute of Science & Technology
[75]
Answer for Q6:
National Institute of Science & Technology
Option 1: 1
i = 1,2,3;
Above Expression contain 2 comma operator and 1
assignment operator.
If we check precedence table then we can say that
“Comma” operator has lowest precedence than
assignment operator
So Assignment statement will be executed first .
1 is assigned to variable “i”
[76]
Q7: What is the output of the following code
segment?
National Institute of Science & Technology
int main()
{
int i;
i = (1,2,3);
printf("i:%d\n",i);
return 0;
}
Options:
1: 3
2: 1
3: 2
4: Error
[77]
National Institute of Science & Technology
Option 1: 3:
Explanation: i = (1,2,3);
Bracket has highest priority than any operator.
Inside bracket we have 2 comma operators.
Comma operator has associativity from Left to
Right.
Comma Operator will return Rightmost operand
[78]
Q8 What is the output of the following code
segment?
National Institute of Science & Technology
int main()
{
int var1=15,var2=10,p,q;
p=var1>14;
q=var1>8 && var2==8;
printf("%d %d", p,q);
return 0;
}
Options:
1: O 0
2: 1 1
3: 1 0
4: Error
[79]
National Institute of Science & Technology
Option 3: 1 0
Explanation:
15>14 is TRUE therefore p=1 but the second
one
15>8 && 10 == 8 is false and hence q=0
[80]
Q9: What is the output of the following code
segment?
National Institute of Science & Technology
#include<stdio.h>
int main()
{ printf("%d %d %d", sizeof(3.14f),sizeof(3.14),
sizeof(3.141L));
}
Options:
1: 4 8 12
2: 4 4 4
3: 1 1 1
4: Error
[81]
National Institute of Science & Technology
Option 1: 1
Explanation:
3.14f is treated as float,
3.14 is promoted to double
and 3.141L is long double type.
[82]
Q10: What is the output of the following code
segment?
National Institute of Science & Technology
int main()
{
int x=4;
printf("%d ",4>>4);
printf("%d ",4<<4);
printf("%d ",x<<x>>x<<2<<x>>2);
printf("%d ",x);
}
Options:
1: 0 64 64 4
2: FBABD 0 1 2 3 4
3: 0 0 1 2 3 4
4: Error
[83]
National Institute of Science & Technology
Option 10 64 64 4
Explanation:
3rd printf is L to R
X<<x is 4<<4 is 64
64>>x 64>>4 is 4
4<<2 is 16
16<<x is 16<<4 is 256
256 >>2 is 64
[84]
Q11: What is the output of the following code
segment?
National Institute of Science & Technology
#include<stdio.h>
int main()
{
int x=5,y;
y=~x;
printf("%d",y);
return 0;
}
Options:
1: -5
2: -6
3: 0
4: 5
[85]
National Institute of Science & Technology
Option 2: -6
Explanation:
Bitwise 1‟s complement of 5 ( i.e., 0101 ) is
stored in y as 1010. [ left most bit is 1 so it is
sign bit].
Since the sign bit is set to 1 here so while
retrieving the value we have to find the 2‟s
compliment to get the no.
1010 „s 2complement is 0101+1 = 0110 (which is
6) since it is negative no hence -6 is printed.
[86]
Q12: What is the output of the following code
segment?
National Institute of Science & Technology
#include<stdio.h>
main(){
int i =0;
i = (2+3, 4>3, 3);
printf("%d", i);
}
Options:
1: 1
2: 5
3: 3
4: Error
[87]
National Institute of Science & Technology
Option 3: 3
Explanation:
In the usage of comma operator, evaluation is
from the left to right, when brackets are there the
evaluation is from right to left
[88]
Q13: What is the output of the following program?
National Institute of Science & Technology
#include<stdio.h>
int main()
{
printf("%d",sizeof(5.2));
}
Options:
A. Compiler Error: Can't determine size of a constant
B. 4 (Size of float)
C. 8 (Size of double)
D. Garbage Value
[89]
National Institute of Science & Technology
Correct answer is : C
Explanation The default type for decimal
constants is double and not float.
Hence the value 5.2 is treated as a double and 8
is printed.
[90]
Q14: What is the output of the following program?
National Institute of Science & Technology
#include<stdio.h>
int main(){
int x=4+2 % -8;
printf("%d",x);
x=4+ -2 % 8;
printf("%d",x);
return 0;
}
Options:
A. 6 2
B. 4 -2
C. 6 -2
D. -4 12
[91]
National Institute of Science & Technology
Correct answer is : A : 6 2
The operator % is higher precendence than +
[92]
Question-15: What is the output of the
following code snippet?
National Institute of Science & Technology
#define x 5+2
int main(){
int i;
i=x*x*x;
printf("%d",i);
}
[93]
National Institute of Science & Technology
ANS: 27
[94]
Question-16: What is the output of the
following code snippet?
National Institute of Science & Technology
int main(){
char c=125;
c=c+10;
printf("%d",c);
}
[95]
Answer 16: (explanation if any):
National Institute of Science & Technology
• Ans: -121
[96]
Question 17: What is the output of the
following code snippet?
National Institute of Science & Technology
int main(){
float a=5.2;
if(a==5.2)
printf("Equal");
else if(a<5.2)
printf("Less than");
else
printf("Greater than");
}
[97]
Answer -17(explanation if any):
National Institute of Science & Technology
[98]
Question-18: What is the output of the
following code snippet?
National Institute of Science & Technology
int main(){
float a=4.5;
if(a==4.5)
printf("Equal");
else if(a<4.5)
printf("Less than");
else
printf("Greater than");
}
[99]
National Institute of Science & Technology
[100]
Question-19: What is the output of the
following code snippet?
National Institute of Science & Technology
[101]
National Institute of Science & Technology
[102]
Question-20: What is the output of the
following code snippet?
National Institute of Science & Technology
int main()
{
char *str="Hello world";
printf("%d",printf("%s",str));
}
[103]
National Institute of Science & Technology
Answer-20(explanation if any):
Output - Helloworld11
[104]
Question-21: A Simple prediction
National Institute of Science & Technology
int y = 2,
z = (y++, ++y);
printf("%d\n", z);
[105]
National Institute of Science & Technology
Answer-21(explanation if any):
Answer: 4
[106]
Question-22: Simple prediction
National Institute of Science & Technology
#include <stdio.h>
int main()
{
int a = 2 + 4 + 3 * 5 / 3 - 5;
printf("%d", a);
}
[107]
National Institute of Science & Technology
Answer-22(explanation if any):
ANSWER 6
[108]
Question-23: A simple prediciton
National Institute of Science & Technology
#include <stdio.h>
int main()
{
int a = 5 * 3 % 6 - 8 + 3;
printf("%d", a);
}
[109]
National Institute of Science & Technology
Answer-23(explanation if any):
ANSWER -2
[110]
Question-24: output ??
#include <stdio.h>
National Institute of Science & Technology
int main()
{
int b = 6;
int c = 7;
int a = ++b + c--;
printf("%d", a);
}
[111]
National Institute of Science & Technology
Answer(explanation if any):
ANSWER - 14
[112]
Question-25: Output??
#include <stdio.h>
National Institute of Science & Technology
int main()
{
double b = 5 % 3 & 4 + 5 * 6;
printf("%lf", b);
}
[113]
National Institute of Science & Technology
Answer(explanation if any):
ANSWER 2.0000000
The precedence of operators is
*
%
+
&
[114]
Question-26:
#include <stdio.h>
National Institute of Science & Technology
int main()
{
double b = 3 && 5 & 4 % 3;
printf("%lf", b);
}
[115]
National Institute of Science & Technology
Answer(explanation if any):
Output: 1.0000000000
[116]
National Institute of Science & Technology
[117]
Question 27: Predict the output??
National Institute of Science & Technology
#include <stdio.h>
main()
{
int sum = 17, count = 5;
double mean;
[118]
National Institute of Science & Technology
Answer:
Output:
Value of mean(double) sum / count = 3.4000000 Value of
mean =(double) ( sum / count)= 3.00000
[119]
Question 28 – SIMPLE guess!
National Institute of Science & Technology
#include<stdio.h>
int main()
{
int j=1;
j+= j+= +j++;
printf("%d ", j);
return 0;
}
[120]
National Institute of Science & Technology
• Output: 5
[121]
QUESTION-29
What is the output?
National Institute of Science & Technology
#include<stdio.h>
int main()
{
printf("%d %d %d ", sizeof(3.14f), sizeof
(3.14), sizeof(3.141L));
return 0;
}
[122]
• Output: 4 8 12
National Institute of Science & Technology
[123]
QUESTION -30 – What is the output?
National Institute of Science & Technology
#include<stdio.h>
int main(main)
{
int k;
if(printf("%o",k=(~2|3) >-0? 7:8<<2));
return 0;
}
[124]
• Output: 30
• =(~2|3) >-0 generates FALSE hence 8<<2 is 32 and its
National Institute of Science & Technology
equivalent octal is 40
•
[125]
National Institute of Science & Technology
[126]
There are two files attached to this device:
National Institute of Science & Technology
[127]
gets and puts functions:
char *gets (char *s);
National Institute of Science & Technology
example is:
#include <stdio.h>
int main()
{
char s[200];
printf("\nPlease input a character string and press ENTER\n");
gets(s);
printf("\nThe character string is\n");
puts(s);
return 0;
}
[128]
scanf ( )
The scanf function allows you to accept input from standard in i.e.,
National Institute of Science & Technology
keyboard.
Format of scanf is:
format describes the format of the input and the variables are the
pointers to the variables in which the input will be stored..
[129]
Type Conversion
specifier
. Char %c
National Institute of Science & Technology
[130]
The. prototype for scanf is located in the header file named stdio.h, and
is as follows:
National Institute of Science & Technology
[131]
Example program to show the reading input with the floating
point conversion specifiers
National Institute of Science & Technology
#include<stdio.h>
int main()
{
double a,b,c;
printf("enter three floating point numbers \n");
scanf("%le%lf%lg",&a,&b,&c);
printf("numbers entered in plain floating point notation\n ");
printf("%f\n%f\n%f\n",a,b,c);
return 0;
}
Output:
enter three floating point numbers
1.23456
1.23456e+03
3.15152e-06
numbers entered in plain floating point notation
1.234560
.
1234.560000
0.000003
[132]
A scan set scans the characters in the input stream , looking only for
National Institute of Science & Technology
those characters that match the characters contained in the scan set,
if match occurs then stored else not.
#include<stdio.h>
int main()
{
char c;
char y[10];
printf(“enter a string \n”);
scanf("%[aeiou]s",y);
printf("char is %s ",y);
return 0;
}
Output:
Enter a string : aaeeppiioouu
char is aaee
[133]
printf( )
National Institute of Science & Technology
[134]
int x=12345;
float z=123.45;
National Institute of Science & Technology
[135]
The program shows the format specifier for the floating variable -ve
number so left justifed value will come here
National Institute of Science & Technology
#include<stdio.h>
#include<stdlib.h>
int main()
{
float z=123.45f;
printf("%-f\n",z); // note: 123.44997 will not come here.
printf("%-9.0f\n",z); // total 9 spaces, 0 space for decimal
printf("%-9.1f\n",z); // total 9 spaces, 1 space for decimal
printf("%-9.2f\n",z); // total 9 spaces, 2 space for decimal
printf("%-9.3f\n",z); // total 9 spaces, 3 space for decimal
return 0;
}
[136]
float neg=-123.4500f;
National Institute of Science & Technology
float pos=123.4500f;
printf("%f\n",neg);
printf("%e\n",neg);
printf("%f\n",pos);
printf("%e\n",pos);
[137]
Program to show the various output examples
printf("%5d\n",123);
printf("%-5d\n",123);
National Institute of Science & Technology
printf("%05d\n",123);
printf("%x\n",123u);
printf("%#x\n",123u);
printf("|%-10.2f|\n",12.3333);
printf("|%-10.3f|\n",12.3333);
printf("|%-10.4f|\n",12.3333);
printf("|%10.2f|\n",12.3333);
printf("|%10.3f|\n",12.3333);
printf("|%10.4f|\n",12.3333);
printf("|%+12.2f|\n",-93.2);
printf("|%0+12.2f|",-93.2);
[138]
National Institute of Science & Technology
Thank you ..
[139]