Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

c programme all short notes, Lecture notes of C programming

good for all programming aspirents

Typology: Lecture notes

2023/2024

Uploaded on 09/17/2024

naveen-ksc
naveen-ksc 🇮🇳

1 document

1 / 101

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
C-Language Theory Naresh IT 1
C-Language Basics V.Shiwa Chaithanya
C-LANGUAGE
Features of C-Language:
C-Language provides following Features:
General Purpose Programming Language
Middle Level Programming Language
Modularity (Or) Procedure-Oriented Programming [POP] Language
Portability
Extendibility
General Purpose Programming Language:
Using C-Language, we can develop many kinds of applications such as
Business, Scientific, Mathematical and Graphical Applications. That is why
C-Language is called General Purpose Programming Language.
Middle Level Programming Language:
C-Language has both High-Level Language Features and Low-Level
Language Features. That is why C-Language is called “Middle Level
Programming Language”. High Level Languages are suitable to develop the
Application Software. Low Level Languages are suitable to develop the
System Software. Using C-Language, we can develop System Software and
Application Software. “UNIX” Operating System, Many Programming
Languages Compilers & Interpreters [System Software s] developed in C-
Language. Google Chrome, Photoshop, Oracle & MS Office [Application
Software s] developed in C-Language.
Modularity (Or) Procedure-Oriented Programming Language [POP
Language]:
Modular Programming Approach (Or) Procedure -Oriented Programming
Approach is a programming style in which we write a program in the form
of Functions. In Some Languages we call them as modules (or) Procedures
(or) Sub Routines. We can also call them as Sub Programs. C-Program is
written in the form of Functions. That is why C-Language is called
“Procedure-Oriented Programming Language” (Or) “Modular
Programming Language.
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b
pf3c
pf3d
pf3e
pf3f
pf40
pf41
pf42
pf43
pf44
pf45
pf46
pf47
pf48
pf49
pf4a
pf4b
pf4c
pf4d
pf4e
pf4f
pf50
pf51
pf52
pf53
pf54
pf55
pf56
pf57
pf58
pf59
pf5a
pf5b
pf5c
pf5d
pf5e
pf5f
pf60
pf61
pf62
pf63
pf64

Partial preview of the text

Download c programme all short notes and more Lecture notes C programming in PDF only on Docsity!

C-Language Basics V.Shiwa Chaithanya

C-LANGUAGE

Features of C-Language:

C-Language provides following Features: General Purpose Programming Language Middle Level Programming Language Modularity (Or) Procedure-Oriented Programming [POP] Language Portability Extendibility

General Purpose Programming Language: Using C-Language, we can develop many kinds of applications such as Business, Scientific, Mathematical and Graphical Applications. That is why C-Language is called General Purpose Programming Language.

Middle Level Programming Language: C-Language has both High-Level Language Features and Low-Level Language Features. That is why C-Language is called “Middle Level Programming Language”. High Level Languages are suitable to develop the Application Software. Low Level Languages are suitable to develop the System Software. Using C-Language, we can develop System Software and Application Software. “UNIX” Operating System, Many Programming Languages “Compilers & Interpreters” [System Software s] developed in C- Language. Google Chrome, Photoshop, Oracle & MS Office [Application Software s] developed in C-Language.

Modularity (Or) Procedure-Oriented Programming Language [POP Language]: Modular Programming Approach (Or) Procedure -Oriented Programming Approach is a programming style in which we write a program in the form of Functions. In Some Languages we call them as modules (or) Procedures (or) Sub Routines. We can also call them as Sub Programs. C-Program is written in the form of Functions. That is why C-Language is called “Procedure-Oriented Programming Language” (Or) “Modular Programming Language”.

C-Language Basics V.Shiwa Chaithanya

Advantages with Modularity are: It improves understandability. It allows reusability. It decreases length of code.

Portability: The Application developed in one System using C-Language can run on any other System even if Operating System is different. If Operating System is same in another system, then place .exe file. If Operating System is different, then place Source Code File ( .c file ).

Extendibility: We can extend the C-Library by adding our own header files. C-Language provides C-Library to us to develop the C-Programs. C-Library is a collection of Header Files where Header File is a collection of predefined functions.

C-Language Basics V.Shiwa Chaithanya

main() Function:

“main()” is a user-defined function in which we can write a set of statements. It is entry point of the program. Every C-Program execution starts from “main” function. Because it is entry point of the program. Its return type is “int / void” in C-Language. It can take two arguments. First argument is “int” type. Second argument is “char*” type array. First argument takes number of arguments. Second argument takes a set of strings. Passing arguments to main function is optional. We can pass arguments to main function from MS DOS command prompt. This mechanism is called “Command Line Arguments”. Writing arguments for “main” function is optional.

