Unit – II Constants Variables DataTypesInC Operators Expressions Input Output
Unit – II Constants Variables DataTypesInC Operators Expressions Input Output
Unit – II
Constants, Variables, Data Types in C, Operators and Expressions, Input-Output functions in C
Presented by
Asmatullah Khan
© A S M A T U L L A H K H A N
2
Unit – II Contents
1. Describe character set, C Tokens-Keywords, 9. Illustrate type conversion techniques.
Variables, Identifiers, Constants and Variables.
2. Define Data type. Classify data types and explain 10. Define and List Pre-processor
with examples. directives.
3. Explain declaration of a variable and assigning
11. Discuss #include and #define Pre-
values to variables.
processor directives.
4. Define operator.
5. Classify and Explain operators with examples.
12. Define Header file and discuss stdio.h,
conio.h, math.h, string.h Header files.
6. Describe precedence and associativity of operators.
7. Define expression. 13. Illustrate getchar(), putchar(), scanf(),
printf() with programs.
8. Describe evaluation of Arithmetic, Relational and
logical expressions.
© A S M A T U L L A H K H A N
Describe character set, C Tokens-Keywords, 3
© A S M A T U L L A H K H A N
Describe character set, C Tokens-Keywords, Variables,
4
© A S M A T U L L A H K H A N
Describe character set, C Tokens-Keywords, Variables,
5
© A S M A T U L L A H K H A N
Describe character set, C Tokens-Keywords, Variables,
6
• Digits
• C language supports 10 digits which are used to construct numerical values in C language.
• Digits - 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
• Special Symbols
• C language supports a rich set of special symbols that include symbols to perform mathematical operations, to
check conditions, white spaces, backspaces, and other special symbols.
• Special Symbols - ~ @ # $ % ^ & * ( ) _ - + = { } [ ] ; : ' " / ? . > , < \ | tab newline space NULL bell backspace verticaltab
etc.,
© A S M A T U L L A H K H A N
7
© A S M A T U L L A H K H A N
8
#include<stdio.h>
#include<conio.h>
int main()
{
int i;
clrscr();
printf("ASCII ==> Character\n");
for(i = -128; i <= 127; i++)
printf("%d ==> %c\n", i, i);
getch();
return 0;
}
© A S M A T U L L A H K H A N
Describe character set, C Tokens-Keywords, 9
© A S M A T U L L A H K H A N
Describe character set, C Tokens-Keywords,, Identifiers, 11
• C Tokens
• C Keywords
• Properties of Keywords
1. All the keywords in C programming
language are defined as lowercase
letters so they must be used only in
lowercase letters
2. Every keyword has a specific
meaning, users can not change that
meaning.
3. Keywords can not be used as user-
defined names like variable,
functions, arrays, pointers, etc...
© A S M A T U L L A H K H A N
Describe character set, C Tokens-Keywords, 12
© A S M A T U L L A H K H A N
Describe character set, C Tokens-Keywords, 13
© A S M A T U L L A H K H A N
Describe character set, C Tokens-Keywords, 14
• A decimal integer value is specified as direct integer value O76 -----> Octal Integer Constant
whereas octal integer value is prefixed with 'o' and
hexadecimal value is prefixed with 'OX'. OX3A -----> Hexa Decimal Integer Constant
• An integer constant can also be unsigned type of integer 50u -----> Unsigned Integer Constant
constant or long type of integer constant.
30l -----> Long Integer Constant
• Unsigned integer constant value is suffixed with 'u' and
100ul -----> Unsigned Long Integer Constant
• long integer constant value is suffixed with 'l’
• whereas unsigned long integer constant value is suffixed with
'ul'. © A S M A T U L L A H K H A N
Describe character set, C Tokens-Keywords, 16
© A S M A T U L L A H K H A N
Describe character set, C Tokens-Keywords, 17
• C Constants 'A’
'2’
• Character Constants '+'
• A character constant is a symbol enclosed in single quotation.
• A character constant has a maximum length of one character.
• There are some predefined character constants called escape sequences.
• Every escape sequence has its own special functionality and every escape sequence is prefixed
with '\' symbol.
• These escape sequences are used in output function called 'printf()'.
© A S M A T U L L A H K H A N
Describe character set, C Tokens-Keywords, 18
© A S M A T U L L A H K H A N
Describe character set, C Tokens-Keywords, 21
Assignment - 1
1. Describe C Language character set in detail.
2. What are Tokens in C Language?
3. Define Keywords. List any 6 of them.
4. What are Identifiers? Give rules to create an identifier name.
5. How many types of Constants are there in C Language? Give
examples for each of its types.
6. What is a Variable in C Language? Give an example.
© A S M A T U L L A H K H A N
Define Data type. Classify data types and 23
Classification
signed short int
Data Types
Basic Data
Types long int
Integer
• Primary data types (Primary
Data Types)
int
unsigned short int
• Character data type float long int
Floating double
• The character data type is a set of characters Point
long double
enclosed in single quotations.
© A S M A T U L L A H K H A N
Define Data type. Classify data types and explain
26
Output:
value of char a : A Press any key to continue . . .
#include"stdio.h" #include"stdio.h"
Output: Output:
value of char a : C Press any key to continue . . . value of char a : P Press any key to continue . . .
Program for unsigned char data type Program for signed char data type
© A S M A T U L L A H K H A N
Define Data type. Classify data types and explain
28
Classification
signed short int
Data Types
Basic Data
Types long int
• Primary data types (Primary Integer
int
Data Types)
• Integer Data type unsigned short int
float long int
• The integer data type is a set of whole numbers. Floating double
Point
• Every integer value does not have the decimal value. long double
• We use the keyword "int" to represent integer data
type in C.
• We use the keyword int to
1. declare the variables and
2. to specify the return type of a function.
• The integer data type is used with different type
modifiers like short, long, signed and unsigned.
© A S M A T U L L A H K H A N
Define Data type. Classify data types and explain
29
Output:
Output:
value of int a : 22 Press any key to continue . . .
value of unsigned int a : 45 Press any key to continue . . .
Program for unsigned int data type Program for int data type
© A S M A T U L L A H K H A N
Define Data type. Classify data types and explain
31
#include"stdio.h" #include"stdio.h"
Output: Output:
value of short a : 15 Press any key to continue . . . value of unsigned short a : 12 Press any key to continue . . .
Program for short data type Program for unsigned short data type
© A S M A T U L L A H K H A N
Define Data type. Classify data types and explain
32
#include"stdio.h" #include"stdio.h"
Output: Output:
value of long a : 19898 Press any key to continue . . . value of unsigned long a : 78876 Press any key to continue . . .
Program for long data type Program for unsigned long data type
© A S M A T U L L A H K H A N
Define Data type. Classify data types and explain
33
Classification
signed short int
Data Types
Basic Data
Types long int
• Primary data types (Primary Integer
int
Data Types)
• Floating Point data types unsigned short int
float long int
• Floating-point data type is used to represent a set of Floating
numbers with the decimal value. double
Point
long double
• Every floating-point value must contain the decimal
value.
• The floating-point data type has two variants...
1. float
2. double
• We use the keyword "float" to represent floating-point
data type and "double" to represent double data type in
C.
• The float value contains 6 decimal places whereas
double value contains 15 or 19 decimal places.
© A S M A T U L L A H K H A N
Define Data type. Classify data types and explain
34
Output:
Output:
value of float a : 22.900000 Press any key to continue . . .
value of double a : 232.600000 Press any key to continue . . .
Program for float data type Program for double data type
© A S M A T U L L A H K H A N
Define Data type. Classify data types and explain
35
© A S M A T U L L A H K H A N
37
Assignment - 2
1. What is a Data type in C language? Mention its types.
2. List and explain character data types with a suitable example.
3. List and explain integer data types with a suitable example.
4. List and explain floating-point data types with a suitable example.
5. Mention the following parameters of various primary Data types
a. Keyword used denote the Data type
b. Memory size allocated
c. Range of values
d. Format type specifier
e. Type Modifiers
© A S M A T U L L A H K H A N
Explain declaration of a variable and 38
© A S M A T U L L A H K H A N
Explain declaration of a variable and 39
A variable that is A variable that is A variable that is declared with All variables in C that are We can share a variable in
declared inside declared outside the the static keyword is called static declared inside the block, are multiple C source files by
the function or function or block is variable. automatic variables by default. using an external variable.
block is called a called a global
local variable. variable. To declare an external
It retains its value between We can explicitly declare an
multiple function calls. automatic variable using auto variable, you need to
It must be
Any function can keyword. use extern keyword.
declared at the
start of the block,
change the value of If you call this function many
and initialize the the global variable. times, the local variable will
local variable print the same value for each
before it is used It is available to all function call, e.g, 11,11,11 and so
the functions. on.
© A S M A T U L L A H K H A N
Explain declaration of a variable and 41
1. local variable 2. global variable 3. static variable 4. automatic variable 5. external variable
void function1() int value=20; void function1() void main() //Create a file myfile.h
{ //global variable { { extern int x=10;
int x=10; int x=10; int x=10; //external variable (also global)
// void function1() //local variable //
local variabl { local variable (also automatic) //Create a file program1.c
e int x=10; static int y=10; #include "myfile.h"
} //local variable //static variable auto int y=20; #include <stdio.h>
} //automatic variable
x=x+1; } void printValue()
y=y+1; {
printf("%d,%d",x,y); printf("Global variable: %d", global_vari
able);
} }
© A S M A T U L L A H K H A N
42
Assignment - 3
1. What is a variable in C Language?
2. List rules for declaring a variable.
3. How many types of variables are there in C Language?
4. Define Local Variable. Give an example.
5. Define Global Variable. Give an example.
6. Define Static Variable. Give an example.
7. Define Extern Variable. Give an example.
8. Define Automatic Variable. Give an example.
© A S M A T U L L A H K H A N
43
Define operator
• An operator is a symbol used to perform mathematical, relational comparison, and logical
operations in a program.
• C programming language supports a rich set of 8 operators that are classified as follows:
1. Arithmetic Operators
2. Relational Operators
3. Logical Operators
4. Increment & Decrement Operators
5. Assignment Operators
6. Bitwise Operators
7. Conditional Operator
8. Special Operators
© A S M A T U L L A H K H A N
Classify and Explain operators with examples 44
cont…
• Arithmetic Operators (+, -, *, /, %)
• The arithmetic operators are the symbols that are used to perform basic mathematical operations
like addition, subtraction, multiplication, division and percentage modulo.
• The addition operator can be used with numerical data types and character data type.
• When it is used with numerical values, it performs mathematical addition and
• When it is used with character data type values, it performs concatenation (appending).
• The remainder of the division operator is used with integer data type only.
Operator Meaning Example
+ Addition 10 + 5 = 15
- Subtraction 10 - 5 = 5
* Multiplication 10 * 5 = 50
/ Division 10 / 5 = 2
% Remainder of the Division 5%2=1
© A S M A T U L L A H K H A N
Classify and Explain operators with examples 45
cont…
• Relational Operators (<, >, <=, >=, ==, !=)
• The relational operators are the symbols that are used to compare two values.
• Every relational operator has two results TRUE or FALSE.
• In simple words, the relational operators are used to define conditions in a
program.
Operator Meaning Example
< Returns TRUE if the first value is smaller than second value otherwise returns FALSE 10 < 5 is FALSE
> Returns TRUE if the first value is larger than second value otherwise returns FALSE 10 > 5 is TRUE
<= Returns TRUE if the first value is smaller than or equal to second value otherwise returns FALSE 10 <= 5 is FALSE
>= Returns TRUE if the first value is larger than or equal to second value otherwise returns FALSE 10 >= 5 is TRUE
== Returns TRUE if both values are equal otherwise returns FALSE 10 == 5 is FALSE
!= Returns TRUE if both values are not equal otherwise returns FALSE 10 != 5 is TRUE
© A S M A T U L L A H K H A N
Classify and Explain operators with examples 46
cont…
• Logical Operators (&&, ||, !)
• The logical operators are the symbols that are used to combine multiple conditions
into one condition.
• Logical AND - Returns TRUE only if all conditions are TRUE, if any of the conditions is FALSE
then complete condition becomes FALSE.
• Logical OR - Returns FALSE only if all conditions are FALSE, if any of the conditions is TRUE
then complete condition becomes TRUE.
Operator Meaning Example
&& Logical AND - Returns TRUE if all conditions are TRUE otherwise returns FALSE 10 < 5 && 12 > 10 is FALSE
|| Logical OR - Returns FALSE if all conditions are FALSE otherwise returns TRUE 10 < 5 || 12 > 10 is TRUE
! Logical NOT - Returns TRUE if condition is FLASE and returns FALSE if it is !(10 < 5 && 12 > 10) is TRUE
TRUE
© A S M A T U L L A H K H A N
Classify and Explain operators with examples 47
cont…
• Increment & Decrement Operators (++ & --)
• The increment operators adds one to the existing value of the operand and the decrement operator
subtracts one from the existing value of the operand.
• The increment and decrement operators are called unary operators because both need only one operand.
• The increment and decrement operators are used infront of the operand (++a) or after the operand (a++).
• If it is used infront of the operand, we call it as pre-increment or pre-decrement and
• If it is used after the operand, we call it as post-increment or post-decrement.
© A S M A T U L L A H K H A N
Classify and Explain operators with examples 48
cont…
• Increment & Decrement Operators (++ & --) Example Program
Output:
© A S M A T U L L A H K H A N
Classify and Explain operators with examples 49
cont…
• Increment & Decrement Operators (++ & --) Example Program
Output:
© A S M A T U L L A H K H A N
Classify and Explain operators with examples 50
cont…
• Assignment Operators (=, +=, -=, *=, /=, %=)
• The assignment operators are used to assign right-hand side value (Rvalue) to the left-hand
side variable (Lvalue).
• The assignment operator is used in different variants along with arithmetic operators.
Operator Meaning Example
= Assign the right-hand side value to left-hand side variable A = 15
+= Add both left and right-hand side values and store the result into left-hand side variable A += 10
⇒ A = A+10
-= Subtract right-hand side value from left-hand side variable value and store the result into left-hand side variable A -= B
⇒ A = A-B
*= Multiply right-hand side value with left-hand side variable value and store the result into left-hand side variable A *= B
⇒ A = A*B
/= Divide left-hand side variable value with right-hand side variable value and store the result into the left-hand side A /= B
variable ⇒ A = A/B
%= Divide left-hand side variable value with right-hand side variable value and store the remainder into the left-hand A %= B
side variable ⇒ A = A%B
© A S M A T U L L A H K H A N
Classify and Explain operators with examples 51
cont…
• Bitwise Operators (&, |, ^, ~, >>, <<)
• The bitwise operators are used to perform bit-level operations in the c programming language.
• When we use the bitwise operators, the operations are performed based on the binary values.
• The following table describes all the bitwise operators in the C programming language. Let us consider two variables
A and B as A = 25 (11001) and B = 20 (10100).
^ the result of Bitwise XOR is 0 if all the bits are same otherwise it is 1 A ^ B ⇒ 13 (01101)
~ the result of Bitwise once complement is negation of the bit (Flipping) ~A ⇒ 6 (00110)
<< the Bitwise left shift operator shifts all the bits to the left by the specified number of positions A << 2 ⇒ 100 (1100100)
>> the Bitwise right shift operator shifts all the bits to the right by the specified number of positions A >> 2 ⇒ 6 (00110)
© A S M A T U L L A H K H A N
Classify and Explain operators with examples 52
cont…
• Conditional Operator (?:)
• The conditional operator is used for decision making.
• It is also called a ternary operator because it requires three operands.
• In this operator, first we verify a condition, then we perform one operation out
of the two operations based on the condition result.
• if the condition is TRUE the first option is performed,
• if the condition is FALSE the second option is performed.
• The conditional operator is used with the following syntax.
Condition ? TRUE Part : FALSE
Part;
Example
A = (10<15)?100:200; ⇒ A value is 100
© A S M A T U L L A H K H A N
Classify and Explain operators with examples 53
cont…
• Special Operators (sizeof, pointer, comma, dot, etc.)
• The following are the special operators in c programming language.
1. sizeof operator sizeof(variableNam
e);
• This operator is used to find the size of the memory (in bytes) Example
allocated for a variable. sizeof(A);
⇒ the result is 2 if A is an integer
2. Pointer operator (*)
• This operator is used to define pointer variables in c programming
language.
3. Comma operator (,)
• This operator is used to separate variables while they are declaring,
separate the expressions in function calls, etc.
4. Dot operator (.)
• This operator is used to access members of structure or union.
© A S M A T U L L A H K H A N
Describe precedence and associativity of 54
operators
• Precedence of Operators in C
• The precedence of operator species that which operator will be evaluated first
and next.
• Associativity in Operators in C
• The associativity specifies the operator direction to be evaluated;
• It may be left to right or right to left.
• Example
int value=10+20*10;
The value variable will contain 210 because * (multiplicative operator) is evaluated before + (additive operator).
© A S M A T U L L A H K H A N
Describe precedence and associativity of operators 55
cont…
Precedence Operator Operator Meaning Associativity
() function call
[] array reference
1 Left to Right
-> structure member access
. structure member access
! negation
~ 1's complement
+ Unary plus
- Unary minus
++ increment operator
2 Right to Left
-- decrement operator
& address of operator
* pointer
sizeof returns size of a variable
(type) type conversion
© A S M A T U L L A H K H A N
Describe precedence and associativity of operators 56
cont…
Precedence Operator Operator Meaning Associativity
* multiplication
3 / division Left to Right
% remainder
+ addition
4 Left to Right
- subtraction
<< left shift
5 Left to Right
>> right shift
< less than
<= less than or equal to
6 Left to Right
> greater than
>= greater than or equal to
== equal to
7 Left to Right
!= not equal to
© A S M A T U L L A H K H A N
Describe precedence and associativity of operators 57
cont…
Precedence Operator Operator Meaning Associativity
8 & bitwise AND Left to Right
bitwise EXCLUSIVE
9 ^ Left to Right
OR
10 | bitwise OR Left to Right
11 && logical AND Left to Right
12 || logical OR Left to Right
© A S M A T U L L A H K H A N
Describe precedence and associativity of operators 58
cont…
Precedence Operator Operator Meaning Associativity
= assignment
*= assign multiplication
/= assign division
%= assign remainder
+= assign addition
14 -= assign subtraction Right to Left
&= assign bitwise AND
^= assign bitwise XOR
|= assign bitwise OR
<<= assign left shift
>>= assign right shift
Assignment - 4
1. Define Operator. Mention its types.
2. Define associativity and precedence of an operator.
3. Explain Arithmetic operators along with their precedence and associativity.
4. Explain Relational operators along with their precedence and associativity.
5. Explain Logical operators along with their precedence and associativity.
6. Explain Bit-wise operators along with their precedence and associativity.
7. What is Ternary operator? Give an example.
8. What is comma operator? Give an example.
9. Explain Increment ++ and Decrement -- operators.
10. What is sizeof operator? Give an example.
© A S M A T U L L A H K H A N
60
Define expression
• An expression is an executable statement or an instruction formed using collection
of operators and operands that represents a specific value.
• Operator is a symbol that performs tasks like arithmetic operations, logical operations, and
conditional operations, etc.
• Operands are the values on which the operators perform the task.
• An expression is a formula in which operands are linked to each other by the use of
operators to compute a value.
• Based on the operator position, the expressions are divided into THREE types.
1. Infix Expression
2. Postfix Expression
3. Prefix Expression
© A S M A T U L L A H K H A N
61
© A S M A T U L L A H K H A N
Describe evaluation of Arithmetic, Relational 62
© A S M A T U L L A H K H A N
Describe evaluation of Arithmetic, Relational 63
© A S M A T U L L A H K H A N
Describe evaluation of Arithmetic, Relational 64
© A S M A T U L L A H K H A N
Describe evaluation of Arithmetic, Relational 66
© A S M A T U L L A H K H A N
Describe evaluation of Arithmetic, Relational 68
© A S M A T U L L A H K H A N
Describe evaluation of Arithmetic, Relational 69
© A S M A T U L L A H K H A N
Describe evaluation of Arithmetic, Relational 70
Assignment - 5
1. What is an Expression in C Language?
© A S M A T U L L A H K H A N
72
• Example:
• In EXPRESSIONS such as, Multiplication of an integer data value with float data value and storing the result into a float
variable, the integer value must be converted to float value so that the final result is a float datatype value.
• In a c programming language, the data conversion is performed in two different methods as follows...
1. Type Conversion
2. Type Casting
© A S M A T U L L A H K H A N
Illustrate type conversion techniques 73
cont…
1. Type Conversion
• The type conversion is the process of converting a data value from one data type to another data
type automatically by the compiler.
• Type conversion is also called implicit type conversion.
• Example:
int i = 10 ;
float x = 15.5 ;
char ch = 'A' ;
i = x ; =======> x value 15.5 is converted as 15 and assigned to variable i
x = i ; =======> Here i value 10 is converted as 10.000000 and assigned to variable x
i = ch ; =======> Here the ASCII value of A (65) is assigned to i
© A S M A T U L L A H K H A N
Illustrate type conversion techniques 74
cont… #include<stdio.h>
1. Type Conversion #include<conio.h>
void main(){
int i = 95 ;
float x = 90.99 ;
char ch = 'A' ;
i=x;
printf("i value is %d\n", i);
x=i;
printf("x value is %f\n", x);
i = ch ;
printf("i value is %d\n", i);
}
© A S M A T U L L A H K H A N
Illustrate type conversion techniques 75
cont…
2. Typecasting
• Compiler converts data from one data type to another data type implicitly.
• When compiler converts implicitly, there may be a data loss.
• USER can also convert the data from one data type to another data type using explicit type
conversion.
• To convert data from one type to another type we specify the target data type in parenthesis as a
prefix to the data value that has to be converted.
• To perform this we use the unary cast operator.
• Typecasting is also called as explicit type conversion.
• The general syntax of typecasting is as follows:
(TargetDatatype) DataValue
© A S M A T U L L A H K H A N
Illustrate type conversion techniques 76
cont… #include<stdio.h>
#include<conio.h>
2. Typecasting
• Example void main(){
int a, b, c ;
int totalMarks = 450, maxMarks = 600 ;
float avg ;
float average ;
printf( "Enter any three integer values : “);
average = (float) totalMarks / maxMarks * 100 ; scanf(“%d%d%d”, a, b, c);
avg = (a + b + c) / 3 ;
printf("avg before casting = %d\n“, avg);
avg = (float)(a + b + c) / 3 ;
printf( "avg after casting = %d\n“, avg);
return 0;
}
© A S M A T U L L A H K H A N
77
Assignment - 6
1. Differentiate between Type-casting and Type-convertion
© A S M A T U L L A H K H A N
78
© A S M A T U L L A H K H A N
Define and List Pre-processor directives 79
cont…
• List of Pre-processor directives • List of Pre-processor directives
1. #undef - used to destroy a macro that was already 6. #elif - is a #else followed by #if in one statement.
created using #define.
7. #endif - is used terminate preprocessor conditional
2. #ifdef - returns TRUE if the macro is defined and macro.
returns FALSE if the macro is not defined.
8. #include - is used to insert specific header file into
3. #ifndef - returns TRUE if the specified macro is not C program.
defined otherwise returns FALSE.
9. #error - is used to print error message on stderr.
4. #if - uses the value of specified macro for
10. #pragma - is used to issue a special command to
conditional compilation.
the compiler.
5. #else - is an alternative for #if.
© A S M A T U L L A H K H A N
Discuss #include and #define Pre-processor 80
directives
• C #include
• The #include preprocessor directive is used to paste code of given in a header-file into current
source code file. It is used to include system-defined and user-defined header files.
• If included file is not found, compiler renders error.
• There are TWO variants to use #include directive.
1. #include <filename>
2. #include "filename"
• The #include <filename> tells the compiler to look for the directory where system header files
are held.
• In UNIX, it is \usr\include directory.
• The #include "filename" tells the compiler to look in the current directory from where program
is running.
© A S M A T U L L A H K H A N
Discuss #include and #define Pre-processor 81
directives cont…
• C #include example
• Let's see a simple example of #include directive. In this program,
#include<stdio.h>
we are including stdio.h file because printf() function is defined in
this file int main()
• #include notes: {
• Note 1: In #include directive, comments are not recognized. printf("Hello C");
So in case of #include <a//b>, a//b is treated as filename.
• Note 2: In #include directive, backslash is considered as return 0;
normal text not escape sequence. So in case of #include <a\
nb>, a\nb is treated as filename.
}
• Note 3: You can use only comment after filename otherwise it
will give error.
© A S M A T U L L A H K H A N
Discuss #include and #define Pre-processor 82
directives cont…
• C #define
#include <stdio.h> #include <stdio.h>
• The #define
preprocessor
directive is used to #define PI 3.14 #define MIN(a,b) ((a)<(b)?(a):(b))
define constant or
micro substitution. main() void main()
It can use any basic { {
data type.
printf("%f",PI); printf("Minimum between 10 and 20
• Syntax } is: %d\n", MIN(10,20));
#define token valu }
e
Output: Output:
3.140000 Minimum between 10 and 20 is: 10
© A S M A T U L L A H K H A N
83
Assignment - 7
1. What is a Preprocessor directive? List any five of them.
2. What is #include?
3. What is a macro?
4. What is #define? Give an example.
© A S M A T U L L A H K H A N
Define Header file and discuss stdio.h, 84
© A S M A T U L L A H K H A N
Define Header file and discuss stdio.h, 85
© A S M A T U L L A H K H A N
Define Header file and discuss stdio.h, 86
© A S M A T U L L A H K H A N
Define Header file and discuss stdio.h, 87
© A S M A T U L L A H K H A N
Define Header file and discuss stdio.h, 88
Function Description
fseek() moves file pointer position to given location
SEEK_SET moves file pointer position to the beginning of the file
SEEK_CUR moves file pointer position to given location
SEEK_END moves file pointer position to the end of file.
f tell() gives current position of file pointer
rewind() moves file pointer position to the beginning of the file
sprint() writes formatted output to string
sscanf() Reads formatted input from a string
remove() deletes a file
fflush() flushes a file
© A S M A T U L L A H K H A N
Define Header file and discuss stdio.h, 89
© A S M A T U L L A H K H A N
Define Header file and discuss stdio.h, 90
© A S M A T U L L A H K H A N
Define Header file and discuss stdio.h, 91
© A S M A T U L L A H K H A N
Define Header file and discuss stdio.h, 92
© A S M A T U L L A H K H A N
Define Header file and discuss stdio.h, 93
© A S M A T U L L A H K H A N
Define Header file and discuss stdio.h, 94
© A S M A T U L L A H K H A N
Define Header file and discuss stdio.h, 95
© A S M A T U L L A H K H A N
Define Header file and discuss stdio.h, 96
© A S M A T U L L A H K H A N
97
Assignment -8
1. Define a Header File. List any three header files used in C
Language.
2. What is “stdio.h”? List any five built-in functions of it.
3. What is “conio.h”? List any five built-in functions of it.
4. What is “math.h”? List any five built-in functions of it.
5. What is “string.h”? List any five built-in functions of it.
© A S M A T U L L A H K H A N
Illustrate getchar(), putchar(), scanf(), printf() 98
with programs
• putchar(), getchar() function in C:
• putchar() function is a file handling function in C programming language which is used to write a character on
standard output/screen.
• getchar() function is used to get/read a character from keyboard input.
File operation Declaration & Description
© A S M A T U L L A H K H A N
Illustrate getchar(), putchar(), scanf(), printf() 99
1 #include <stdio.h>
2 int main()
3 {
4 char c;
5 printf("Enter some character. Enter $ to
6 exit...\n");
Output:
7 while (c != '$');
8 { Enter some character. Enter $ to exit…
9 c = getchar(); A
10 printf("\n Entered character is: "); Entered character is: A
11 putchar(c); B
12 printf("\n") Entered character is: B
13 } $
14 return 0; Entered character is: $
15 }
© A S M A T U L L A H K H A N
Illustrate getchar(), putchar(), scanf(), printf() 100
© A S M A T U L L A H K H A N
Illustrate getchar(), putchar(), scanf(), printf() 102
Output:
Hello! C Language !
There are 20 characters in above printf statement
© A S M A T U L L A H K H A N
Illustrate getchar(), putchar(), scanf(), printf() 103
Output:
© A S M A T U L L A H K H A N
105
Assignment - 9
1. Explain the following input output predefined functions with suitable examples
a. scanf()
b. printf()
c. getchar()
d. putchar()
© A S M A T U L L A H K H A N
106