[go: up one dir, main page]

0% found this document useful (0 votes)
25 views62 pages

C Basic

Uploaded by

adityabose8434
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)
25 views62 pages

C Basic

Uploaded by

adityabose8434
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/ 62

By

Syed Ahsan Shabbir


Asst. Prof (IT), HoD(CSE)
NSU
History of C Language

➢C programming language was developed in 1972 by Dennis Ritchie at bell laboratories of


AT&T (American Telephone & Telegraph), located in the U.S.A.
➢Dennis Ritchie is known as the founder of the c language.
➢It was developed to overcome the problems of previous languages such as B, BCPL, etc.
➢Initially, C language was developed to be used in UNIX operating system.
➢It inherits many features of previous languages such as B and BCPL.
➢Most of the UNIX operating system is coded in 'C'.
➢Initially 'C' programming was limited to the UNIX operating system, but as it started
spreading around the world, it became commercial.
Founder or Father of C Language
➢ C was initially designed for programming UNIX operating system.
➢Now the software tool as well as the C compiler is written in C.
➢Major parts of popular operating systems like Windows, UNIX, Linux is still written in C.
➢This is because even today when it comes to performance (speed of execution) nothing
beats C.

❑Program:
➢There is a close analogy between learning English language and learning C language.
➢The classical method of learning English is to first learn the alphabets used in the language,
then learn to combine these alphabets to form words, which in turn are combined to form
sentences and sentences are combined to form paragraphs.
➢Learning C is similar and easier.
➢Instead of straight-away learning how to write programs, we must first know what
alphabets, numbers and special symbols are used in C, then how using them constants,
variables and keywords are constructed, and finally how are these combined to form an
instruction.
➢A group of instructions would be combined later on to form a program.
➢ So a computer program is just a collection of the instructions necessary to solve a
specific problem.
➢The basic operations of a computer system form what is known as the computer’s
instruction set. And the approach or method that is used to solve the problem is known as
an algorithm.
➢ So for as programming language concern these are of two types.
1) Low level language 2) High level language
➢Low level language:
➢ Low level languages are machine level and assembly level language.
➢In machine level language computer only understand digital numbers i.e. in the form of 0
and 1.
➢So, instruction given to the computer is in the form binary digit, which is difficult to
implement instruction in binary code.
➢This type of program is not portable, difficult to maintain and also error prone.
➢The assembly language is on other hand modified version of machine level language.
➢Where instructions are given in English like word as ADD, SUM, MOV etc. It is easy to
write and understand but not understand by the machine. So the translator used here is
assembler to translate into machine level.
❑High level language:
❖These languages are machine independent, means it is portable.
❖The language in this category is Pascal, Cobol, Fortran etc.
❖High level languages are understood by the machine. So it need to translate by the
translator into machine level.
❖A translator is software which is used to translate high level language as well as low
level language in to machine level language.
❖Three types of translator are there:
➢Compiler
➢Interpreter
➢Assembler
➢ Compiler and interpreter are used to convert the high level language into machine level
language.
➢The program written in high level language is known as source program and the
corresponding machine level language program is called as object program.
➢Both compiler and interpreter perform the same task but there working is different.
➢Compiler read the program at-a-time and searches the error and lists them.
➢If the program is error free then it is converted into object program.
➢When program size is large then compiler is preferred.
➢Whereas interpreter read only one line of the source code and convert it to object code.
➢If it check error, statement by statement and hence of take more time.
Let's see the programming languages that were developed before C
language.

Language Year Developed By


