Unit 1 Notes-2
Unit 1 Notes-2
Features of C Language
1. Simple
5. Rich Library
6. Memory Management
7. Fast Speed
8. Pointers
9. Recursion
10. Extensible
1) Simple
5) Rich Library
7) Speed
The compilation and execution time of C language is fast since there are
lesser inbuilt functions and hence the lesser overhead.
8) Pointer
9) Recursion
10) Extensible
********************************************************
How to install C
There are many compilers available for c and c++. You need to download
any one. Here, we are going to use Turbo C++. It will work for both C
and C++. To install the Turbo C software, you need to follow following
steps.
2. Create turboc directory inside c drive and extract the tc3.zip inside
c:\turboc
You can download turbo c++ from many sites. download Turbo c++
Now, you need to create a new directory turboc inside the c: drive. Now
extract the tc3.zip file in c:\truboc directory.
Select Start installation by the down arrow key then press enter.
Now C is installed, press enter to read documentation or close the
software.
4) Click on the tc application located inside c:\TC\BIN
Now double click on the tc icon located in c:\TC\BIN directory to write the
c program.
First C Program
Before starting the abcd of C language, you need to learn how to write,
compile and run the first c program.
To write the first c program, open the C console and write the following
code:
1. #include <stdio.h>
2. int main(){
3. printf("Hello C Language");
4. return 0;
5. }
return 0 The return 0 statement, returns execution status to the OS. The
0 value is used for successful execution and 1 for unsuccessful execution.
There are 2 ways to compile and run the c program, by menu and by
shortcut.
By menu
Then click on the run menu then run sub menu to run the c program.
By shortcut
*****************************************************
Compilation process in c
What is a compilation?
The preprocessor takes the source code as an input, and it removes all
the comments from the source code. The preprocessor takes the
preprocessor directive and interprets it. For example, if <stdio.h>, the
directive is available in the program, then the preprocessor interprets the
directive and replace this directive with the content of the 'stdio.h' file.
The following are the phases through which our program passes before
being transformed into an executable form:
o Preprocessor
o Compiler
o Assembler
o Linker
Preprocessor
The source code is the code which is written in a text editor and the
source code file is given an extension ".c". This source code is first passed
to the preprocessor, and then the preprocessor expands this code. After
expanding the code, the expanded code is passed to the compiler.
Compiler
Assembler
Linker
The main working of the linker is to combine the object code of library
files with the object code of our program
hello.c
1. #include <stdio.h>
2. int main()
3. {
4. printf("Hello javaTpoint");
5. return 0;
6. }
Character set:-
The character set is the fundamental raw material of any language
and they are used to represent information. Like natural languages,
computer language will also have well
defined character set, which is useful to build the programs.
1. Sourcecharacterset
a. Alphabets
b. Digits
c. SpecialCharacters
d. WhiteSpaces
2. Executioncharacterset
a. EscapeSequence
Sourcecharacterset
ALPHABETS
1)Uppercaseletters A-Z
2)Lowercaseletters a-z
DIGITS - 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
SPECIACHARACTERS
~ tilde
| vertical bar
@ at symbol
+ plus sign
< less than
_ underscore
- minus sign
> greater than
= equal to
& ampersand
/ slash
* asterisk
[ left bracket
; semicolon
] right bracket
, comma
WHITESPACECHARACTERS
\\ Back slash
ExecutionCharacterSet
Certain ASCII characters are unprintable, which means they are not
displayed on the screen or printer. Those characters perform other
functions aside from displaying text. Examples are backspacing, moving
to a newline, or ringing a bell.
Backslash character constants
Horizontal tab 009 \t
Moves next horizontal tab
New line 010 \n
Moves next Line
Vertical tab 011 \v
Moves next vertical tab
Form feed 012 \f
Moves initial position of next page
Carriage return 013 \r
Moves beginning of the line
Double quote 034 \"
Present Double quotes
Single quote 039 \'
Present Apostrophe
Question mark 063 \?
Present Question Mark
Back slash 092 \\
Present back slash
Octal number \000
Tokens in C
Classification of tokens in C
Keywords in C
o Identifiers in C
o Strings in C
o Operators in C
o Constant in C
o Special Characters in C
Keywords in C
do if static while
Identifiers in C
Strings in C
char a[10] = "javatpoint"; // The compiler allocates the 10 bytes to the 'a'
array.
char a[] = "javatpoint"; // The compiler allocates the memory at the run
time.
Constants in C
A constant is a value assigned to the variable which will remain the same
throughout the program, i.e., the constant value cannot be changed.
Types of constants in C
Constant Example
Special characters in C
Some special characters are used in C, and they have a special meaning
which cannot be used for another purpose.
o Curly braces { }: It is used in the opening and closing of the code.
It is used in the opening and closing of the loops.
o Comma (,): It is used for separating for more than one statement
and for example, separating function parameters in a function call,
separating the variable when printing the value of more than one
variable using a single printf statement.
Variables in C
1. type variable_list;
1. int a;
2. float b;
3. char c;
Here, a, b, c are variables. The int, float, char are the data types.
We can also provide values while declaring the variables as given below:
1. int a=10,b=20;//declaring 2 variable of integer type
2. float f=20.8;
3. char c='A';
Rules for defining variables
o A variable name can start with the alphabet, and underscore only. It
can't start with a digit.
1. int a;
2. int _ab;
3. int a30;
1. int 2;
2. int a b;
3. int long;
Types of Variables in C
1. local variable
2. global variable
3. static variable
4. automatic variable
5. external variable
Local Variable
1. void function1(){
2. int x=10;//local variable
3. }
1. int value=20;//global variable
2. void function1(){
3. int x=10;//local variable
4. }
Static Variable
A variable that is declared with the static keyword is called static variable.
1. void function1(){
2. int x=10;//local variable
3. static int y=10;//static variable
4. x=x+1;
5. y=y+1;
6. printf("%d,%d",x,y);
7. }
If you call this function many times, the local variable will print the
same value for each function call, e.g, 11,11,11 and so on. But
the static variable will print the incremented value in each function
call, e.g. 11, 12, 13 and so on.
Automatic Variable
All variables in C that are declared inside the block, are automatic
variables by default. We can explicitly declare an automatic variable
using auto keyword.
1. void main(){
2. int x=10;//local variable (also automatic)
3. auto int y=20;//automatic variable
4. }
External Variable
myfile.h
1. extern int x=10;//external variable (also global)
program1.c
1. #include "myfile.h"
2. #include <stdio.h>
3. void printValue(){
4. printf("Global variable: %d", global_variable);
5. }