C Programming
C Programming
Charatcteristics of c
It is high level programming language.
Small size -it has 32 key words so it easy to learn compared to other languages.
Middle level programming bcos c compiler combines assembly languages as well as high level
languages.
Well suited for structured programming, in this approach c enables blocks, modules, functions.
Supports pointers to refer memory address of the variables, array, structures, and functions. C is a
portable language bcos program written for one computer can be run on other computer without any
little or no modification.
C is a extensible language it enables the user to add own functions to the C library.
Faster than java and python.
Compiler based language.
Uses of C
Implementing operating systems and system applications.
Cis used as an intermediate language for implementations of other languages.
Used to create a GUI end user applications.
Compilers for several other programming languages were designed using C.
Preprocessor
The perprocessor contains special instructions for how to prepare the program for compilation. It is also called as
macro processor. Bcos it allows to define macros. All preprocessor commands start with # symbol.
Eg: include
Macros
A macro is a segment of code which is replaced by the value of macro. It is defined by #define directive.
Types of macros
Two types,
1. Object like macros
2. Function like macros
Object like macros
The object like macro is an identifier that is replaced by value. It is used to represent numeric constants.
Eg: #define PI 3.14
Function like macros
The function like macros looks likes the function call.
Eg: #define MIN(a, b) ((a)<(b))?(a)(b))
Local decleration
The data declared within a function are called local decleration. This data visible only within that function. The life
time of data is till the program ends.
#include<stdio.h>
This statement tells the compiler to include standard input/output library or header files in the
program(stdio.h).
It used to use the built-in functions for reading the values from the keyboard and printing the result on the
screen.
Eg: printf(), scanf()
Int main()
Every C program contains main function which is the starting point of the program.
Int is a return type of the main function.
End of the function it will return integer type value.
Types of files in C
4 types,
1. Source file
2. Header file
3. Object file
4. Executable file
Source files
It contains the source code of the program. The file extension of any C source code file is .c.
Header files
We use the subroutines for large programs.
And same subroutine is used in anither programs.
In that time, we can include the header file of that subroutine instead of copy that full program.
Header file extension is .h.
Uses of header files
Reduce the possible of error- Copying the same subroutine form one file to another file may makes error and
the maintainability is very difficult.
If we want to change the subroutine program only change the source code of the subroutine and recompile
this source code and links them with program that uses them.
Reduce the time.
What happens if the header file is not included?
Missing the inclusion of appropriate header file in a C program will generate an error. Such a program may compile
but the linker will give an error message as it will not find the function to be used in the program.
Standard header files
Some functions are provided by C compiler that is included in standard header files.
Eg: string.h for string handling functions.
Stdlib.h – for some miscellaneous function.
Math.h – for mathematical functions.
Alloc.h – for dynamic memory allocation.
Conio.h – for clearing the screen.
Use of conio.h
It stands for console input and output header file. Eg: clrscr(),getch().
Object files
Object files are generated by the compiler as the result of processing the source code.
Linker uses the object files to produce the executable file by combining the object files together.
It has an extension .o and some OS windows and MSD-DOS it is .obj extension.
Executable files
The binary executable file is generated by the linker. The linker links the various object files together to
produce the executable file. It has an extension of .exe.
C tokens
Keywords
Variables
Strings
Operators
Special characters
Constants
Keywords
C has a set of reserved words often known as keywords that can not be used as an identifier.
All keywords are basically a sequence of words and have a fixed meaning. Keywords must be written in lowercase
letters.
List of keywords
1. auto
2. break
3. case
4. char
5. const
6. continue
7. default
8. do
9. double
10. enum
11. extern
12. float
13. for
14. goto
15. if
16. int
17. long
18. register
19. return
20. short
21. sizeof
22. signed
23. static
24. struct
25. switch
26. signed
27. typedef
28. union
29. unsigned
30. void
31. volatile
32. while
Use of enum keyword
Enumeration is a user define data type. It is used to assign the name to the integral constants which makes the
program easy to read and maintain. The keyword enum is used to declare the enumeration.
Eg: enum week{Mon=10, Tue, Wed, Thu=34, Fri, Sat=5, Sun}
Use of typeof
It is a keyword used to assign a alternative names to the existing data type. It is mostly used with user defined data
types, whwn names of the datatypes slightly complicated.
Syntax: typedef <existing_name> <alias_name>
Eg: typedef unsigned long ulong;
Use of volatile keyword
This is applied to a variable when it is declared. It tells the compiler that the value of variable may change any time
without any action being taken by the code the compiler finds nearby.
Why is volatile needed?
The volatile field is needed to make sure that the multiple threads always see the newest value, even the cache
system or compiler optimizations are at work. Reading from a volatile variable always returns the latest written value
from this variable.
Identifiers
Identifiers are the names given to the program elemnets like variables,arrays and functions. Identifiers may consist
of sequence of character, numbers and underscores.
Eg: marks,name,roll_no
List of data types
Data type Size in bytes Range
char 1 -128 to 127
Unsigned char 1 0 to 255
Signrd char 1 -128 to 127
int 2 -32768 to 32767
Unsigned int 2 0 to 65535
Signed int 2 -32768 to 32767
Signed short int 2 -32768 to 32767
Short int 4 -32768 to 32767
Unsigned short int 2 0 to 65535
Long int 4 -2147483648 to 2147483647
Unsigned long int 4 0 to 4294967295
Signed long int 4 -2147483648 to 2147483647
float 4 3.4E-38 to 3.4E+38
double 8 1.7E-308 to 1.7E+308
Long double 10 3.4E-4932 to 1.1E+4932
Global variables
When the variable declare outside all the function is called global variable.
Streams
A stream is a logical entity that represents a file or device that can accept input and outputs. All input and output
functions in C operates in data streams.
Types of streams
2 types,
1. Text streams
2. Binary streams
Text streams
In a text stream , sequence of character is devided into lines with each line being terminated with new line.
The program writes the new line to signal the end of a line.
Binary streams
Binary streams contains dta values using their memory representation. It consist of one or more bytes of arbitrary
information.
Modifiers
The amount of variable to be allocated for a variable is derived by modifiers. Modifiers are prefixed with data types to
modify amount of storage space to modify.
5 modifiers in C,
1. Short
2. Long
3. Signed
4. Unsigned
5. Long long
Conversion hierarchy of data types
1. long double
2. double
3. float
4. unsigned long int
5. long int
6. unsigned int
7. int
8. short,char
If else real world example
If your mark is greater than 40 you will pass the xam…like this.
Switch case real world example
“Restaurant menu” where one is supposed to choose from a list of available options.
It is useful in if you have lot of if else going on.
Quiz game
Calculator coding
Tv remote
switch is something like an array of bulbs and you want to light the bulbs in a pattern way so you will use switch in
that case!
When to use call by value and call by reference in functions?
In call by value the changes made in the called function will not be reflected in the calling function.
One advantage of the call by reference method is that it is using pointers, so there is no doubling of the
memory used by the varibles(as eith the copy of the call by value method)…So it is better to use a call by
value by default and only use call by reference if data changes are expected.
Difference between global and static variables
Global variables
Global avriables are variables which are defined outside the function. The scoper of global variables begin at
the point where they are defined and lasts till the end of the file/program. They have external linkage, which means
that in other source files, the same name refers to the same location in memory.
Static variables
Static global variables are private to the source file where they are defined and do not conflict withother
variables in other source files which would have the same name. Both global, as well as static variables, have static
initialization, which means that if you don’t assign them a value, they will get initialized to 0 or NULL(pointers).
Which is the best storage class in c?
For faster access of variable, it is better to go for register specifiers rather than auto specifiers. Because,
register variables are stored in register memory whereas auto variables are stored in main CPU memory. Only few
variables can be stored in register memory.
Real time applications of pointers
Null pointers
A null pointer is a pointer which points nothing.
Some uses of the null pointer are:
a) To initialize a pointer variable when that pointer variable isn’t assigned any valid memory
address yet.
b) To pass a null pointer to a function argument when we don’t want to pass any valid memory
address.
c) To check for null pointer before accessing any pointer variable. So that, we can perform error
handling in pointer related code e.g. dereference pointer variable only if it’s not NULL.
Void pinter
A void pointer is a pointer that has no associated data type with it. A void pointer can hold address of
any type and can be typcasted to any type.
Advantages
1) malloc() and calloc() return void * type and this allows these functions to be used to allocate
memory of any data type (just because of void *)
Eg: int a = 10;
char b = 'x';
void *p = &a; // void pointer holds address of int 'a'
p = &b; // void pointer holds address of char 'b'