Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
scanf(“control string”,&variable1,&variable2,….);
The control string contains format of the data being received. The ampersand symbol (&)before each variable
name is the operator that specifies variable name’s address. The use of & is must in scanf() function.
Ex: scanf(”%d”,&x);
When this statement is encountered, the execution of program stops and waits for the value of the variable x to
be typed in .The format %d specifies that an integer value is to be read from the terminal, the user has to type
the value in integer form. Once number typed and return key pressed, the next statement in the program is
executed.
Commonly used format specifications:
%c – read a single character.
%d – read a decimal Integer.
%e – read a floating point value in exponential form.
%f – read a floating point value.
%i – read a decimal, hexadecimal or octal Integer.
%h– read a short integer.
%x – read a hexadecimal integer (unsigned) using lower case a – f.
%X – read hexadecimal integer (unsigned) using upper case A – F.
%o – read an octal integer.
%s – read a string
%u –read unsigned decimal integer.
OUTPUT FUNCTION-printf()
printf() function in ‘C’ allows us to display output on console. Output data can be written from computer to
standard output device such as monitor. The general form of printf() is as follows
printf(“control string”,exp1,exp2,exp3,……);
The control string contains format of the data to be displayed and exp1, exp2, exp3…are output expressions.
The function accepts a series of arguments, each applying to a conversion specifier in the given control string,
printf() prints the formatted information to the standard output device, usually the display screen.
Token:- A Token is a smallest unit in a ‘C’ Program, which is similar to a word in the natural language text.
In ‘C’ Language these tokens are classified into following categories.
a. Keywords
b. Identifiers
c. Constants
d. White spaces
e. Operators
Keywords:- Keywords are one special type of tokens in ‘C’ Language, where each keyword will indicate
a specific task. ‘C’ Language provides the following list of 32 Keywords, each indicates a specific task. These
keywords are similar to the verbs in Natural languages. The task for every keyword is defined by the compiler
developer at the time of compiler development. The definition of every keyword will not be able to change by
the programmer or user. These are fixed.
Keyword list:-
auto double int struct
break else long switch
case enum register typedef
char extern return union
const float short unsigned
continue for signed void
default goto sizeof volatile
do if static while
Along with these keywords the following additional keywords are supported by the Turbo-C and Borland-
C compilers.
asm far interrupt pascal near huge cdecl
Identifier:-
In C programming, identifiers are names given to C entities, such as variables, functions, structures,
etc. identifiers are created to give unique name to C entities to identify it during the execution of the
program.
Rules to create an identifier in ‘C’ program:-
a. An identifier should contains only letters, digits and underscore symbol (_).
b. A keyword is not used as an identifier.
c. The first character in an identifier should be a letter or underscore (_) but not digit.
Integer constants:-
Integer constants are the numeric constants without any fractional part or exponential part. In ‘C’
Language we can represent integer constants in different number systems. 1. Decimal system 2. Octal
system 3. Hexadecimal system.
Note:
Every octal number in ‘C’ program will preceded by 0(zero).
Every hexadecimal number in ‘C’ program will preceded by 0x or 0X as shown in the following
examples.
Examples;
Decimal constants -- 99, -88, 123, etc.
Octal constants -- 04334, 0453, etc..
Hexadecimal constants -- 0x23 , 0X23, 0x34ab4.
Example:
#include<stdio.h>
void main()
{
int x = 0123;
int y = 012;
printf(“%d”,x+y);
}
The output of this program 93, because here x, y are initialized with octal constants. The sum of x, y
value will be converted into decimal number by “%d “ in printf() statement and then print on output device. If
we use “%o” instead of “%d” then it prints octal equivalent number of x+y (i.e 135) on output device.
Floating-point constants:-
Floating point constants are the numeric constants that have either fractional form or exponent form.
Exponent form is called as scientific notation. For example
Ex: float x = 3.14159;
float x = 314.159E-2;
float y = 0.0314159E+2;
NOTE:-
1. The general form for scientific notation is
Mantissa e
exponent
Character constants:-
If any ‘C’ alphabet is enclosed with 2 single quotes then it is called character constant in ‘C’ language.
Example:-
‘5’ , ‘a’, ‘A’, ‘ ‘, ….etc
Character constants have integer values known as ASCII values.
Example:
printf(“%d”,’B’);
It prints the 66. This 66 is the ASCII value of character ‘B’.
Every character in ‘C’ Language will contains a ASCII value, so that we can use ‘C’ character in
asthmatic operations.
1. Write a program to print all ASCII value of all characters.
#include<stdio.h>
void main()
{
int i=0;
for(i=0;i<256;i++)
{
printf(“\n%d ---- %c\n”,i,i);
}
}
String Constants:-
If a sequence of characters are enclosed with in double quotes then that is treated as string constant in
‘C’ Language. For example
“hello”, “123”, “x”, “1”, “23+23*45”.
Backslash Character Constants:-
‘C’ language provides few special backslash character constants, which are used in output functions.
For example a symbol ‘\t’ stands for tab space character. These backslash character constants are also known
as escape sequences. The following list of backslash characters are supported by ‘C’ programming language
to indicate specific individual task.
S.NO Constants Meaning
1 ‘\a’ Alert bell( beep sound)
2 ‘\b’ Back space
3 ‘\f’ Form feed
4 ‘\n’ New line
5 ‘\r’ Carriage return
6 ‘\t’ Horizontal tab
7 ‘\v’ Vertical tab
8 ‘\’’ Prints the Single quote on output device
9 ‘\” ’ Prints the Double quote on output device
10 ‘\?’ Prints the question mark on output device
11 ‘\\’ Prints the backslash on output device
12 ‘\0’ Null character
Variables:
Variables are memory locations in computer’s memory to store data. To indicate the memory location,
each variable should be given a unique name called identifier. Variable names are just the symbolic
representation of a memory location.
Example: sum, car_no, r_number, count etc.
Syntax for variable declaration statement in ‘C’:
Data_type identifier[,[=constants]identifier];
Example: int x, marks;
int x = 5, marks = 56;
Rules for writing variable names in C
1. Variable name can be composed of letters( both uppercase and lowercase letters), digits and
underscore(_) only.
2. The first letter of a variable should be either a letter or an underscore. But it is discouraged to start
variable name with an underscore though it is legal. It is because, variable name that starts with
underscore can conflict with system names and compiler may complain.
3. There is no rule for the length of a variable. However, the first 31 characters of a variable are
discriminated by the compiler. So, the first 31 letters of two variables in a program should be different.
NOTE:- In C programming, you have to declare variable before using it in the program.
Integers are whole numbers with a range of values supported by a particular machine. Generally,
integers occupy one word of storage, and since the word sizes of machines vary (like 16-bit processor or 32-
bit processor) the size of integer that can be stored depends on the computer. If we use a 16-bit word length,
the size of the integer is 2 bytes. If we use a 32-bit processor then the size of the integer is 4 bytes. Size and
ranges of different basic data types in C language are given in the following table.
Floating point numbers (real numbers) are stored in 32 bits( in all 16-bit and 32-bit machines), with 6 digits of
precision. When the accuracy provided by a float number is not sufficient then double can be used. A double
data type numbers uses 64 bits giving a precision of 14 digits.
Session-6 Pointers
Content:
Definition:Pointer is a variable that store the address of another variable.
Session-7 Files
Content:
A file is some reserved space on the disk where a group of related data is stored permanently in the system.
FILE OPERATIONS
The different basic file operations are
Naming a file
Creating / Opening a file
Closing a file
Writing data to a file
Reading data from a file
Appending a file
C supports a number of functions that have the ability to perform basic file operations, which includes:
FILE *file-pointer;
For example,
FILE *p1, *p2, *fp;
“filename” – is the name of the file and is a string of characters that make up a valid filename for the
operating system. It may contain two parts, a primary name and an optional period with the extension.
For example,
“input.data”
“STUDENT.DAT”
“RESULTS.OUT”
“mode” – specifies the purpose of opening the file. The mode of the file may be one of the following
“w” -- open a new file for writing data
“r” -- open an existing file for reading data
“a” -- open an existing file for appending data to it
“r+” -- open an existing file for both reading and writing data
“w+” -- same as “w” except both for reading and writing
“a+” -- same as “a” except both for reading and appending
Closing a file
A file must be closed as soon as all operations on it have been completed. The function fclose() is used to
close the file that has been opened earlier.
fclose( ) is the counterpart of fopen( ). Closing file means delinking the file from the program and saving
the contents of the file. Its general form is
fclose(fp);
This would close the file associated with the file pointer fp.
fprintf( ) :The function is used to write multiple data items which may (or may not) be of different
types to a file. Its general form is.
Where fp is a pointer to the file, “control-string” contains output specifications for the data to be written
and arg1, arg2 … are the arguments list. The function writes the data values of arg1, arg2… to the file
pointed by fp.
fscanf( ) :- This function is used to read multiple data items which may be of different types from a
file. Its general form is
The function reads data values in a given format from the file pointed by fp and returns the data values,
which are collected by the variable list.
Arithmetic operators:
C Language provides a set of operators to perform few basic arithmetic operations, which are
described in the following table
s.no Operator symbol Meaning
1 + Addition operator or unary addition operator
2 - Subtraction operator or unary subtraction operator
3 * Multiplication operator
4 / Division operator
5 % Modulo division operator
If any C expression having only arithmetic operators then we call it as arithmetic expression.
If all operands of any arithmetic expression are integers then that arithmetic expression is called as
integer arithmetic expression
If all operands of any arithmetic expression are real numbers then that arithmetic expression is called
as real arithmetic expression or floating point arithmetic expression.
If any expression has different data type operand then we call that expression as mixed mode
arithmetic expression.
Ex: void main()
{ int x=4,y=5;
float a,b;
a = 2.4;
b = 4.5;
printf(“\n%d”,x+y); integer arithmetic expression
printf(“\n%f”,a+b); floating point arithmetic expression
printf(“\n%f”,a+x); Mixed mode arithmetic expression
}
An integer arithmetic expression always gives integers and a real arithmetic expression gives floating
value.
We can use -,+,*,/ on any basic data type values, but % will not work on float or double data.
Ex: void main()
{
float a=2.0,b=3.1;
a = a%b; this statement will gives an error.
}
If number of operands of an operator is one then we call that operator as unary operator.
If any operator takes two operands then we call that operator as binary operator.
If 3 then we call it as ternary operator. In C language we have only one ternary operator.
In integer division operation decimal part will truncate.
During the integer division, if both operands are having the same sign then the result is truncated
towards the zero. If one of these two operands is negative then the direction of truncation is
implementation dependent.
Ex: x = -3/5; here x is either 0 or -1
During the modulo division the sign of the two operand are different then the result is always the sign
of the first operand
Ex: X = -3%5; then x becomes -3.
X = 3%-5; then x becomes 3.
Relational operators:-
C language provides six relational operators, which are used to compare the values of any two
variables or to compare the values of any two expressions. In C language all operators are binary operators.
The symbols which are used for relational operations and their meanings are described in the following table.
s.no Symbol Meaning of the operator Unary/binary Precedence Associativity
1 < Is less than Binary 1(high) Left-Right
2 > Is greater than Binary 1 Left-Right
3 <= Is less than or equal to Binary 1 Left-Right
4 >= Is greater than or equal to Binary 1 Left-Right
5 == Is equal to Binary 2 Left-Right
6 != Is not equal to binary 2(low) Left-Right
Relational operators are having less precedence than all arithmetic operators.
In relational operators <, >, <=, >= are having same precedence and similarly == , != are having same
precedence.
<, <=, >, >= are having high precedence than ==, != operators.
The expression in which we use only relational operators is called as Relational Expression.
A relational expression can return either 0 or 1 i.e. either false or true.
All relational operators are binary operators.
Logical operators:-
C language provides 3 basic logical operators which are used to connect two or more expressions.
Usually we use these operators in conditional statements to find the Boolean value of conditional expression.
The symbols and their meanings are shown in the following table.
Definitions:-
Op1 Op2 Op1 && Op2 Op1 || Op2 !Op1
Non-zero Non-zero 1 1 0
Non-zero Zero 0 1 0
Zero Zero 0 0 1
Zero Non-zero 0 1 1
Logical AND:- if both operands are non-zero values then only it returns 1 otherwise it returns 0.
Logical OR:- if both operands are zero values then only it returns zero otherwise it returns 1.
Logical NOT:- if the operand is non-zero value then it returns zero otherwise it returns 1
Conditional operator:
The operator symbol is ?: This is also called ternary operator. The conditional operator has three expressions.
The general form is
expression1?expression2:expresssion3
operand ++
where the operand must be a variable but cannot be constant or expression..++operand is called pre increment
or prefix increment and operand++ is called post increment or postfix increment
Example: pre incrementation
int i=20;
++i;
printf(“i=%d”,i);
output: i=21
Example: post incrementation
int i=20;
i++;
printf(i=%d”,i);
output:i=21;
In the above example prefix and postfix operation results in the same output. The prefix and postfix operations
differ in the value used for the operand when it is embedded inside expressions
Example: int x=10,y;
y=++x;
printf(“x=%d,y=%d”,x,y);
output: x=11,y=11
Here x value is incremented before it is assigned to y. So the expression y = ++x is equivalent to the two
expressions x=x+1 and y=x
Example: int a=5,b;
b=++a*4;
printf(“a=%d,b=%d”,a,b)
output: a=6,b=24
Example: int x=10,y;
y=x++;
printf(“x=%d,y=%d”);
output: x=11,y=10
Here value of x is incremented after it is assigned to y, thus the expression y=x++ is equivalent to the
expressions y=x and x=x+1;
Example: int a=5,b;
b=a++*4;
printf(“a=%d,b=%d”,a,b);
output: a=6,b=20
--operand
or
operand--
where the operand must be a variable but cannot be constant or expression.--operand is called pre decrement
or prefix decrement and operand-- is called post decrement or postfix decrement
Example:
int i=10;
--i;
printf(“i=%d”,i ) ;
output: i=9
Example: int i=10;
i--;
printf(“i=%d”,i);
output: i=9;
In the above example both the prefix and postfix operations results in the same value.
i) Simple assignment
The operator used is ‘=’.
The general form is
Variable name=expression;
The value of the expression is evaluated first and is assigned to the variable. The evaluation of the expression
is right to left.
Example:
x=10 a=10 && 5
y=12.5 a=x>y
a=”A”
area=0.5*b*h.
Note:
= is the assignment operator used to assign right side expression value to the left hand side variable.
= = is comparison operator to compare the value of left hand and right hand side expression.
a=b will assign b value to a
a==b will compare the values of a and b for equality.
The value of the expression is assigned to the variables v1, v2, v3 etc. the evaluation is right to left.
Example: a=b=0.
a=b=x+y
The value of x+y is assigned to b and then the value of b is assigned to a.
i) Comma operator
The comma operator is used to combine two more expressions into a single expression.
The general form is
expression1,expression2,---
,expressionN
The expression are evaluated from left to right and the value of the right most expression is the result of the
overall expression.
Example: int i=10,j;
j= ( i+=5, --i , i+20);
j is assigned the value 34.
where the operand may be name of the data type or constant or variable or expression.
Example: sizeof(13.56)
sizeof(239462l)
sizeof(896254ll)
sizeof(‘*’)
sizeof(a+b)
iii) address operator
General form is
&operand
Where the operand must be a variable name. This operator is used to determine the address of the memory
location allotted to a variable.
&x represent the address of the memory location allotted to the variable x and it is assigned to the pointer p.
p x
3472 10
3472
iv) Indirection operator
General form is
*operand
Where the operand must be a pointer.This operator is used to get the value at the address represented by the
pointer. This operator is also called de-referencing operator or value at address operator.
Example: int x=10,y,*p;
p=&x;
y=*p;
The value 10 of x is assigned to the variable y.
The expression y=*p is equivalent to the expression y=x.
v) Cast operators
These operators are used to convert the data type of operand explicitly to another type.
The general form is
(data type name)expression
Where datatype is int, double, char , pointer etc.
Example:(int)9.3 ,(double)15,(float)327,3/(int)7.2,(int)8.5/(int)4.3
Example: int a=10,b=6;
float x;
x=(float)a / (float)b;
The values of a and b are substituted in the right side expression in floating point mode and the value assigned
to x is1.6666666
Practice Programs:
1. What is the value assigned to a?
int a, x=2;
a = x + 3 * x++;
2. What is the output of the following program?
#include<stdio.h>
main()
{
int x= 5;
printf(“ %d , %d , %d”, ++x, x , x ++);
}
3. What is the output when the following program is executed?
#include<stdio.h>
main()
{
int a = 5, b = 0, c ;
c = a || b ++ ;
printf (“b = % d, c = %d”, b, c);
}
4.
What value is assigned to c when the following code is exectued?
int a=5,b=6,c;
c=(a+b>a-b)?a*b;a%b;
5. What is the output when the following code is executed?
#include<stdio.h>
main()
{
int x=10,y=15,a,b;
a=x++;
b=++y;
printf(“%d , %d ”,a,b);
}
Precedence and Associativity of all C operators:-
When an operator has operators of different types, then they are converted to a common type according
to a small number of rules. In autotype conversion, it converts a “narrower” operand into a “wider” one without
losing information.
Rules:-
If either operand is long double then other operand also converted to long double data type,
Otherwise, if either operand is double then other operand also converted to double data type,
Otherwise, if either operand is float then other operand also converted to float data type,
Otherwise, if either operand is integer then other operand also converted to integer data type.
If either operand is long integer then other operand is also converted to long integer data type.
Ex:-
void main()
{
int x =3;
float z = 2,y;
y = z/x;
printf(“%f”,y);
}
In this example consider sub-expression z/x. in z/x, z is float so that other operand x will also converted to the
float data type.
Explicit type conversion can be forced in any expression with a unary operator called a type casting
operator. In any expression if user wants to convert a “wider” data type to any “narrower” data type then this
type casting operator is useful. The syntax of type casting is
(type-name)expression;
Ex:- int x;
float y=5.0f;
x = (int)y;
Definitions:-
Bitwise AND(&) : if both bits are 1 then only it returns 1 otherwise it returns 0.
Bitwise OR(|) : if both bits are 0 then only it returns 0 otherwise it returns 1.
Bitwise XOR(^) : if both bits are same then it returns 0 otherwise it returns 1.
Bitwise 1’s compliment (~) : if the bit is 1 it returns 0 otherwise it returns 1.
Truth table:
Op1 Op2 Op1&Op2 Op1 | Op2 Op1^Op2 ~Op1
1 1 1 1 0 0
1 0 0 1 1 0
0 1 0 1 1 1
0 0 0 0 0 1
Ex:
void main()
{
int x=5;
int y =6;
int c;
c = x&y;
printf(“%d”,c);
c = x|y;
printf(“%d”,c);
c = x^y;
printf(“%d”,c);
c = x<<y;
printf(“%d”,c);
c = x>>y;
printf(“%d”,c);
c = ~x;
printf(“%d”,c);
printf(“%u”,c);
}