Introduction to C-Language
C is an improved version of B language which was developed by Dennis Ritchie in Bell
Laboratory.
It is a high level programming language that uses English like words for writing
instruction in the program.
It is used to develop application software and system software. Unix operating system is
totally written in the c language so it is more powerful language as compare to other
languages..
It is a procedure oriented language in which we develop the program by breaking them
into functions so it is also called functional programming.
It is also called a case sensitive language that is in c “syllabus and SYLLABUS is
different words not the same.
Tokens
Smallest units or symbols/Characters that recognize by the C system is called tokens.
Tokens are used to construct building block of C program.
Types of Tokens in C:
1. Keywords – Reserved words in C Langauge is called keywords. It has the fixed
meaning in C language so we cannot use them for other naming. these are total 32 in c
language. eg. int, float, char, double, if, while, do for etc.
2. Identifiers – it consists of alphabets, digits and underscore. Name of the variables,
Constant, functions, arrays and structure is called identifier. it represents values into
memory. e.g., stmarks, sum, main, stid, address, contacts.
3. Constants – Fixed values that do not change at the time of executing program is called
constant. (e.g., 100, 30, 3.10, 'A', ’B’).
4. Strings –group of characters enclosed in double quotes is called string. e.g., "Hello",
“remarkable”
5. Operators –These are Symbols used to perform operations on values. e.g., +, -, *, /,%
>, < == etc.
6. Special Symbols / Punctuators – Symbols with specific meaning called special
symbols. (e.g., ;, { }, [ ], ( ), #).
7. Variables:- Storage location name where we store a specific type of value in the
memory is called variable. The variable accepts different value at different time. we input
a type of value through keyboard that accepts by the variable at run time.
Data types
data type define the type of data that a variable stores in the memory. What type of value
you want to store in memory determine by the data type. These are as follows-
1. Primary or Basic Data Types
These types are pre-defined in c language and directly usable by the
programmer. int, float, double, char
1. integer data type
Keyword: int
You can store values without
decimal Size : occupy memory 2
bytes
Maximum Range of values -32,768 to 32,767
2. Float data type
Keyword: float
Size occupy 4 bytes
Range : 3.4e-38 to 3.4e+38
Store values with 6 decimal
digits
3. Double data
type Keyword :
double Size
occupy 8 bytes
Range of values 1.7e-308 to
1.7e+308 stores value with 15
decimal digits
4. character data type
Keyword : char
Size occupy 1 byte
Range of values -128 to 127
2. Derived Data Types
These are created from basic data types.
Derived data types are - Array, pointer, structure and union
3. User-defined Data Types
These are defined by the programmer so it is called user defined data type.
typedef (Type definition) and enum (Enumeration) keywords are used for making user
define data type.
Data type modifier
Modifiers means→ Keywords that change the size or range of a data type.
Types:
1. Size Modifiers:-
short modifier -
Decrease the size of integer and
long modifiers -
increase the size of integer data type.
2. Sign Modifiers
signed - it allows integer data type to store the positive and negetive value both.,
unsigned- allows only positive value into int data type.
For example
short int x;
long int y;
signed int a;
unsigned int b;
Basic Structure of a C Program
A C program is divided into different sections. The general structure looks like this:
//program to print the hello message
#include <stdio.h> // Preprocessor
Directive int main()
{
// Local Declarations
// Statements
return 0;
}
// User-defined Functions (optional)
Sections of a C Program
1. Documentation Section (Comment lines)
Used to write program information, author details, or purpose. Compiler avoids this
section. there are two types of comments in c language eg single line comment and
multiline comment Example:
1. Multiline comment
/* This program calculates the sum of two numbers
Designed by the mayank sharma*/
2. Single line comment
//program to swap two values
2. Link Section
This section is used to import libraries in c language. The program is to be processed
from where the # hash sign is presented in the program. all preprocessor directives begin
with hash # symbol.
#include<stdio.h> is called include directive that includes header file for using the input
output functions in C.
eg.
in this section Includes header files (libraries) needed in the program as
follows . Example:
#include <stdio.h> // for input/output
functions #include <math.h> // for
mathematical functions
Symbolic constant
we have to way to define the symbolic constant in c. which are as follows-
1. #define directive
it is also used in this section to create the symbolic constants and macros.
Name to given a value is called symbolic constant that do not change their value
throughout the program.
Name to given a piece of code is called macro. it is a way to use the code with a specific
name.
2. const keyword
it is also an another way to create a symbolic constant that do not allow to change their
value throughout the program.
eg. const int x = 10;
3. Global Declaration Section
we declare variables and functions that can be used throughout the program.
global variables have global scope that is these are visible in more than one segment.
Example:
int wages = 350; // global variable
4. main() Function Section
Make sure the execution of the program starts from here. So it is called an executable
function or an executable program.
each and every program of c must have a main() function if it is an executable
program. it has two parts -declaration part - local variables are declared in this
part. local variable means that visibility is within a segment.
Scope of the variable - visibility of variable in the program is called scope of variable.
a) executable part
we write statements which are executed then after respond on the
screen. For Example:
int main() {
int a = 10, b = 20; //declaration
part int sum = a + b; //executable
part printf("Sum = %d", sum);
return 0;
}
5. Subprograms (User-defined Functions)
This is a subprogram section where the programmer define functions for reusing code at
multiple points in the program. it is defined by the user to perform specific tasks so it is
called user defined function.
Example:
int sum()
{
//statements
return 0;
}
sample program of c language
Open Turboc compiler IDE editor (Integrated development environment)
Compiler used to convert your source code into machine code that understand by the
computer. Open new file then write the following code
//program to print the hello message
#include<stdio.h>
int main()
{
printf("Hello
friends"); return 0;
}
How to compile and run of C program
We will follow these following steps to compile and run the c program
1. Save program file by pressing f2 function key or save command from file menu
2. Give name it as hello.c
3. Compile it by pressing ALT + F9 key
After compiling program will generate new file that is called object or machine code file
that understand by the computer only.
The machine code file is having .obj
type. eg. [Link]
4. Run program by pressing CTRL + F9 key
After running program the linker links function with their code then executes to program.
After running program an executable file is created by the system that responds on the
screen.
The executable file is having .exe
type eg. [Link]
5. View output of program by pressing ALT + F5 function key
Formatted I/O Functions
printf() & Scanf() Function
printf() function
it is an output function used to display data on the
screen. It is defined in the <stdio.h> header file.
Its syntax is as follows-
printf("format specifier", variable1, variable2, ...);
format specifier →Format specifiers tell to the compiler what type of data is being
printed or read using printf() and scanf() function.
Common Format Specifiers are as follows-
Specifier Used For
%d Integer (None Decimal Number)
%f float (decimal number)
%.2f Float with 2 decimal
%c Single character
%s String
%u Unsigned integer
%% To print % sign
For example
#include <stdio.h>
#define name "Vishal
singh"
int main() {
int a = 10;
float b = 3.1415;
char c = 'A';
float marks = 65.69;
printf("Integer: %d\n", a);
printf("Float: %.2f\n", b); // prints 2 decimal places
printf("Character: %c\n", c); printf("String: %s\n", name); printf("marks:
%0.2f%%\n",marks); return 0;
}
Output:
Integer:
10
Float: 3.14
Character: A
String: Manoj
marks = :65.69
%
scanf() function in C
scanf() is an input function.
It is used to take input from the keyboard and store it in program
variables. It is defined in the header file <stdio.h>.
Syntax
scanf("format specifier", &variable);
format specifier → tells to the compiler what type of data to read (%d, %f, %c, %s, etc.).
&variable means address of the variable designated that the entered value gets stored at
that location.
Example 2:
#include
<stdio.h> int
main() {
int rollno;
printf("Enter your rollnumber: ");
scanf("%d", &rollno);
printf("Your roll number is %d",
rollno); return 0;
}
Note: & address of operator does not recognize white space or stop reading at white
space in the string when you write the full name like "rakesh sharma" then it will take
only rakesh from the string.
//Calculate simple interest
#include <stdio.h>
int main() {
float principal, rate, time, si;
printf("Enter principal amount: ");
scanf("%f", &principal);
printf("Enter annual interest
rate:"); scanf("%f", &rate);
printf("Enter time (in years): ");
scanf("%f", &time);
si = (principal * rate * time) / 100;
printf("\nSimple Interest = %.2f\n", si);
return 0;
}
//Calculate compound interest
include <stdio.h>
#include <math.h> // for pow()
function int main()
{
float principal, rate, time, amount,
ci; printf("Enter principal amount:
"); scanf("%f", &principal);
printf("Enter annual interest rate:");
scanf("%f", &rate);
printf("Enter time”
); scanf("%f",
&time);
amount = principal * pow((1 + rate / 100),
time); ci = amount - principal;
printf("\nCompound Interest = %.2f\n",
ci); printf("Total Amount = %.2f\n",
amount); return 0;
}
//Calculate total and percentage for given marks of three subjects
#include <stdio.h>
int main() {
float subject1, subject2, subject3;
float totalMarks, percentage;
printf("Enter marks for Subject 1:
"); scanf("%f", &subject1);
printf("Enter marks for Subject
2: "); scanf("%f", &subject2);
printf("Enter marks for Subject
3: "); scanf("%f", &subject3);
totalMarks = subject1 + subject2 +
subject3; percentage = (totalMarks / 300) *
100;
printf("\nTotal Marks = %.2f out of 300\n",
totalMarks); printf("Percentage = %.2f%\n",
percentage);
return 0;
}
Operators in C
In C programming, operators are symbols that perform operations on variables and
values. There are several types of operators in C.
There are three types of operators
C language provides the following types of operators −
1. Arithmetic Operators
2. Relational Operators
3. Logical Operators
4. Bitwise Operators
5. Assignment Operators
6. Other Operators
1. Arithmetic Operators
Used to perform basic mathematical operations.
Operator Description Example
+ Addition a+b
- Subtraction a-b
* Multiplication a*b
/ Division a/b
% Modulus (remainder of two a%b
valuies)
//program to calculate months & remaining days according to given values of total days
include<stdio.h>
int main()
{
int days,month,rdays;
printf("Enter days"); scanf("%d", &days);
month = days/30;
rdays = days%30;
printf("Month=%d\n", month);
printf("remaining days=%d", rdays);
return 0;
}
2. Relational (Comparison) Operators
These operators compare two values and return either true (1) or false (0).
For example
x=5,y=6;
== Is Equal to
(x==y) return 0
!= Not equal to
(x!=y) return 1
> Greater than
(x>y) return 1
< Less than
(x<y) return 1
>= Greater than or equal to
(x>=y) return 0
<= Less than or equal to
(x<=y) return 1
3. Logical Operators
Used to perform logical operations. These are as follows-
1. && (LOGICAL AND) → Returns true if both conditions are true.
For example
int a=2;
int b=3;
(a > 0 && b > 0)
Result= true
2. || (LOGICAL OR) → Returns true if at least one condition is true.
For example
int a=2;
int b=to
(a > 0 || b <0)
Result= true
3. ! (LOGICAL NOT) → Reverses the result (true → false, false → true).
Example:
int a=3;
!(a > 5)
Result=true
4. Assignment Operators
These are used to assign values to variables.
∙= Simple assignment
∙+= Add and assign
-= Subtract and assign
*= Multiply and assign
/= Divide and assign
For example
a=3:
a=a+1 we can write this like this
a+=1;
5. Ternary (Conditional) Operator
This is a shorthand for an if-else statement.
● condition ? expr1 : expr2
● If condition is true, expr1 is evaluated; otherwise, expr2 is evaluated.
6. Special operator
7. Comma Operator
The comma operator allows multiple expressions to be evaluated sequentially, and the
value of the last expression is returned.
int x, y, z;
7. Size of Operator
Used to determine the size (in bytes) of a data type or returns size of specify data type in
bytes.
For example
Printf(“size of int type%d”, sizeof(int));
8. Typecast Operator
It is used to convert a data type into another data type, it is called type conversion or type
casting. It is done by the type cast operator.
There are two types of conversion of data types one is implicit conversion and explicit
conversion.
Implicit conversion – automatic conversion of one type to another type called implicit
conversion.
Float x = 21;
Int y = 2.25;
Explicit conversion
Used to explicitly or manually convert a data type to another data type using type case
opeator.
Syntax
(data type)variable name
float
result int
x,y;
x = 5;
y=2;
result = (float)x/y;
Hierarchy of Operators
Heirarchy means – it is a system of organizing people or things according to their
grading system.
Hierarchy of operators, also known as operator precedence, is the set of rules
that determines the order in which operators in a mathematical or programming
expression are evaluated.
For example
the arithmetic operators have the precedence and associativity in a
mathematical expression as follows Arithmetic operators are-
+, -, *, /, %
Precedence of above operators are *, /, % +, -
Precedence means which one would be processed the value first
in the expression. For example
100 + 200 /10 – 4 * 1
Hierarchy of above operators are as follows
100 + 200 /10 – 4 * 1
100 + 20 – 4 * 10
100 + 20 - 4 * 10
100 + 20 - 40
120-40
80