Algol 1960 International Group
BCPL 1967 Martin Richard
B 1970 Ken Thompson
Traditional C 1972 Dennis Ritchie
K&RC 1978 Kernighan & Dennis Ritchie
ANSI C 1989 ANSI Committee
ANSI/ISO C 1990 ISO Committee
❑Integrated Development Environments (IDE):
➢ The process of editing, compiling, running, and debugging programs is often managed by
a single integrated application known as an Integrated Development Environment, or IDE
for short.
➢An IDE is a windows-based program that allows us to easily manage large software
programs, edit files in windows, and compile, link, run, and debug programs.
➢On Mac OS X, CodeWarrior and Xcode are two IDEs that are used by many
programmers.
➢Under Windows, Microsoft Visual Studio is a good example of a popular IDE.
➢Kylix is a popular IDE for developing applications under Linux.
Features of C
➢It is a very simple and easy language, C language is mainly used for develop desktop
based application.
➢All other programming languages were derived directly or indirectly from C
programming concepts. This language have following features;
❑Simple:
➢Every c program can be written in simple English language so that it is very easy to
understand and developed by programmer.
➢C is renowned for its simplicity and is easy to use because of its structured
approach.
➢It has a vast collection of keywords, operators, built-in functions and data types
which make it efficient and powerful.
❑Platform dependent
➢A language is said to be platform dependent whenever the program is executed in
the same operating system where that was developed and compiled but not run
and execute on other operating system. C is platform dependent programming
language.
❑Portable
➢C is portable, which means a C program runs in different environments.
➢C compilers are available for all operating systems and hardware platforms.
Additionally, you can easily write code on one system and port it to another.
➢When we can copy .exe file to any other computer which contain window operating
system then it works properly, because the native code of application an operating
system is same. But this exe file is not executed on other operating system.
❑Mid-level programming language:
➢C is also used to do low-level programming. It is used to develop system applications
such as kernel, driver etc.
➢It also supports the feature of high level language. That is why it is known as mid-
level language.

❑Structured programming language:


❖C is a structured programming language in the sense that we can break the program
into parts using functions. So, it is easy to understand and modify.
❑Rich Library:
➢C provides a lot of inbuilt functions that make the development fast.

❑Extensible:
➢C language is extensible because it can easily adopt new features.

❑Recursion:
➢In c, we can call the function within the function. It provides code reusability for
every function.
❑Pointer:
➢C provides the feature of pointers. We can directly interact with the memory by using
the pointers. We can use pointers for memory, structures, functions, array, etc.

❑Speed:
➢C is super fast. The compilation and execution of programs is much faster on C than
with most other languages.

❑Memory Management:
➢It supports the feature of dynamic memory allocation. In C language, we can free
the allocated memory at any time by calling the free() function.
❑Structure of C Language program
1 ) Comment line
2) Preprocessor directive
3 ) Global variable declaration
4) main function( )
{
Local variables;
Statements;
}
User defined function
}
}
❑Comment line
➢It indicates the purpose of the program.
➢It is represented as

// Single line comment

/*………………*/ Multiline comment


➢Comment line is used for increasing the readability of the program.
➢It is useful in explaining the program and generally used for documentation.
➢Comment line can be single or multiple line.
❑Preprocessor Directive:
➢#include<stdio.h> tells the compiler to include information about the standard input/output
library.
➢It is also used in symbolic constant such as #define PI 3.14(value).
➢The stdio.h (standard input output header file) contains definition &declaration of system
defined function such as printf( ), scanf( ), pow( ) etc.
➢Generally printf() function used to display and scanf() function used to read value.
❑Global Declaration:
➢This is the section where variable are declared globally so that it can be access by all the
functions used in the program. And it is generally declared outside the function :

❑ main( )
➢Every function has one main() function from where actually program is started and it is
encloses within the pair of curly braces.
➢The main( ) function can be anywhere in the program but in general practice it is placed in
the first position.
printf() and scanf() in C
❖The printf() and scanf() functions are used for input and output in C language. Both
functions are inbuilt library functions, defined in stdio.h (header file).

➢printf() function
❖The printf() function is used for output. It prints the given statement to the console.

➢scanf() function
❖The scanf() function is used for input. It reads the input data from the console.
❖The main function does not return any value when void (means null/empty) as
void main(void ) or void main()
{
printf (“C language”);
}
Output: C language
❖The program execution start with opening braces and end with closing brace.
❖And in between the two braces declaration part as well as executable part is
mentioned.
❖And at the end of each line, the semi-colon is given which indicates statement
termination.
C programming Basic Structure
Example - hello world program

