Programming in C
Programming in C
Year: I Semester: II
IiIII
Regulation 2020
UNIT I INTRODUCTION TO C
C programming: Overview of C - Visual Studio code-Constants - Compiling a C Program - Variables
and Data Types - Technical Difference between Keywords and Identifiers - Types of C Qualifiers and
format specifies - Operators and Expressions - Operators Precedence - Type conversion - Input-Output
Statements.
INTRODUCTION TO C.
C is a general-purpose high level language that was originally developed by Dennis
Ritchie at AT & T’s Bell Laboratories in 1972.
Many of the important ideas of C stem fromthe language BCPL(Basic Combined
Programming Language), developed by Martin Richards.
History of C language
C is one of the most popular programming language, it was developed by Dennis
Ritchie at AT & T‟s Bell Laboratories of USA in 1972.
C is a middle language
Low level language-0 and 1‟s.
High level language: eg FORTRAN, PASCAL, COBOL, BASIC, C, C++……etc.
C stands in between these two categories. It is neither a low level language nor ahigh
level language. It is a middle level language.
STRUCTURE OF A C PROGRAM
Every C program contains a no. of building blocks. These building blocks should be written in a
correct order and procedure, to execute without any errors. The structure of C is given below.
DOCUMENTATION
SECTION
PREPROCESSOR SECTION
DEFINITION SECTION & GLOBAL
DECLERATIONSECTION
main()
{
DECLARATION
PART
EXECUTEABLE
PART
}
SUB PROGRAM SECTION
{
BODY OF THE SUB
i) Documentation Section
The general comments are included in this section.
The comments are not a part of executable programs.
The comments are placed between delimiters (/* and */).
Example:
/* Factorial of a given number */
#include <string.h>
#include<conio.h>
Example:
#define PI 3.14
#define TRUE 1
#define FALSE 0
The variables that are used in more than one function throughout the program are called
global variable.
It should declare before the main() function.
Global variable otherwise known as External variable or Public variable
Example:
int a;
main()
{
v) main() function
Each and every C program should have only one main() function.
Without main() function, the program cannot be executed.
The main function should be written in lowercase only.
It should not be terminated with semicolon.
Example Program
void main()
area=PI*r*r;
printf("Area of the circle=%f",area);
getch();
}
Execution is the process of running the program, to execute a „c‟ program, we need
tofollow the steps given below.
1. Creating the program
2. Compiling the program
3. Linking the program with system library.
4. Executing the program.
Let us look at a simple code that would print the words "Hello World":#include
<stdio.h>
int main()
{
/* my first program in C */ printf("Hello, World! \n");
return 0
}
1. The
first line of the program #include <stdio.h> is a preprocessor command, whichtells
C compiler to include stdio.h file before going to actual compilation.
2. The next line int main() is the main function where program execution begins.
3. The next line /*...*/ will be ignored by the compiler and it has been put to add
additional comments in the program. So such lines are called comments in the program.
4. The next line printf(...) is another function available in C which causes the message
"Hello, World!" to be displayed on the screen.
5. The next line return 0; terminates main()function and returns the value 0.
3. Open a command prompt and go to the directory where you saved the file.
If there are no errors in your code, the command prompt will take you to the next lineand
would generate executable file.
C CHARACTER SET
The character set is the fundamental raw material of any language and they are used torepresent
information.
ESCAPE
CHARACTE RESULT
SEQUENC
R
E
Backspace \b Moves previous position
Horizontal tab \t Moves next horizontal tab
Vertical tab \v Moves next Vertical tab
New line \n Move next line
Form feed \f Moves initial position of next page
C TOKENS
C-Tokens otherwise known as „lexical elements‟
The smallest individual units of a C program are known as tokens.
In C program, tokens are categorized into six, they are given below.
C Tokens
i) Keywords
Keywords are reserved words whose meaning cannot be changed. They are used toconstruct
the program. They are written in lower case. There are 32 keywords are available.
ii) Identifiers
Example
iii) Constants ( Write about Constants and its types with example.)
The items whose values cannot be changed during the execution of program are called
constants. ‟C‟ constants can be classified as follows.
a) Numeric constants
There are two types of numeric constants,
1. Integer constants
2. Real or floating-point constants
1. Integer constants.
An integer constant refers to a sequence of digits without a decimal point.
Example:marks90,per75
2. Real Constant
A Real constant is made up of a sequence of numeric digits with presence of a decimal
point.
Department of Information Technology, SMVEC
U20EST201 – Programming in C Regulation: 2020
Defining Constants
value of area : 50
You can use const prefix to declare constants with a specific type
as follows:const type variable = value;
value of area : 50
C STORAGE CLASSES
A storage class defines the scope (visibility) and life-time of variables and/or functions
within a C Program. These specifiers precede the type that they modify. There are the following
storage classes, which can be used in a C Program
auto
register
static
extern
The auto storage class is the default storage class for all local variables.
{
int mount;
auto int month;
}
The example above defines two variables with the same storage class, auto can only beused
within functions, i.e., local variables.
The register storage class is used to define local variables that should be stored in a register
instead of RAM. This means that the variable has a maximum size equal to the register size
(usually one word) and can't have the unary '&' operator applied to it (as it does not have a
memory location).
{
register int miles;
}
The register should only be used for variables that require quick access such as
counters.
It should also be noted that defining 'register' does not mean that the variable will
be stored in a register.
It means that it MIGHT be stored in a register depending on hardware and
implementation restrictions.
The static storage class instructs the compiler to keep a local variable in existence during
the life-time of the program instead of creating and destroying it each time it comes into
and goes out of scope.
Therefore, making local variables static allows them to maintain their values between
function calls.
The static modifier may also be applied to global variables. When this is done, it causes
that variable's scope to be restricted to the file in which it is declared.
In C programming, when static is used on a class data member, it causes only one copy ofthat
member to be shared by all objects of its class.
#include <stdio.h>
Department of Information Technology, SMVEC
U20EST201 – Programming in C Regulation: 2020
/* function declaration */
void func(void);
static int count = 5; /* global variable */ main()
{
while(count--)
{
func();
}
return 0;
}
/* function definition */ void func( void )
{
static int i = 5; /* local static variable */ i++;
printf("i is %d and count is %d\n", i, count);
}
You may not understand this example at this time because I have used function and
global variables, which I have not explained so far.
So for now, let us proceed even if you do not understand it completely. When the above
code is compiled and executed, it produces the following result:
i is 6 and count is 4 i is 7 and count is 3 i is
8 and count is 2 i is 9 and count is 1 i is 10
and count is 0
The extern storage class is used to give a reference of a global variable that is visible to
ALL the program files.
When you use 'extern', the variable cannot be initialized as all it does is point the variable
name at a storage location that has been previously defined.
When you have multiple files and you define a global variable or function, which will be
used in other files also, then extern will be used in another file to give reference
ofdefined variable or function.
Just for understanding, extern is used to declare a global variable or function in another
file.
The extern modifier is most commonly used when there are two or more files sharing
thesame global variables or functions as explained below.
Variable declaration:
Syntax:
datatype v1,v2,v3,….,vn;
Description:
data_type - It is the type of data.
V1, v2, v3 ,…, vn - list of variables.
Example 1:
int regno;
float cgpa;
char name[10];
Example 2:
Declaration of multiple variables of the same data types can be done in one statement.
int mark1;
int mark2;
int mark3;
int mark4;
Initializing variables:
Initialization of variables can be done using the assignment operator(==).
Syntax:
Variable = constant;
Or
Datatype variable = constant;
Example:
A=5;
B=8;
int i =23; float
s=3.14;
Scope of variables
Scope of a variable implies the availability of variables within the program.
Two types:
Local variables
Global variables
Example:
func()
{
int i=10; /* Local definition */
i++; /* Local variable */
printf( "Value of i = %d -- func() function\n", i );
}
The variables that are declared before the function main ( ) are called the
global/external variables. These are available for all the functions inside the program.
Example:
int i=4; /* Global definition */
main()
{
i++; /* Global variable */
func();
printf( "Value of i = %d -- main function\n", i );
}
func()
{
int i=10; /* Local definition */
i++; /* Local variable */
printf( "Value of i = %d -- func() function\n", i );
}
Department of Information Technology, SMVEC
U20EST201 – Programming in C Regulation: 2020
7. DATA TYPES
Data type is the type of the data, that are going to access within the program. C supports
different data types, each data may have predefined memory requirement and storage representation.
„C‟ supports the following 4 classes of data types.
Int (Integer)
Integer data type is used to store numeric values without any decimal point e.g. 7, -101,
107, etc.
Syntax:
int variable name;
Example:
int roll, marks, age;
Float
Float data type is used to store numeric values with decimal point. In other words, float data
type is used to store real values, e.g. 3.14, 7.67 etc. e.g. percentage, price, pi, area etc. may contain real
values.
Syntax:
float variable name;
Example:
float per, area;
Char (Character)
Char (Character) data type is used to store single character, within single quotes e.g.'a',
'z','e' etc. e.g. Yes or No Choice requires only 'y' or 'n' as an answer.
Syntax:
char variable name;
Example:
char chi='a', cha;
Double
Double is used to define BIG floating point numbers. It reserves twice the storage forthe
number. It contains 8 bytes.
Example:
double Atoms;
Typedef:
The 'typedef' allows the user to define new data-types that are equivalent to existing
data types. Once a user defined data type has been established, then new variables, array,structures,
etc. can be declared in terms of this new data type.
Syntax:
typedef type new-type;
Example:
typedef int number;
Array
An array is a collection of variables of same type i.e. collection of homogeneous data referred by a
common name. In memory, array elements are stored in a continuous location.
Syntax:
Datatype arrayname[ ];
Example:
int a[10];
char chi [20];
Pointer
A pointer is a special variable that holds a memory address (location in memory) ofanother
variable.
Syntax:
datatype *var_name;
Example:
int a,*b;
Struct
A struct is a user defined data type that stores multiple values of same or different datatypes
under a single name. In memory, the entire structure variable is stored in sequence.
Syntax:
struct < structure name>
Department of Information Technology, SMVEC
U20EST201 – Programming in C Regulation: 2020
{
member1;
member2;
};
Structure name is the name of structure e.g. store details of a student as- name, roll, marks. struct
student
{
Char name [20];
Int roll,
Float marks;
};
Union
A union is a user defined data type that stores multiple values of same or different data types
under a single name. In memory, union variables are stored in a common memory location.
Syntax:
union<tagname>
var1;var2;
};
Tag name is the name of union, e.g, store details of a student as- name, roll, marks.
Union student
{
Char name[20];
Int roll,
Float marks;
};
Void
Void data type is used to represent an empty value (or) null value. It is used as a return
type if a function does not return any value.
a) Arithmetic operators
C allows basic Arithmetic operations like addition, subtraction,
multiplication and division.
- Subtraction 9-2=7
* Multiplication 3*5=15
/ Division 9/3=3
% Modulus 9%2=1
Example:1
#include<stdio.h>
#include<conio.h>
main ( )
{
int i,j,k;
clrscr( );
i=10;
j=20;
k=i+j;
printf(“values of k is %d”,k);
} getch( );
Output:
Value of k is 30
Example:2
#include <stdio.h>
main()
{
c = a + b;
printf("Line 1 - Value of c is %d\n", c );c
= a - b;
printf("Line 2 - Value of c is %d\n", c );c
= a * b;
printf("Line 3 - Value of c is %d\n", c );c
= a / b;
printf("Line 4 - Value of c is %d\n", c );c
= a % b;
printf("Line 5 - Value of c is %d\n", c );c
= a++;
printf("Line 6 - Value of c is %d\n", c );c
= a--;
printf("Line 7 - Value of c is %d\n", c );
b) Relational operator
A relational operator is mainly used to compare two or more operands.
OPERATOR MEANING EXAMPLE RETURN
< Less than 2<9 1
> Greater than 2>9 0
== Equal to 2==2 0
!= Not equal to 2!=3 0
Example:1
#include<stdio.h>
#include<coio.h>
main( )
{
clrscr( );
printf(“\n Condition: Return values\n”);
printf(“\n 5!=5 :%5d”,5!=5);
} printf(“\n 5==5: %5d”,5==5);
Output:
main()
{
int a = 21; int b = 10; int c ;
if( a == b )
{
printf("Line 1 - a is equal to b\n" );
}
else
Department of Information Technology, SMVEC
U20EST201 – Programming in C Regulation: 2020
{
printf("Line 1 - a is not equal to b\n" );
}
if ( a < b )
{
printf("Line 2 - a is less than b\n" );
}
else
{
printf("Line 2 - a is not less than b\n" );
}
if ( a > b )
{
printf("Line 3 - a is greater than b\n" );
}
else
{
printf("Line 3 - a is not greater than b\n" );
}
/* Lets change value of a and b */ a = 5;b =
20;
if ( a <= b )
{
printf("Line 4 - a is either less than or equal to b\n" );
}
if ( b >= a )
{
printf("Line 5 - b is either greater than or equal to b\n" );
}
}
When you compile and execute the above program, it produces the following result:
Line 1 - a is not equal to b Line 2 - a is not less
c) Logical operator:
Logical operators are used to combine the results of two or more conditions.
Example:1
#include<stdio.h>
main()
{
printf("\n Condition : return values\n");
printf("\n5>3 && 5<10: %5d",5>3 && 5<10);
printf("\n8>5||8<2 : %5d",8>5||8<2):
printf("\n!(8==8) : %5d",!(8==8));
}
Output:
Example:2
main()
{
int a = 5; int b = 20; int c ;
if ( a && b )
{
printf("Line 1 - Condition is true\n" );
}
if ( a || b )
{
printf("Line 2 - Condition is true\n" );
}
/* lets change the value of a and b */ a = 0;b =
10;
if ( a && b )
{
printf("Line 3 - Condition is true\n" );
}
else
{
printf("Line 3 - Condition is not true\n" );
}
if ( !(a && b) )
{
printf("Line 4 - Condition is true\n" );
}
d) Assignment operator
Assignment operators are mainly used to assign a value or expression or value ofa
variable to another variable.
left operand
Example:
#include<stdio.h>
#include<Conio.h>
main( )
{
int i,j,k;
clrscr( );
k=(i=4,j=5);
printf(“k=%d”,k);
} getch( );
Output:
K=5
Example:
#include <stdio.h>
main()
{
int a = 21; int c ;c
=a;
printf("Line 1 - = Operator Example, Value of c = %d\n", c );
c +=a;
printf("Line 2 - += Operator Example, Value of c = %d\n", c );
c -=a;
printf("Line 3 - -= Operator Example, Value of c = %d\n", c );
c *=a;
printf("Line 4 - *= Operator Example, Value of c = %d\n", c );
c /=a;
printf("Line 5 - /= Operator Example, Value of c = %d\n", c );
c= 200;
c %=a;
printf("Line 6 - %= Operator Example, Value of c = %d\n", c );
c <<= 2;
printf("Line 7 - <<= Operator Example, Value of c = %d\n", c );
c >>= 2;
printf("Line 8 - >>= Operator Example, Value of c = %d\n", c );
c &=2;
printf("Line 9 - &= Operator Example, Value of c = %d\n", c );
c ^=2;
printf("Line 10 - ^= Operator Example, Value of c = %d\n", c );
c |=2;
printf("Line 11 - |= Operator Example, Value of c = %d\n", c );
}
When you compile and execute the above program, it produces the following result:
Line 1 - = Operator Example, Value of c = 21
Line 2 - += Operator Example, Value of c = 42
Line 3 - -= Operator Example, Value of c = 21
Line 4 - *= Operator Example, Value of c = 441
Line 5 - /= Operator Example, Value of c = 21
Line 6 - %=Operator Example, Value of c = 11
Line 7 - << = Operat or Example, Value of c = 44
Line 8 - >> = Operator Example, Val ue of c = 11
9 - -&=
Line 10
Line Operatorr Example,
^= Operato Valu eofofcc==0 2
Example ,Value
Line 11 - |= Operator Example, Value of c = 2
OPERATOR MEANING
++a Pre increment
--a Pre decrement
a++ Post increment
a-- Post decrement
Example:
#include<stdio.h>
#include<conio.h>
main( )
{
int a=10;
printf(“a++=%d\n”,a++);
printf(“++a=%d\n”,++a);
printf(“a--=%d\n”,a--);
} printf(“--a=%d\n”,--a);
Output:
a++=10
++a=12
Department of Information Technology, SMVEC
U20EST201 – Programming in C Regulation: 2020
a--=11
--a=11
f) Conditional operators
Conditional operators itself check the condition and executes the statement dependingupon
the condition.
Syntax:
Description:
“? :” operator acts as a ternary operator.It
first evaluates the condition
If it is true then „exp1‟ is evaluated.If
Example: it is false yhen‟exp2‟ is evaluated.
main( )
{
int a=5,b=3,big;
big=a>b?a:b;
printf(“Big is……%d”,big);
Output: }
Big is 5
f) Bitwise operator:
It is used to manipulate the data at-bit level. It operates only integers.
OPERATOR MEANING
& Bitwise AND
| Bitwise OR
^ Bitwise XOR
<< Shift Left
>> Shift Right
~ One‟s Complement
= 0000 1101
~A = 1100 0011
The Bitwise operators supported by C language are listed in the following table. Assumevariable A
holds 60 and variable B holds 13, then:
Example:
12 = 00001100 (In Binary)
25 = 00011001 (In Binary)
#include <stdio.h>
main()
{
int a=12,b=39;
printf("Output=%d",a&b);
}
Output
Output=4
g) Special operator: „C‟ language supports some of the special operators they are
OPERATORS MEANING
, Comma operators
Size of Size of operators
& and * Pointer operators
. and -> Member section operators
Comma operators:
It is used to operate the statement such as variables, constants,expressin etc.
Example: val= (a=3,b=9,c=77);
Sizeof( ) operator:
IT is Unary operator.It returns the length in bytes of specified variable.It returns the
length in bytes of specified variable. It is very useful to find the bytes occupied by specified variables
in the memory.
Pointer operator:
&-> this symbol is used to specify the ADDRESS of the variables.
*-> this symbol is used to specify the VALUE if a variable.
OPERATORS PRECEDENCE IN C
Here, operators with the highest precedence appear at the top of the table, those
with thelowest appear at the bottom. Within an expression, higher precedence
operators will be evaluated first.
Example:
#include <stdio.h>
main()
{
int a = 20; int b = 10; int c = 15; int d = 5; int e;e =
(a + b) * c / d; // ( 30 * 15 ) / 5 printf("Value of (a +
b) * c / d is : %d\n", e );
e = ((a + b) * c) / d;// (30 * 15 ) / 5
printf("Value of ((a + b) * c) / d is : %d\n" , e );
e = (a + b) * (c / d);// (30) * (15/5)
printf("Value of (a + b) * (c / d) is : %d\n", e );
e = a + (b * c) / d; // 20 + (150/5) printf("Value
of a + (b * c) / d is : %d\n" , e );
return 0;
}
When you compile and execute the above program, it produces the following result:
Value of (a + b) * c / d is 90
Value of ((a + b) * c) / d is 90
Value of (a + b) * (c / d) is : 90
Value of a + (b * c) / d is :50
In C language there are 2 types of I/P & O/P statements are available.
gets() puts()
INPUT OUTPUT
getchar() putchar()
getc() putc()
gets() puts()
This getchar() function is written in standard I/O library. It reads a single character from a
standard input device.
Program:
#include<stdio.h>
#include<conio.h>
main()
{
char ch;
printf(“Enter any character/digit…”);
ch=getchar();
if(isalpha(ch)>0)
printf(“It is a alphabet”);
Department of Information Technology, SMVEC
U20EST201 – Programming in C Regulation: 2020
else
if(isdigit(ch)>0)
printf(“It is a digit”);
else
printf(“It is alphanumeric”);
}
Output:
1. Enter any character/digit…a
It is alphabet
2. Enter any character/digit…1
It is a digit
3. Enter any character/digit…#
It is alphanumeric
Program:
#include<stdio.h>
#include<conio.h>
main()
{
char ch;
printf(“Enter any alphabet either in lower or uppercase..”)ch=getchar();
if(islower(ch))
{
putchar(toupper(ch));
}
else
{
putchar(tolower(ch));
} }
Output:
putc() function:
This is used to display a single character in a character variable to standard output device.This
function is mainly used in file processing.
Syntax:
putc(character variable);
Eg:
char c;
putc(c);
gets() function:
The gets() function is used to read the string from the standard input device(keyboard).
Syntax:
puts(char type of array variable);
Eg:
puts(s);
Program:
#include<stdio.h>
#include<conio.h>
main()
{
char scientist[40];
puts(“Enter name:”);
gets(scientist);
puts(“print the name”);
puts(scientist);
}
OUTPUT:
Enter name: Risha
print the name: Risha
INPUT OUTPUT
scanf() printf()
fscanf fprintf()
scanf() function:
Input data can be entered into the computer using standard input C library function called
scanf().This function is used to enter any combinations of input.
scanf() function is mainly used to read information from the standard input device keyboard.
Syntax:
scanf(“control string”,&var1,&var2,…&varn);
Eg:
int n;
scanf(“%d”,&n);
Program:
#include<stdio.h>
#include<conio.h>
main()
Department of Information Technology, SMVEC
U20EST201 – Programming in C Regulation: 2020
{
int m,n,a;
clrscr();
printf(“Enter the 2 numbers:”);
scanf(“%d%d”,&m,&n);
if(m>n)
{
a=m;
m=n;
n=a;
}
printf(“the interchanged values are: “%d%d”,m,n);
getch();
}
printf() function:
Output data can be displayed from the computer using standard output C
library function called printf().This function is used to display any combinations of
data.
prints() function is mainly used to display information from the standard
output device(monitor).
Syntax:
printf(“control string”,&var1,&var2,…&varn);
Eg:
int n;
printf(“%d”,n);
Program:
#include<stdio.h>
#include<conio.h>
main()
{
clrscr();
printf(“Engineering Students \n”);
getch();
}
Typecasting is converting one data type into another one. It is also called as data conversion ortype
conversion. It is one of the important concepts introduced in 'C' programming.
'C' programming provides two types of type casting operations:
1. Implicit type casting
2. Explicit type casting
Implicit type casting
Also known as „automatic type conversion‟
1. Implicit type casting means conversion of data types without losing its original meaning. (i.e.)
without changing the significance of the values stored inside the variable.
2. Implicit type conversion happens automatically when a value is copied to its compatible data
type.
3. During conversion, strict rules for type conversion are applied. If the operands are of two
different data types, then an operand having lower data type is automatically converted into a
higher data type.
char -> short int ->int ->unsigned int -> long -> unsigned long ->longlong ->
unsigned long long-> float -> double -> long double
4. Implicit type of type conversion is also called as standard type conversion. We do not
require any keyword or special statements in implicit type casting.
5. Converting from smaller data type into larger data type is also called as typepromotion.
Output
10
10
1. const
CONST
2. KEYWORD:
volatile
Constants are also like normal variables. But, only difference is, their values can‟t be
modified by the program once they are defined.
They refer to fixed values. They are also called as literals.
They may be belonging to any of the data type.
Syntax:
constdata_typevariable_name; (or) constdata_type *variable_name;
Please refer C – Constants topic in this tutorial for more details on const keyword.
2. VOLATILE KEYWORD:
When a variable is defined as volatile, the program may not change the value of the variable
explicitly.
But, these variable values might keep on changing without any explicit assignment by the
program. These types of qualifiers are called volatile.
For example, if global variable‟s address is passed to clock routine of the operating system
to store the system time, the value in this address keep on changing without any assignment
by the program. These variables are named as volatile variable.
Syntax:
volatile data_typevariable_name; (or) volatile data_type *variable_name;
Format specifiers in C are used for input and output purposes. Using format specifier the
TYPES OF C QUALIFIERS:
There are two types of qualifiers available in C language.
They are,compiler can understand that what type of data is in input and output operation.
There are some elements that affect the format specifier. Below, I have mentioned elementsthat
Department of Information Technology, SMVEC
U20EST201 – Programming in C Regulation: 2020
When you are printing using the printf function, there is no specific difference between the %i and %d
format specifiers. But both format specifiers behave differently with scanf function.
The %d format specifier takes the integer number as decimal but the %i format specifier takes the
integer number as decimal, hexadecimal or octal type. it means the %i automatically identified the base
of the input integer number.
%c Character
%d Signed integer
%f Float values
%g or %G Similar as %e or %E
%i integer
%lf Double
%o Octal representation
%p Pointer
%s String
%u Unsigned int
%x or %X Hexadecimal representation
48
Format Specifier Type
%n Prints nothing
%% Prints % character
#include <stdio.h>
int main()
{
int data = 65;
printf("%c\n", data);
return 0;
}
Output
Example
#include <stdio.h>
int main()
{
int data1, data2, data3;
printf("Enter value in decimal format:");
scanf("%d",&data1);
printf("data1 = %i\n\n", data1); printf("Enter
value in hexadecimal format:");
scanf("%i",&data2);
printf("data2 = %i\n\n", data2);
printf("Enter value in octal format:");
scanf("%i",&data3);
printf("data2 = %i\n\n", data3);
return 0;
}
49
Example
#include <stdio.h>
int main()
{
float data = 6.27;
printf("%f\n", data);
printf("%e\n", data);
return 0;
}
Output
6.270000
6.270000e+000
Example
#include <stdio.h>
int main()
{
float data = 6.276240;
printf("%f\n", data);
printf("%0.2f\n", data);
printf("%0.4f\n", data);
return 0;
}
Output:
6.276240
6.28
6.2762
Example
#include <stdio.h>
int main()
{
int pos = 14; float
data = 1.2;
printf("%*f",pos,data); return
0;
}
Ans:
50
The output of the above code will be 1.200000 with 6 space.
Explanation:
51
Here 1.200000 is printing with, 6 spaces, because by giving * in printf we can specify an additional
width parameter, here „pos‟ is the width and „data‟ is the value. if the number is smaller than the width
then rest is filled with spaces.
Example
#include <stdio.h>
int main(void)
{
double data1 = 123456.0;
printf("%e\n", data1);
printf("%f\n", data1);
printf("%g\n", data1);
printf("\n");
double data2 = 1234567.0;
printf("%e\n", data2);
printf("%f\n", data2);
printf("%g\n", data2); return
0;
}
Output:
1.234560e+005
123456.000000
123456
1.234567e+006
1234567.000000
1.23457e+006
Explanation:
When using the G ( or g) conversion specifier, the double argument representing a floating-pointnumber
is converted in style f or e (or in style F or E ), depending on the value converted and the precision.
#include <stdio.h>
int main()
{
int data = 65;
printf("%o\n", data);
return 0;
Output: 101
52
Format specifier (Hexadecimal number): %x, %X
#include <stdio.h>
int main()
{
int data = 11;
printf("%x\n", data);
return 0;
}
Output: b
#include <stdio.h>
int main()
{
charblogName[] = "aticleworld";
printf("%s\n", blogName); return
0;
}
Output: aticleworld
#include <stdio.h>
int main()
charblogName[] = "aticleworld";
printf("%s\n", blogName);
printf("%24s\n", blogName);
printf("%-24s\n", blogName);
printf("%24.6s\n", blogName);
printf("%-24.6s\n", blogName);
return 0;
}
53
Single program
#include<stdio.h>
main()
Char ch='B';
x =45, y =90;
printf("%d\n", x);
printf("%i\n", y);
float f =12.67;
a =67;
charstr[]="Hello World";
printf("%s\n",str);
printf("%-20s\n",str);//left align
printf("%20.5s\n",str);//shift to the right 20 characters including the string, and print stringup to 5
character
54
printf("%-20.5s\n",str);//left align and print string up to 5 character
55
Output
B
45
90
12.670000
1.267000e+001
103
43
Hello
World Hello
World Hello
WorldHello
Hello
Mainly, an IDE includes 3 parts i.e. source code editor, build automation tool (compiler) and a
debugger.
The source code editor is something where programmers can write the code, whereas, build
automation tool is used by the programmers for compiling the codes and the debugger is used
to test or debug the program in order to resolve any errors in the code.
Furthermore, these IDEs also comes with additional features like object and data modeling,
unit testing, source code library, and a lot more.
As of now, several IDEs are available for various programming languages like Python,
C++,Java, JavaScript, R and others. The modern IDEs even possess intelligent code completion
for maximizing the programmer‟s productivity.
These are simple editing environments consisting of several features making coding
quick and efficient.
Takes less time and effort- It includes various tools and features that help to prevent
mistakes, organizes resources and provide shortcuts.
It allows quick navigation to the type
Programmers can quickly navigate to other members by using hyperlinks
IDEs organize imports and can add appropriate imports
56
It can give warning in case of any errors or mistakes
IDEs are great for generating code or completing the code depending upon previous
codes.
57
VISUAL STUDIO CODE
Visual Studio Code is a streamlined code editor with support for development operations like
debugging, task running, and version control.
It aims to provide just the tools a developer needsfor a quick code-build-debug cycle and leaves
more complex workflows to fuller featured such asVisualStudioCode.
It is an open-source code editor developed by Microsoft for Windows, Linux and Mac OS.
Visual Studio Code is based on an Electron framework.
According to a survey done in 2018 by Stack Overflow, it was ranked the most popular
developer environment tool among others.
Furthermore, this IDE is also customizable which lets programmers change the theme, keyword
shortcuts and preferences.
A visual studio code is a lightweight software application with a powerful source code editor that runs
on the desktop. It is a free source code editor developed by Microsoft for Windows, Mac OS and Linux.
It is a software editor that has a rich extension of various languages like C++,C+, C, Java,
Python, PHP, Go, etc. and runtime language extensions such as .NET and Unity. Itis easy to edit,
build, syntax highlighting, snippets, code refactoring and debugging. In visual studio code, we can
change the application's background theme, keyboard shortcuts set on our preferences, install an
extension and add additional functionality.
3. Download the C/C++ Extension. It is an extension provided by Microsoft that support visual
studio code. It helps in IntelliSence, debugging and code browsing of the programming code in
the visual studio.
4. Download the C/C++ compilers. There are some popular compilers are:
1. GCC on Linux
58
code.visualstudio.com. Visual Studio Code is a free source-code editor made by Microsoft for
Windows, Linux and macOS. Features include support for debugging, syntax highlighting, intelligent
code completion, snippets, code refactoring, and embedded Git.
Key Benefits:
Programming Languages Supported: C, C++, C#, CSS, Go, HTML, Java, JavaScript, Python, PHP,
TypeScript and much more.
59
Download & Install the C/C++ Extension
1. We need to click on the extension button that displays a sidebar for downloading and
installing the C/C++ extension in the visual studio code. In the sidebar, type C Extension.
In this image, click on the Install button to install the C/C++ extension.
A MinGW is an advanced GCC compiler software used to compile and execute code. It issoftware
that supports only the window operating system.
60