printf(): printf() is a predefined function or built-in function included in “stdio.h” header file. It is used to print the data on console screen [Output Screen].

Example-1: printf(“hello”); //prints hello

Example-2: int x=20; printf(“x=%d”,x); //prints x=

Program to print “hello” on Console Screen: #include<stdio.h> main() { printf("hello"); }

Output: Hello

C-Language Basics V.Shiwa Chaithanya

Escape Sequences (Or) Backslash Characters:

Escape sequence is a character constant. It has a \ and a following meaningful character. It is used to print the non-printing characters such as double quote (“), single quote (’)….etc. It is used in printing (output) statements like printf().

Program to demonstrate Escape Sequences:

#include<stdio.h> int main() { printf(" hello\n"); printf(" welcome to \t C-Language"); printf("\n "hello" "); printf("\n 'hi' "); printf("\a\n abcd\befg"); }

Output: hello welcome to C-Language "hello" 'hi' abcefg

Escape Sequence Meaning

\n New Line \t Tab \b Back space \a Alert Beep \” “ \’ ‘ \ \

C-Language Basics V.Shiwa Chaithanya

Note: Integer Type: Number without decimal places is called “Integer Type”. Example: 65 879 -16 -

Floating Point [Real]: Number with decimal places is called “Floating Point Type (or) Real Type”. Example: 123.4567 56.78 -34.

Primitive Type Memory [Size] short int 2 Bytes int 4 Bytes long int 4 Bytes long long int 8 Bytes float 4 Bytes double 8 Bytes long double 16 Bytes char 1 Byte

Integer Type:

Number without decimal places is called “Integer Type”. Example: 65 879 -16 - There are Four Integer related Data Types in C-Language. They are:

  1. short int
  2. int
  3. long int
  4. long long int There are Two types in every Integer related Data Type. They are: i. signed ii. unsigned signed: It can accept positive, zero and negative values. unsigned: It can accept positive values and zero only. It cannot accept negative values.

C-Language Basics V.Shiwa Chaithanya

Floating Point Type: Number with decimal places is called “Floating Point Type (or) Real Type”. Example: 123.4567 56.78 -34.

There are Three Floating point related data types in C-Language. They are:

  1. float
  2. double
  3. long double

Float Type Memory Precision

[Decimal Places]

Format Specifier

float 4 Bytes 6 %f double 8 Bytes 15 %lf long double 16 Bytes 18 %Lf

Integer Type Memory Range Format

Specifier

short int 2 Bytes 32768 to 32767 (or) -2^15 to 2^15 -

%hd (or) %hi

unsigned short int 2 Bytes 0 to 65535 (or) 0 to 2^16 -

%hu

int 4 Bytes -2^31 to 2^31 -1 %d (or) %i

unsigned int 4 Bytes 0 to 2^32 -1 %u

long int 4 Bytes -2^31 to 2^31 -1 %ld (or) %li

unsigned long int 4 Bytes 0 to 2^32 -1 %lu

long long int 8 Bytes -2^63 to 2^63 -1 %lld (or) %lli

unsigned long long int 8 Bytes 0 to 2^64 -1 %llu

C-Language Basics V.Shiwa Chaithanya

Declaring Floating Point Type Variable: float avrg; // Allocates 4 Bytes memory for avrg

Declaring Character Type Variable: char section; //Allocates 1 Byte memory for section

Assigning Values:

Syntax:

Assigning Integer Value: rno=50; y=x;

Assigning Float Value: avrg=56.78;

Assigning Charcater: section=‘A’;

Reading Data from Keyboard: scanf(): “scanf()” is a predefined function included in “stdio.h” header file. It is used to read the data from keyboard.

Example-1: Reading an Integer value from keyboard: int rno; printf(“Enter rno:”); scanf(“%d”,&rno);

Example-2: Reading a float value from keyboard: float radius; printf(“Enter radius of circle:”); scanf(“%f”,&radius);

= / ;

C-Language Basics V.Shiwa Chaithanya

C-Language Tokens: A smallest individual unit of a C-program is called “C-Token”.

C-Language Tokens are: Identifiers Keywords Constants [Literals] Operators Separators

Identifiers: The names of variables, functions, labels or any user-defined name in the program is called “Identifier. It is used for identification purpose.