#include <stdio.h>
int main()
{
printf("Hello World");
return 0;
}
❑Character set
➢A character denotes any alphabet, digit or special symbol used to represent
information. Valid alphabets, numbers and special symbols allowed in C are
There are four types of Character Set:-

Character Set
Uppercase A-Z
1. Letters
Lowercase a-z
2. Digits All digits 0-9
All Symbols: , . : ; ? ' " ! | \ / ~ _$ % # &
3. Special Characters
^*-+<>(){ }[]
Blank space ‘ ', Horizintal tab '\t',
White Spaces
4. Carriage return, New line '\n', Form
Know more
feed
❑Keywords
➢Keywords are reserved words. All keywords have fixed meanings.
➢Keywords serve as basic building blocks for program statements.
➢In ‘C’, there are 32 keywords. All keywords must be written in lower case.
auto double int struct
break else long switch
case enum register typedef
char extern return union
const short float unsigned
continue for signed void
default goto sizeof volatile
do if static while
❑Identifiers
➢A C identifier is a name used to identify a variable, function, or any other user-
defined item.
➢An identifier starts with a letter A to Z, a to z, or an underscore '_' followed by
zero or more letters, underscores, and digits (0 to 9).
➢C does not allow punctuation characters such as @, $, and % within identifiers.
➢C is a case-sensitive programming language.
➢Thus, Syed and syed are two different identifiers in C.
➢Here are some examples of acceptable identifiers −
mohd zara abc move_name a_123
myname50 _temp j a23b9 retVal
❑Rule for defining identifiers:
1. An alphabet or underscore used in the starting letter of identifiers.
For example: int _sum, sum_amount;
Here sum and sum_amount both are valid identifiers.
2. An identifier cannot be keywords or standard function name.
For example: int else;
Here int else invalid identifier because else is keyword.
3. An identifier length up to 32 characters allowed.
4. Only underscore in special characters is allowed.
For example: int sum_amount;
Here sum_amount is valid identifier.
5. Identifiers are case sensitive i.e. uppercase letters and lower case
letters are different.
For example: int sum, Sum; Here, sum and Sum both are different
identifiers.
❖As the name says, identifiers are used to identify a particular element in a program.
❖Each identifier must have a unique name. Following rules must be followed for
identifiers:
➢The first character must always be an alphabet or an underscore.
➢It should be formed using only letters, numbers, or underscore.
➢A keyword cannot be used as an identifier.
➢It should not contain any whitespace character.
➢The name must be meaningful.
o Example of invalid identifiers
✓2sum (starts with a numerical digit)
✓int (reserved word)
✓char (reserved word)
✓m+n (special character, i.e., '+')
Differences between Keyword and Identifier

Keyword Identifier
Keyword is a pre-defined word. The identifier is a user-defined word
It must be written in a lowercase letter. It can be written in both lowercase and
uppercase letters.
Its meaning is pre-defined in the c Its meaning is not defined in the c
compiler. compiler.
It is a combination of alphabetical It is a combination of alphanumeric
characters. characters.
It does not contain the underscore It can contain the underscore character.
character.
❑Constants in C
➢A constant is a value or variable that can't be changed in the program, for example:
10, 20, 'a', 3.4, "c programming" etc.
➢There are different types of constants in C programming.
➢List of Constants in C
Constant Example
Decimal Constant 10, 20, 450 etc.

Real or Floating-point Constant 10.3, 20.2, 450.6 etc.

Octal Constant 021, 033, 046 etc.

Hexadecimal Constant 0x2a, 0x7b, 0xaa etc.

Character Constant 'a', 'b', 'x' etc.

String Constant "c", "c program",


❑2 ways to define constant in C
❖There are two ways to define constant in C programming.
const keyword
#define preprocessor
1) C const keyword
❖The const keyword is used to define constant in C programming.
const float PI=3.14;
Now, the value of PI variable can't be
changed.

Output:
The value of PI is: 3.140000
➢If you try to change the value of PI, it will render compile time error.

Output:

