




























































































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
good for all programming aspirents
Typology: Lecture notes
1 / 101
This page cannot be seen from the preview
Don't miss anything!
C-Language Basics V.Shiwa Chaithanya
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().
#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
\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:
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:
[Decimal Places]
float 4 Bytes 6 %f double 8 Bytes 15 %lf long double 16 Bytes 18 %Lf
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 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 _
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
“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
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.
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:
Execution Process: First, it checks the condition. If condition is true, executes the statements. Otherwise, skips the statements.
Example:
Syntax: if(
if(age>=18) printf(“Eligible to Vote”);
Control Structures V.Shiwa Chaithanya
if else:
Execution Process: First, it checks the condition. If condition is true, executes ‘if’ block statements. Otherwise, it executes else block statements.
Example:
Syntax: if(
if(age>=18) printf("Eligible to Vote"); else printf("Not Eligible to Vote");