0% found this document useful (0 votes)
16 views22 pages

C Lecture 1.1

The document provides a comprehensive overview of the C programming language, including its definition, history, and characteristics. It explains the components of C such as tokens, keywords, identifiers, constants, operators, and data types, along with examples of simple C programs. Additionally, it covers formatted input/output, type conversion, and the significance of C in modern software development.

Uploaded by

devlina.karmakar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views22 pages

C Lecture 1.1

The document provides a comprehensive overview of the C programming language, including its definition, history, and characteristics. It explains the components of C such as tokens, keywords, identifiers, constants, operators, and data types, along with examples of simple C programs. Additionally, it covers formatted input/output, type conversion, and the significance of C in modern software development.

Uploaded by

devlina.karmakar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

Language vs.

Programming language
Language: Language is a way of communication between two persons in which both the persons need to understand
the meaning , which means a comfort zone must be created in between them.

Programming language: Programming language is a way of communication between man and machine which creates
a feasible way to communicate with machine.

Computer only understands binary number which is called low level language but human understands high level
language but computer needs always instruction to perform any task on it, which must be in a language that would be
understandable by human- computer both, when a computer needs more than one instruction it is called program. Now
these instructions must be written in a specific language, which is called programming language.
History of C
C language was developed by Dennis Ritchie at AT & T Bell laboratory, in 1972.
In earlier days before the invention of C FORTRAN, COBOL were used but these had lots of limitations, to fulfill the
deficit of these languages , C language was developed and after that it becomes the most flexible and favorable
language in most of the application/ software development field. The evolution of C language was like :
Year Language Used for
1957 FORTRAN Formula Translation language, scientific and mathematical
application
1960 COBOL Common Business Oriented language, business application
1967 BCPL Basic Combined programming language, general purpose but
had some issues
1970 B issues continued
1972 C General purpose programming language
Why to learn C
Primarily C language was used only to write Unix OS but later lots of application and system software were written, in C
language and it becomes a very friendly language to all.