Compile Time Error: Cannot modify a const object


2) #define preprocessor

Output:

3.140000
❑Variables
A variable is a name of storage area for holding data and variable data can vary
during the execution of the program.
❖For example: The statement in C is given by
int sum;
❖Indicate that int represent integer data type. sum is variable name.
❑Rules for defining Variable Name
1. An alphabet or underscore used in the starting letter of variable name.
2. A variable name cannot be keywords or standard function name.
3. A variable name length is compiler dependent.
4. Only underscore in special characters is allowed.
5. Variable name is case sensitive i.e. uppercase letters and lower case letters are different.
❑Data Types in C
➢A data type specifies the type of data that a variable can store such as integer,
floating, character, etc.
❑Fundamental Data types
➢These data types are predefined data types in C compiler shown below,
The memory size of the basic data types may change according to 32 or 64-bit operating system.
Let's see the basic data types. Its size is given according to 32-bit architecture.
Data Types Memory Size Range
char 1 byte −128 to 127
signed char 1 byte −128 to 127
unsigned char 1 byte 0 to 255
short 2 byte −32,768 to 32,767
signed short 2 byte −32,768 to 32,767
unsigned short 2 byte 0 to 65,535
int 2 byte −32,768 to 32,767
signed int 2 byte −32,768 to 32,767
unsigned int 2 byte 0 to 65,535
short int 2 byte −32,768 to 32,767
signed short int 2 byte −32,768 to 32,767
unsigned short int 2 byte 0 to 65,535
long int 4 byte -2,147,483,648 to 2,147,483,647
signed long int 4 byte -2,147,483,648 to 2,147,483,647
unsigned long int 4 byte 0 to 4,294,967,295
float 4 byte
double 8 byte
Operators in C Language
❖An operator is a symbol that defines the kind of operation that compiler has to
perform.
❖C program having various types of operators are classified as
❑Arithmetic operators
❑Assignment operators
❑Increment and decrement operators
❑Relational operators
❑Logical operators
❑Ternary/conditional operators
❑Bitwise operators
❑Special operators
❑ Arithmetic Operators
➢Arithmetic operator used to perform arithmetic operation on variables or constants.
➢For explanation purpose, we will discuss arithmetic operators in table form as
shown below,
Example: Arithmetic Operators
❑Assignment Operators:
➢Assignment operators, assign a value to variable, constant or expression.
➢The most common assignment operator is =
Operator Example Same as
= a=b a=b
+= a += b a = a+b
-= a -= b a = a-b
*= a *= b a = a*b
/= a /= b a = a/b
%= a %= b a = a%b
Example: Assignment Operators

Output
c=5
c = 10
c=5
c = 25
c=5
c=0
C – Increment/Decrement Operators
➢Increment operators are used to increase the value of the variable by one and
decrement operators are used to decrease the value of the variable by one in C
programs.
Syntax:
Increment operator: ++var_name; (or) var_name++;
Decrement operator: – -var_name; (or) var_name – -;

Example:
Increment operator : ++ i ; i ++ ;
Decrement operator : – – i ; i – – ;
printf("decrement, a-- & b++, =a=%d,b=%d\n",a,b);
c=++a;
printf("Pre increment, c=++a, c=%d,a=%d\n",c,a);
#include<stdio.h> c=a++;
#include<conio.h>
printf("Post increment, c=a++, c=%d,a=%d\n",c,a);
d=--b;
void main(void) printf("Pre decrement, d=--b, d=%d,b=%d\n",d,b);
{ d=b--;
int a=10,b=20,c,d; printf("Post decrement, d=b--, d=%d,a=%d\n",d,b);
getch();
printf("a=%d,b=%d\n",a,b);
}
a++;
b++;
printf("increment, a++ & b++, a=%d,b=%d\n",a,b);
a--;
b--;
❑C Relational Operators
➢A relational operator checks the relationship between two operands.
➢If the relation is true, it returns 1; if the relation is false, it returns value 0.
➢Relational operators are used in decision making and loops.
Example: Relational Operators
❑ Logical Operators
➢An expression containing logical operator returns either 0 or 1 depending upon
whether expression results true or false.
➢Logical operators are commonly used in decision making in C programming.
Operator Meaning Example
If c = 5 and d = 2 then,
Logical AND. True only if all
&& expression ((c==5) &&
operands are true
(d>5)) equals to 0.

