C Basic
C Basic
❑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.
❑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
❑ 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.
Output:
The value of PI is: 3.140000
➢If you try to change the value of PI, it will render compile time error.
Output:
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.
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
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!!!