Unit II C Fundamentals SGT
Unit II C Fundamentals SGT
Note
• We can't store decimal values using int data type.
• If we use int data type to store decimal values, decimal values will be truncated
and we will get only whole number.
• In this case, float data type can be used to store decimal values in a variable.
Character data type
• Character data type allows a variable to store only one
character.
• Storage size of character data type is one. We can store
only one character using character data type.
• "char" keyword is used to refer character data type.
Example:
char ch='m';
char ch='5';
Floating point data type:
Floating point data type consists of two types. They are,
– float
– double
float:
• Float data type allows a variable to store decimal values.
• Storage size of float data type is 4. This also varies depend
upon the processor in the CPU as "int" data type.
• The range for float datatype is from 3.4E-38 to 3.4E+38.
• We can use upto 6 digits after decimal using float data
type
For example, 10.456789 can be stored in a variable using
float data type.
Syntax:
emum identifier {value1, value2,.... valuen}; // definition
enum identifier v1, v2,..., vn; // declaration of variables
v1= value3;
v4 = value1; //assigning values to variables
Example:
enum colour {green, blue, red, pink}; // green, blue, red, pink are constants
// or values
enum colour flowers, leaves; // flowers and leaves are variables
flowers =red;
Leaves= green;
• Compiler automatically assigns the integer values
beginning with 0 to all enumeration constants.
i.e. in above example, green, blue, red, pink
represent the integer values 0, 1, 2, 3
respectively.
• Enumeration constants can also be explicitly
assigned.
• In the same example: {enum colour green= 10,
blue, red, pink = 25};
• Here, blue is assigned 11 and red is 12. If no value
is assigned to pink, it will take the value 13.
• Character Set
• The C character set includes letters, digits,
special characters and white spaces as
building blocks to form basic programming
elements (tokens).
• Characters are used to write tokens. These
characters are defined by Unicode character
set.
Alphabets A, B, C, ………….Y, Z
a,b,c,………………y, z
Digits 0,1,2,3,4,5,6,7,8,9
Special Symbols ~ ` ! @ # $ % ^ & * ( 0 _ + = - , + *+ ‘ “ < > ? , .
• Constants in C
• The constants in C are the read-only variables whose values cannot
be modified once they are declared in the C program. The type of
constant can be an integer constant, a floating pointer constant, a
string constant, or a character constant. In C language,
the const keyword is used to define the constants.
• What is a constant in C?
• As the name suggests, a constant in C is a variable that cannot be
modified once it is declared in the program. We can not make any
change in the value of the constant variables after they are defined.
Syntax :
data_type variable-1,variable-2,------, variable-n;
Ex : int x,y,z;
float a,b;
char m,n;
Assigning values to variables : values can be assigned to
variables using the assignment operator (=). The general
format statement is :
Syntax : variable = constant;
Ex : x=100;
a= 12.25;
m=‟f‟;
we can also assign a value to a variable at the time of the
variable is declared. The general format of declaring and
assigning value to a variable is :
Syntax : data_type variable = constant;
Ex ; int x=100;
float a=12.25;
char m=‟f‟;
• Example:
int a=7;
int b=3;
int c;
7 3
a b c Garbage Value
Fig: Memory
• Types of Variables in C
There are many types of variables in c:
1. local variable
2. global variable
3. static variable
• Format Specifiers in C
• The format specifier in C is used to tell the compiler about the type of
data to be printed or scanned in input and output operations. They
always start with a % symbol and are used in the formatted string in
functions like printf(), scanf, sprintf(), etc.
• The C language provides a number of format specifiers that are
associated with the different data types such as %d for int, %c for char,
etc. In this article, we will discuss some commonly used format
specifiers and how to use them.
%lf Double
%s String
• Escape Sequence in C
• The escape sequence in C is the characters or the sequence of characters
that can be used inside the string literal. The purpose of the escape sequence
is to represent the characters that cannot be used normally using the
keyboard. Some escape sequence characters are the part of ASCII charset but
some are not.
• Different escape sequences represent different characters but the output is
dependent on the compiler you are using.
• Escape Sequence List
• The table below lists some common escape sequences in C language.
• Input / Output (I/O) Functions : In „C‟ language, two types
of Input/Output functions are available, and all input and
output operations are carried out through function calls.
Several functions are available for input / output operations
in „C‟. These functions are collectively known as the
standard i/o library.
• Input: In any programming language input means to feed
some data into program. This can be given in the form of
file or from command line.
• Output: In any programming language output means to
display some data on screen, printer or in any file.
1 : printf() : output data or result of an operation can be
displayed from the computer to a standard output device
using the library function printf(). This function is used to
print any combination of data.
Output
Hello World
Operators and their precedence
Operators : An operator is a Symbol that performs an operation. An
operators acts some variables are called operands to get the
desired result.
Ex : a+b;
Where a,b are operands and + is the operator.
Types of Operator :
1) Arithmetic Operators.
2) Relational Operators.
3) Logical Operators.
4) Assignment Operators.
5). Unary Operators.
6) Conditional Operators.
7) Special Operators.
8) Bitwise Operators.
9) Shift Operators.
• Arithmetic Operations in C
• The arithmetic operators are used to perform arithmetic /
mathematical operations on operands.
• Relational Operators in C
• The relational operators in C are used for the comparison of the two
operands. All these operators are binary operators that return true or false
values as the result of comparison.
• These are a total of 6 relational operators in C
• Logical Operator in C
• Logical Operators are used to combine two or more
conditions/constraints or to complement the evaluation of the
original condition in consideration. The result of the operation of a
logical operator is a Boolean value either true or false.
• Assignment Operators
• Assignment operators are used to assign values to
variables.
• In the example below, we use the assignment operator (=)
to assign the value 10 to a variable called x:
• Example
• int x = 10;
Unary Operators
• Unary operators require only one operand.
Operator Meaning
- Unary minus
+ Unary plus
++ Increment
-- Decrement
~ Bitwise Complement
& Address of
* Indirection
! Logical not
• All unary operators are used as prefix except the
increment and decrement.
Operator Meaning
++m Pre increment. A prefix unary plus operator adds 1 to the operand
and then assigns the result to the variable on left.
m++ Post increment. A postfix unary plus operator assigns the result to
the variable on left then adds 1 to the operand.
--m Pre decrement. A prefix unary minus operator subtracts 1 from the
operand and then assigns the result to the variable on left.
m-- Post decrement. A postfix unary minus operator assigns the result to
the variable on left then subtracts 1 from the operand.
Bitwise OR Operator |
The output of bitwise OR is 1 if at least one corresponding bit of two operands is 1. In C
Programming, bitwise OR operator is denoted by |.
• Example:
x<<3 shifts all bits in x, three places to the left.
If x contains the bit pattern 0110 1001, the x<<3 gives 0100 1000.
For each bit shifted to left, a zero is added to the right of the number.
• Example:
x>>3 shifts all bits in x, three places to the right.
If x contains the bit pattern 0110 1001, the x>>3 gives 0000 1101.
For each bit shifted to right, a zero is added to the left of the number.
Right Shift Operator
Right shift operator shifts all bits towards right by certain number of specified
bits. It is denoted by >>.
• Syntax
• operand1 ? operand2 : operand3;
return 0;
}
Output
Enter the radius of the circle: 5
The area of the circle is: 78.539749
What will be the output of following program
#include<stdio.h>
main()
{
int num=1;
num+=5;
printf("num=%d\n",num);
num-=3;
printf("num=%d\n",num);
num*=2;
printf("num=%d\n",num);
num/=2;
printf("num=%d\n",num);
}
Output
num=6
num=3
num=6
num=3
What will be the output of following program
#include<stdio.h>
main()
{
int ch;
printf("Enter the character");
ch=getchar();
printf("The character was %c\n",ch);
printf("The ASCII code of %c is %d\n",ch,ch);
}
Output
Enter the characterA
The character was A
The ASCII code of A is 65
Note: The ASCII values for uppercase letters (A-Z) range from 65 to 90,
the ASCII values for lowercase letters (a-z)range from 97 to 122.
the ASCII values for numbers (0-9) 48-57
#include<stdio.h>
#include<conio.h>
main()
{
clrscr();
printf("\nprogram");
printf("\b\bscience");
printf("\rcomputer");
getch();
}
Output
program science
computer
#include<stdio.h>
main()
{
int intN=55;
int intM=10;
char X='A';
intN+=50;
printf("\n1.uploaded value of intN is %d\n",intN);
printf("2.updated value of X is %d\n",X);
X='B';
printf("3.updated value of X is %c\n",X);
intN-=8;
printf("4.updated value of intN is %d\n",intN);
intN++;
printf("5.updated value of intN is %d\n",intN);
intN=intN++ + ++intM;
printf("6.updated value of intN is:%d, intM: is %d\n",intN,intM);
}
Output
1.uploaded value of intN is 105
2.updated value of X is 65
3.updated value of X is B
4.updated value of intN is 97
5.updated value of intN is 98
6.updated value of intN is:109, intM: is 11
#include<stdio.h>
#include<conio.h>
main()
{
int ch;
clrscr();
ch=getchar();
putchar(++ch);
ch++;
putchar(ch++);
printf("\n");
putchar(ch);
--ch;
putchar(--ch);
putchar(ch);
getch();
}
Output
5
67
866
#include<stdio.h>
main()
{
int x=125,y=140, z=175;
char a='A', b='B',c='C';
x=++x+--y+z--;
y=x-- + ++y+z++;
z=--x+y+z++;
a=a++;
b+=1;
c=--c+1;
printf("\nx=%d y=%d z=%d",x,y,z);
printf("a=%c b=%c c=%c",a,b,c);
printf("a=%d b=%d c=%d",a,b,c);
}
Output
x=438 y=754 z=1367a=A b=C c=Ca=65 b=67 c=67
#include<stdio.h>
main()
{
int a,b,c,sum;
scanf("%1d%2d%3d",&a,&b,&c);
printf("sum=%d",a+b+c);
}
Output
12
3
4
sum=8
OR
1
12
123
sum=136
#include<stdio.h>
main()
{
int i=15, j=4, m, n;
m=i>9;
n=j>4&&j!=2;
printf(“m=%d n=%d”,m,n);
}
Output
m=1 n=0
#include<stdio.h>
main()
{
int x=5, m, k=1, n;
float y=2.5;
m=x*1000+y*10;
k=m/1000+x;
n=(x==y) ? k:m;
printf(“%d/n%d/n%d”,m,k,n);
}
Output
5025/n10/n5025
#include<stdio.h>
#define CUBE(x) (x*x*x)
main()
{
printf(“%d”,CUBE(4+5));
}
Output
49
Because * has a higher precedence than +, this
expression is evaluated as
(4 + (5 * 4) + (5 * 4) + 5), producing 49
#include<stdio.h>
main()
{
int a=9, b=12, c=3;
float x,y,z;
x=a-b/3+c*2-1;
y=a-b/(3+c)*(2-1);
z=a-(b/(3+c)*2)-1;
printf(“x=%f\ny=%f\nz=%f”,x,y,z);
}
Output
x=10.000000
y=7.000000
z=4.000000