If c = 5 and d = 2 then,
Logical OR. True only if either one
|| expression ((c==5) ||
operand is true
(d>5)) equals to 1.

Logical NOT. True only if the If c = 5 then, expression


!
operand is 0 !(c==5) equals to 0.
Example 5: Logical Operators
Ternary/Conditional Operator
➢If any operator is used on three operands or variable is known as Ternary Operator. It can
be represented with ? : . It is also called as conditional operator
➢The ternary operator is an operator that takes three arguments.
➢The first argument is a comparison argument, the second is the result upon a true
comparison, and the third is the result upon a false comparison.
➢Ternary operator is shortened way of writing an if-else statement.
➢Ternary operator is a?b:c it say that the condition a is true b will be executed else c will be
executed.
➢The conditional operator is kind of similar to the if-else statement as it does follow the
same algorithm as of if-else statement but the conditional operator takes less space and
helps to write the if-else statements in the shortest way possible.
Example: Ternary Operator
#include <stdio.h>
int main()
{
#include <stdio.h> int age;
printf("Enter your age");
int main() scanf("%d",&age);
{ (age>=18)? (printf("eligible for voting")) : (printf("
not eligible for voting"));
int a = 10, b = 20, c; return 0;
c = (a < b) ? a : b; }
printf("%d", c);
}
❑ Bitwise Operator
The bitwise operators are the operators used to perform the operations on the data at
the bit-level. Always remember one thing that bitwise operators are mostly used with the
integer data type.
❖ When we perform the bitwise operations, then it is also known as bit-level programming.
❖ It consists of two digits, either 0 or 1. It is mainly used in numerical computations to make
the calculations faster.
Operator Meaning of operator
& Bitwise AND operator
| Bitwise OR operator
^ Bitwise exclusive OR operator
~ One's complement operator (unary operator)
<< Left shift operator
>> Right shift operator
The result of the computation of bitwise logical operators is shown in the table given
below.

x y x&y x|y x^y

0 0 0 0 0

0 1 0 1 1

1 0 0 1 1

1 1 1 1 0
We have two variables a and b.
a =6;
#include <stdio.h> b=4;
The binary representation of the above two
int main() variables are given below:
{ a = 0110
b = 0100
int a=6, b=14; When we apply the bitwise AND operation
printf("The output of the Bitwise A in the above two variables, i.e., a&b, the out
put would be:
ND operator a&b is %d",a&b);
Result = 0100
return 0;
}
❑C – Special Operators:
❖These operators are used to perform the particular type of operation and they are:
a. Comma operator (,)
b. Size of operator
➢Comma operator (,):
❖This operator uses to join two expressions and make the program more compact.
For example: Let us consider two statements:
int a=5;
int b=10;
❖The above two statements are written using comma (,) operator as:
int a=5, b=10;
➢Size of operator:
❖This operator returns the size in bytes occupied by operand in memory. The
operand may be a variable, a constant or a data qualifier.
❖The general form is:
sizeof(operand); #include <stdio.h>
#include <conio.h>
void main()
{
printf("Size of integer is %d\n",sizeof(int));
printf("Size of float is %d\n",sizeof(float));
getch();
}
Difference Between Header File And Library
HEADER FILES LIBRARY FILES
They have the extension .h They have the extension .lib

They contain function declaration. They contain function definitions


They are available inside “include sub They are available inside “lib sub
directory” which itself is in Turbo directory” which itself is in Turbo
compiler. compiler.
Library files are non human readable.
Header files are human readable.Since
Since they are in the form of machine
they are in the form of source code.
code.

Header files in our program are included Library files in our program are included
by using a command #include which is in last stage by special software called as
internally handle by pre-processor. linker.
Thanks!!!

You might also like