You are on page 1of 51

Department of

Unit-2 Computer Engineering-


Basic of C language Diploma

CP
09CE1104

Prof. Divyarajsinh Parmar


 History of C
 Features of C Language
Objectives  Basic Structure of C Programs
 Steps of Program Execution(Compilation And Execution)
 C Tokens
History of C
 Robust Language
 Built-in-Functions
Features of  Well structured Programming Language
 Efficient and Fast
C Language
 Highly Portable
 Ability to extend
Documentation Section
Link Section
Definition Section
Global Declaration Section
void main() //Function Section

Basic {

Structure of Declaration part


Executable part
C Programs
}
Subprogram section
Function 1
Function 2 (User-defined Functions)
……
#include<stdio.h>
#include<conio.h>
void main()
Simple C {
Program printf(“Hello World..”);
}
Steps of
Program
Execution
(Compilation
And
Execution)
#include<stdio.h>
void main()
{
Example printf(“Hello World..”);
}
#include<stdio.h>
void main() Output
{
Example printf(“Hello World..”);
}

Hello World..
#include<stdio.h> Output
void main()
{
Hello World..How are you?
Example printf(“Hello World..”);
printf(“How are you?”);
}
#include<stdio.h> Output
void main()
{ Hello World..
How are you?
Example printf(“Hello World..”);
printf(“\nHow are you?”);
}
 As every language contains a set of characters used to construct
words, statements, etc., C language also has a set of characters
which include alphabets, digits, and special symbols. C language
supports a total of 256 characters.

Character set
 C language character set contains the following set of characters...
in C  Alphabets
 Digits
 Special Symbols
 Alphabets
 C language supports all the alphabets from the English language. Lower
and upper case letters together support 52 alphabets.
 lower case letters - a to z
 UPPER CASE LETTERS - A to Z

 Digits
Character set  C language supports 10 digits which are used to construct numerical
values in C language.
in C  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.,
 Individual words and punctuation marks in C program are called as
C tokens.
OR
 A token is the smallest unit in a program.

#include<stdio.h>
C Tokens void main()
{
printf (“Hello World..”) ;
}
C Tokens
 Keywords are C tokens that have a strict meaning.
 They are explicitly reserved and cannot be redefined.
 ANSII C has 32 key words.
C Keywords
auto double int struct

break else long switch

Keywords case enum register typedef

char extern return union

continue for signed void

do if static while

default goto sizeof volatile

const float short unsigned


 Refers to the names of variables, functions and arrays.
 Rules for defining identifiers:
 First character must be an alphabet (or underscore).
 Must contain only letters, digits or underscore.
Identifiers  Only first 31 characters are significant.
 Cannot use a special keyword.
 Must not contain white space.
 Constants represent fixed values.

Constants
 Integer Constants
 Floating-point Constants
Constants  Character Constants
 String Constants
 Consists a sequence of digits.
 Types of Integers:
Integer  Decimal -> 123
 Octal -> 0463
Constants  Hexadecimal -> 0x8F
 These represent numbers containing fractional parts.
Floating
 Example: 2.36
point -97.2354
Constants
 These represent a single character value.
Example: ‘A’
Character ‘b’
Constants ‘@’
 A sequence of characters which are enclosed in double quotes.
String  Example: “Computer”
“12345”
Constants
 Symbolic representation of a value
 Used to store values in program.
 It’s value changes as the program statements are
executed.
Variables  Example:
x
 balance
 User_name
 A variable definition specifies a data type and contains a list of one or
more variables of that type as follows −
 Syntax: type variable_list;

 Here, type must be a valid C data type including char, w_char, int, float,
double, bool, or any user-defined object; and variable_list may consist of
one or more identifier names separated by commas. Some valid
Variable declarations are shown here −
 int i, j, k;
Declaration  char c, ch;
 float f, salary;
 double d;

 Variables can be initialized (assigned an initial value) in their declaration.


The initializer consists of an equal sign followed by a constant expression
as follows −
 Syntax: type variable_name = value;
 C language supports 4 fundamental data types :

Data-Types in
C
 Some other data types :

Data-Types in
C
 Symbol that indicates the operation or activity to be performed.
 Example
 + means addition like A+B
Operators  Here, A,B called operands
 + called operator
Operators Unary operator : single operand only, like ++A, -A

Binary operator : double operand only, like A+B, A/B

Ternary operator : triple operand only, like A?B:C


Types of
Operator
 Performs arithmetic operations

Arithmetic
Operators
 Used to compare two operands

Relational
Operators
 && (AND operation)
 In C form : condition-1 && condition-2
 Example :

Logical
Operators
 || (OR operation)
 In C form : condition-1 || condition-2
 Example :

Logical
Operators
 ! (NOT operation)
 In C form : !condition-1
 Example :

Logical
Operators
 It copy/store, thus assigning value (It is not exchange the value)
 hence the name is assignment operator.

Assignment Syntax : Variable = expression

Operators
Example
𝑋 = 𝑌;
𝑋 = 4 + 5;
𝑃𝑖 = 3.1416
Shorthand
Operators
++A (Pre-increment)
Increment/
A=A+1
Decrement
Operators
A++ (Post-increment)
Increment/
Decrement
Operators
 Also known as ternary operator.

Conditional
Operator
Bitwise
Operator
 Comma operator
 value = (x=10, y=5, x+y);
 sizeof operator
 Give size of particular variable in memory
Special  Example:
Operators  sizeof(char) is 1 byte
 sizeof(int) is 2 bytes
 Address operator (&)
 Standard input functions:
 scanf()
 getchar()
 gets()
 getch()
I/O Function  Standard output functions:
 printf()
 putchar()
 puts()
int main()
{
char c;
printf("n Please enter a character:");
I/O Function c=getchar();
printf("n The character entered is: ");
putchar(c);
return 0;
}
void main()
{
/* character array of length 100 */
char str[100];
I/O Function printf("Enter a string");
gets( str );
puts( str );
getch();
}
 When a data type is converted into another data type by a
programmer or user while writing a program code of any
programming language, the mechanism is known as type
casting. The programmer manually uses it to convert one
data type into another. It is used if we want to change the
target data type to another data type. Remember that the
Type casting destination data type must be smaller than the source data
type. Hence it is also called a narrowing conversion.
 Syntax: Destination_datatype = (target_datatype) variable;
 (data_type) it is known as casting operator

Type casting  Example:


float b = 3.0;
int a = (int) b; // converting a float value into integer
 If a data type is automatically converted into another
data type at compile time is known as type conversion.
The conversion is performed by the compiler if both
data types are compatible with each other. Remember
that the destination data type should not be smaller than
the source type. It is also known as widening conversion
Type of the data type.
conversion
Example:

int a = 20;
Type Float b;
conversion b = a; // Now the value of variable b is 20.000
/* It defines the conversion of int data type to float data
type without losing the information. */
Conversion
Order
That’s All

You might also like