Most of libraries of today’s OS are written in C like Unix, Linux, Windows, Mac, Android etc.
Most of language compiler(C,C++,Java, C#) are written in C language.
Oracle, MySQL these databases are designed using C.
Text editors like notepad, WordPad, MS Office are also written in C.
Device drivers for printer, scanner, phone, pen drive are written in C.
Some major parts of web browsers: Google Chrome, Internet Explorer are designed in C.
Virus and antiviruses are written in C.
Robotics, Embedded System, IOT are designed in C.
Characteristics of C

General purpose programming language.


Robust programming language.
Procedural oriented programming language.
Structured programming language.
Simple and small programming language.
Middle level programming language.
Machine independent programming language.( OS compatible)
Case sensitive programming language.
Dynamic memory management.
C character Set
For any language 1st it needs to learn letters, in C these letters are termed as character set. Combining letters words are
formed, which are called tokens in C. By maintaining grammatical rules sentences are formed using words like
instructions are generated using tokens where syntax rules are followed. Finally collection of sentences are known as
paragraph, alike collection of instructions are called programs. So here:
Letters are termed as character set
Words are termed as tokens
Sentences are termed as instructions
Paragraph are termed as programs
C character set follows 256 characters, these are:-
Alphabets-upper(A-Z) & lower case(a-z)
Decimal digits-0 – 9
Special symbols- +,-,=,(,),{,},[,],*,;,,,:
White spaces- blank space, tab space, \t, \r, \n (these are called escape sequences)
C character set supports ASCII(American Standard Code for Information Interchange) code.
Tokens
Tokens are the smallest individual unit of any program. Every instruction of a program is collection of tokens.
Tokens

Keywords
White spaces
Identifiers
Constants Operators Special symbolsString literals
Keywords: these are special pre defined words by language inventor , keywords are used only for their intended purpose,
always written in lowercase, keywords cant be re defined by the programmer. There are total 32 keywords in C language,
sometimes these are called reserved words.
List of 32 keywords:
auto continue enum if short break default extern
int signed case do float long sizeof char
double for register static const else goto return
struct switch typedef union unsigned void volatile while

Identifiers: these are user defined names given to array, variable, function, structure , union, files etc. All alphabets (A-Z/a-
z) , all digits(0-9) are acceptable to form the name of an identifier. But it can’t be started with any digit, underscores are
allowed but no blank spaces are allowed , identifiers must not be any reserved words. Length of any identifier depends on
machine, but max limit is 32 characters.

Constants/ Literals: it refers to a fixed value which doesn’t change during program execution. Constants are of 2 types:
integer numeric constant and character constant.
Constant

numeric character

integer real
single character string character
(5,-10,-100) (7.5,0.95, -10.39) (‘a’, ‘(‘, ‘$’ ) (“abc”, “ “)
When any c program is executing compiler needs to know about the format specifier of all constants like:
%d – decimal integer %f – fractional equivalent
%c- string character %s- string constant
Special symbols: c uses some special symbols for writing programs to separate all the tokens from each other to identify them
uniquely. These are:

;- semicolon used to terminate any instruction


:-colon used to printing any statement or value
;-comma used to separate identifiers from keywords
{ }- curly braces used to identify the execution part of program
( )- parenthesis used to print and scanning data
[ ]- square bracket used to mainly array declaration
=- equals used to initialize value of any variable
==- is equal to used to compare values
!=- is not equal to used to compare values
“ “- double quote used to declare string constant
‘ ‘- single quote used to declare single character constant
Operators: an operator is a symbol that tells the processor to perform certain mathematical or logical or bitwise
manipulations.
Operators are implemented with operands, in C operators are of 3 types, these are:
Mathematical operators: +,-,*,/,%, = etc. are used to perform any mathematical operations.(a+b, a*b)
Logical operators: &&,||, ! etc. are used to perform logics in a program. (a&&b, a||b)
Bitwise operator: &, |, ~, <<, >>, ^ etc. are used for bitwise operation. (a|b, a^b)
Apart from these 3 some special operators are used in c , sizeof , type.

Operator precedence and associativity: precedence determines which operator is performed first in an expression with more
than one operator and associativity is used when 2 operators of same precedence appear in an expression.
Operator precedence and associativity
Operator Description Associativity
() Parenthesis or function
call
[] Brackets or array subscript
. Dot or member selection Left to right
operator
-> Arrow operator
++ -- Postfix increment
/decrement
+- Unary plus/minus
type typecasting
* Dereference operator Right to left
& Address of operator
Sizeof Determine size in bytes
+-*/% Arithmetical operator Left to right
<<, >>, <, <=, >, >=, ==, Bitwise operator Left to right
&, |,^
&&, ||, ! Logical operator Left to right
?: Ternary operator Right to left
= Assignment operator Right to left
Variable: variable is the name of memory location, it acts as a container, it always holds some values which are
changeable.
Ex: int a= 5, here int is integer data type, a is the variable name which is called identifier , the value is 5, which can be
changed by replacing any value.

Data type: it defines the type of any data used in C language to declare any variable to identify the value of it, data types
massively used in c, are of 2 types: primary or system defined data type and secondary or user defined data type
Primary data types: int 2 byte
float 4 byte
char 1 byte
void no size
Secondary data types: array
pointer
structure
union
Formatted input output and header files in C
In C language to read and write data some functions are used which has a specific format , these are:
printf()- function for printing data or writing data
scan()- function for taking input data from user or scanning or reading data
C language has it’s own library which consists of different header files under which different system defined functions are
created.
C library

stdio.h dos.h
printf() conio.h string.h math.h
scanf() clrscr() strlen() sqrt()
getch() strcpy() sin(), cos()
Syntax of C program
Type conversion in C
Type conversion refers the changing an entity of one data type to another. There are two types of data conversion : implicit
and explicit type conversion
implicit type conversion: it is done by compiler automatically, when smaller data types are automatically converted into
larger data types this is called type promotion.
Ex: double to int, float to int etc.
double d, int a,
d= (int)d
explicit type conversion: when smaller data types are converted into larger data types, which are done by user are called
explicit type conversion.
Ex: int to float, char to int
a= (float)a
Some simple programs in C :
WAP in C for 2 numbers to add, subtract, multiply and divide these numbers taking input from keyboard.
#include<stdio.h>
#include<conio.h>
int main()
{
int a,b,c,add,sub,mul,div; // a,b,c are integer variable; add,sub,mul,div are variables to store the results of all operations.
printf("enter values of a and b");
scanf("%d%d",&a,&b); // &a,&b are address of operator
add=a+b;
sub=a-b;
mul=a*b;
div=a/b;
printf("add=%d,sub=%d,mul=%d,div=%d",add,sub,mul,div);
return 0;
getch();

}
WAP to convert temperature from Fahrenheit to Celsius.
#include<stdio.h>
#include<conio.h>
int main()
{
float c,f;
printf("enter the temp in farenheit"); // temperature is a floating number
scanf("%f",&f);
c=((f-32)*5)/9;
printf("c=%f",c);
return 0;
getch();
}
WAP to find simple interest by taking principal amount, rate of interest and time taking from keyboard.
#include<stdio.h>
#include<conio.h>
int main()
{
float p,r,t,si;//si is simple interest
printf("enter values of p,r,t");
scanf("%f%f%f,&p,&r,&t");
si=(p*r*t)/100;
printf("simple interest=%f",si);
return 0;
getch();

}
WAP to calculate area and perimeter of a rectangle.
#include<stdio.h>
#include<conio.h>
int main()
{
float len,bre,area,peri;
printf("enter length and bredth");
scanf("%f%f",&len,&bre);
area=len*bre;
printf("the area is=%f",area);
peri=2*(len+bre);
printf("the perimeter is=%f",peri);
return 0;
getch();

}
WAP to obtain sum of first and last digit of a four digit number.
#include<stdio.h>
#include<conio.h>
int main()
{
int n,f,l,a;
printf("enter 4 digit number");
scanf("%d",&n);
f=n/1000;
l=n%10;
a=f+l;
printf("the sum is=%d",a);
return 0;
getch();
}
WAP to calculate gross salary if gross=basic +da +hra, da=20% of basic, hra=10% of basic
#include<stdio.h>
#include<conio.h>
int main()
{
float basic,hra,da,gross;
printf("enter value of basic");
scanf("%f",&basic);
hra=basic*0.1;
da=basic*0.2;
gross=basic+da+hra;
printf("the gross salary is=%f",gross);
return 0;
getch();
}
WAP to swap the values of two numbers without using third variable.
#include<stdio.h>
#include<conio.h>
int main()
{
int a,b;
printf("enter values of a and b");
scanf("%d%d",&a,&b);
printf("before swapping values are %d\n%d",a,b);
a=a+b;
b=a-b;
a=a-b;
printf("\nafter swapping values are=%d\n%d",a,b);
return 0;
getch();
}

You might also like