Rules for naming an Identifier: An Identifier is made up of with Letters [A to Z, a to z], Digits [ to 9], Underscore [ _ ] and Dollar [ $ ]. Other characters like space, @, # cannot be used in Identifier. Example: int m1; //valid

int total_mark$; //valid => _ and $ can be used

int sub1_mark$; //valid => 1, _ and $ can be used

int total marks; //Invalid => cannot contain space int m@rks; // Invalid => cannot contain @

It should not be started with digit. Example: int subject1_marks; //valid int 1subject_marks; //Invalid => cannot be started with digit 1.

It can be started with Letter or Underscore or Dollar. Example: int _x; //valid => can be started with _

int $x; //valid => can be started with $

C-Language Basics V.Shiwa Chaithanya

123.4567 -45.67 67.893 => Floating Point Constants

‘A’ ‘+’ ‘4’ ‘B’ ‘S’ => Character Constants

“raju” “++” “1992” “Hyd” => String Constants

Operators: Operator is a symbol which is used to perform operations like Arithmetic Operations or Logical operations. Example:

      • / % > >= < <=

Separators: Separators are: variable separator , statement separator ; block separator { } …etc

Operators V.Shiwa Chaithanya

OPERATORS

“Operator” is a symbol that is used to perform Arithmetic Operations or Logical Operations. Arithmetic Operations: a+b a-b a*b a/b Logical Operations: a>b age<18 age>=18 m<35 m>= The variables that are participating in the operations are called “Operands”. Combination of Operands, Operators and Numbers is called “Expression”.

Example: 2pir, pirr, bb-4ac, n%2, x+yz

In 2pir => 2 is Number. Pi and r are Operands. * is Operator.

C- Language provides following Operators:

Type Operators Purpose Arithmetic Operators

  • [Addition Operator]
  • [Subtraction]
  • [Multiplication] / [Division] % [Modulus]

Used to perform Arithmetic operations

Relational(or) Comparison Operators

[Greater Than Operator] = [Greater Than or Equal To]

< [Less Than] <= [Less Than or Equal To] == [Equal To] != [Not Equal To]

Used to compare two values

Logical Operators && [And] || [Or] ! [Not]

Used to perform logical operations

Operators V.Shiwa Chaithanya

Operator Precedence & Associativity:

Operator Precedence: Operator Precedence is used to evaluate the Expressions. It determines the order of operations to be performed in the Expression.

Example-1: x + y * z In the above Expression, ‘’ has highest priority than ‘+’. So yz is calculated first. Then ‘+’ operation will be done.

Example-2: (x+y)z In the above expression, (x+y) will be calculated first. Because, ‘(‘ has highest priority than ‘’.

Associativity: Associativity is used when multiple operators have same level precedence. Associative can be either Left to Right (or) Right to Left.

Operators V.Shiwa Chaithanya

Example-1: a+b*c-d/e

In the above Expression, * and / are same level priorities. So, Associativity will be used. For * and /, Associativity is Left to Right. So, ‘*’ will be calculated first. Then / will be calculated.

  • and – are same level priorities. Associativity of + and – is Left to Right. So,
  • will be calculated first. Then – will be calculated.

Example-2: a=b=c=d=

In the above Example, 4 Assignment operators are used. For Assignment operators, Associativity is Right t Left. First 50 will be assigned to d. d will be assigned to c. c will be assigned to b. Then b will be assigned to a.

Control Structures V.Shiwa Chaithanya

Conditional Control Structures: Conditional control structure executes the statements based on conditions.

C-Language provides following Conditional Control Structures:

  • if
  • if else
  • if else if
  • nested if

if:

  • The statements in if block get executed when the condition is true. It skips the statements when the condition is false.
  • It is suitable when we want to perform a task based on condition.
  • If one statement is in ‘if’ block, we have no need to specify curly braces ( { } ). We must specify curly braces if multiple statements are in ‘if’ block.

Execution Process: First, it checks the condition. If condition is true, executes the statements. Otherwise, skips the statements.

Example:

Syntax: if() { //statements }

if(age>=18) printf(“Eligible to Vote”);

Control Structures V.Shiwa Chaithanya

if else:

  • The statements in if block get executed when the condition is true.
  • The statements in else block get executed when the condition is false.
  • It is suitable when we want to perform any one of the two tasks.

Execution Process: First, it checks the condition. If condition is true, executes ‘if’ block statements. Otherwise, it executes else block statements.

Example:

Syntax: if() { //statements } else { //statements }

if(age>=18) printf("Eligible to Vote"); else printf("Not Eligible to Vote");