C language
What is C:
- C is a procedural programming language.
- It was initially developed by Dennis Ritchie in 1972.
- It was mainly developed as a system programming language to write UNIX OS.
- It was developed to overcome the problems of previous languages such as B,BCPL etc.
Features of C:
- Simple: This language provides structured approach to break the problem into parts.
- Machine independent or portable: C program can be executed on different machines based on machine specific changes.
- Structured programming language: We can break the program into parts using functions. functions also provide code reusability.
- Memory Management: We can free the allocated memory at any time by calling the free() function.
- Speed: The compilation and execution time of c language is fast.
- Pointer: We can directly interact with the memory.
- Recursion: We can call the function with in another function.
Structure of C program:
Header
|
Main()
|
Variable declaration
|
Body
|
Return Statement
|
Flow of execution:
Pre processor:
It is a tool that instructs the compiler to do pre processing before actual compilation.
All pre processor commands start with # symbol.
Ex: #include,#define.
Compiler:
Compiler is a translator that translates the source code from high level language to lower level language(assembly language,object code or machine code) to create executable code.
Assembler:
Compiler of assembly language.
Linker:
Linker is a program that combines one or more object file generated by compiler into single executable file,library file or another object file.
Loader:
A program which loads the executable file to primary memory of machine.
Pro's and Con's of C:
Pro's:- Building block for other languages.
- Highly portable language.
- Structured programming language.
- Easy to learn.
- Several built in functions in C library.
- We can create user-defined functions to solve specific problem.
- We can access hidden or blocked objects from use by other programming languages.
- Speed up programs
- Compile language
- Easy to know how higher level languages works and interact with the machine.
Con's:
- C does't have the concept of oops.
- No run time checking.
- No strict type checking.
- Does't have the concept of namespace(Scope to the identifiers like functions,variables).
- Does't have the concept of constructor and destructor.
- Con't solve the real world programming tasks.
- Extending the programming issues(errors and bugs)
Difference B/W Error,Defect,Bug,Fault and Failure:
- A mistake in coding is called Error.
- Error found by tester is called Defect.
- Defect accepting by development team is called Bug.
- If an end user finds an issue then that particular issue is Failure.
- Fault is incorrect step or process or data definition.
Difference B/W Header file and Library:

Difference between Compiler and Interpreter

Difference between Compiler and Interpreter
How to install C
There are many compilers to available to write C. Some of them are
Code Editors:
1) Turbo C/C++
2) GCC compiler
3)Net beans
4) VI/VIM Editor
5) Code Lite
6) Sublime Text
7) Blue fish
8) Microsoft visual studio
9) Code Block
9) Code Block
Installing Turbo C++:
Step 1: Download Turbo C++
Step 2: Extract the downloaded zip file.
Step 3: Run the setup.exe file
Step 4: Complete the installation.
How to use Turbo C++:
1) First Start Turbo C++
2) select new file and write the c program
3) Press F2 for saving
4) Press Alt+F9 for compilation
5) Press Ctrl+F9 for run
Example:
Step 1: Write code in new file,save with .c extension.
Step 2: Compile the code. If there is an error rectify then run the code to get output.
Step 3: Press Alt F5 to see output.
Code:
Output:
Printf() and Scanf():
- The printf() statement is used to print output in console.
- Syntax: Printf("format string",argument list).
- The format string can be %d-integer, %c-character, %s string, %f float.
- The scanf() function is used to read the input from console.
- Syntax: Scanf("format string",argument list).
Example:
Code:
Output:
Install and How to use GCC Compiler:
Step 1: Download Codeblocks-mingw-setup.exe file.
Step 2: Complete the installation.
Step 3: Copy the path in C drive up to bin folder and set environment variable.
Step 4: Write the code in Notepad++ or Edit Plus and save the file.
Step 5: Open Command prompt, goto directory where the file is saved.
Step 6: To compile file type the command gcc -o executableFileName fileName.c
Step 7: To run the file type fileName.exe or fileName
Code:

Output:

Step 3: Copy the path in C drive up to bin folder and set environment variable.
Step 4: Write the code in Notepad++ or Edit Plus and save the file.
Step 5: Open Command prompt, goto directory where the file is saved.
Step 6: To compile file type the command gcc -o executableFileName fileName.c
Step 7: To run the file type fileName.exe or fileName
Code:
Output:
Real world applications of C:
- Operating Systems
- Development new languages
- Computational platforms(MATLAB,Mathematica)
- Embedded systems
- Graphics and Games
- Programming robots
Parts of C program:
- Pre-processor
- Header file
- Function
- Variables
- Statements & expressions
- Comments
Code:
Output:
C - Variable:
- A variable is a named location in a memory to hold a value.
- The value of C variable may get change in the program.
- The variable might be belonging to any data type like int,float,char etc.
- Memory space is not allocated at the time of variable declaration.
- Memory space is allocated at the time of variable definition.
- Variable initialization means assigning a value to the variable.
Declaring & Initializing a variable:

Types of Variable:
1) Local variable - The scope variable is with in the function only.
2) Global variable - The scope variable will be through out the program.
3) Environment variable - The scope variable will be available for all c applications and programs.The in-built functions which are used to access,modify and assign environment variables called environment functions{setenv(),getenv(),putenv()}.
Ex Code:

Output:

Types of Variable:
1) Local variable - The scope variable is with in the function only.
2) Global variable - The scope variable will be through out the program.
3) Environment variable - The scope variable will be available for all c applications and programs.The in-built functions which are used to access,modify and assign environment variables called environment functions{setenv(),getenv(),putenv()}.
Ex Code:
Output:
C - Data types:
- Data types are used to define a variable before use in a program
- Basic data types: int, char, float, double.
- Enumeration data types: enum.
- Derived data types: pointer,array,structure,union.
- void data type: void.
Integer data type:
- To store numeric values.
- The storage size of int data type is 2 or 4 or 8 bytes depending upon the processor in cpu.
- If we are using 16 bit processor 2 bytes(16 bits) {32 bit processor - 4 bytes, 64 bit processor - 8 bytes } of memory will be allocated to a variable.
- We can't store decimal value in int data type.
Character data type:
- The storage size of char is 1 byte.
- We can store only one character in a variable.
- To store more than one value we have to use array of characters concept.
Floating point data type:
- Float data type is used to store decimal values.
- Storage size of float is 4 bytes.
- Ex: 9.238764 (contains 6 digits after decimal point)
- Double data type is also same like float but it allows up to 10 digits after decimal point.
Enumerator data type :
- Enumeration data type consists of named integer constants list.
- Syntax: enum identifiers { enumerator-list }.
Ex Code:
Output:
Task: Declare string variable and assign your name and display your name
Code:
Output:
Task: Assign your name to 3 different variables Surname, First name, Last name and display your full name
Code:
Output:
Operators:
1) Arithmetic operators
2) Relational operators (>,<,<=,>=,==,!=)
3) Logical operators (&&,||,!)
4) Bitwise operators (&,|,`,^,>>,<<)
5) Assignment operators (=,+=,-=,*=,&,^)
6) Conditional operators (condition?true_value:false_value)
Ex Code:
Output:
Task: Display highest number in the above two numbers
Code:
Output:
Task on Logical operators:
Code:
Output:
Conditional Statements:
Decision control statements (if-else,nested if), group of statements are executed when condition is true
Syntax:
Case control statement (switch case) syntax:
Task: Ask the user to enter 3 numbers and display the largest number in that 3 numbers
Code:
Output:
Loop control statements
1) For
2) While
3) Do-while
4) Break - To exit from the loop
5) Continue - To continue the loop
Task: Ask the user to enter a number and display the multiplication table of that number
Code:
Output:
C - Function :
1) What is function?
C - function is a set of instructions to perform specific part in a program. Collection these functions creates a program.
2) Use of function:
- Re-usability
- Dividing big task into small pieces to achieve functionality.
- Improve understandability of very large programs.
- There is no limit to call a function.We can call any number of times.
3) Function declaration,Function call and Function definition:
Ex:
4) How to call function:
There are two ways to call a function in a program.Those are
i) Function by value - The value of variable is passed to the function as a parameter. Different memory will be allocated to both actual and formal parameters. Because, value of actual parameter is copied to formal parameter.
ii) Function by reference - The address of variable is passed to the function as a parameter.
Same memory will be allocated to both actual and formal parameters.
5) Types of function:
System defined - functions which are already defined in C library.
Ex: printf(), scanf(), strcat(), strcpy(), strlen().
User defined - functions which are defined by user.

Task: Write a function to add two numbers
Code:

Output:

Task: Write a function to display multiplication table
Code:

Output:

Input & Output functions:
The standard input - output header file (stdio.h) contains the definition of the functions which are used to display output on screen and to take input from user respectively.
Printf() - This function is used to display output. It will return number of characters printed by it.
Scanf() - This function is used to read an input from user. It will return number of characters read by it.
getchar() - This function reads a character and from the terminal and returns it as an integer.Reads single character at a time.
putchar() - This function displays a character passed to it on the screen and returns the same character. It displays only one character.
gets() - This function reads character entered from keyboard until new line is entered.
puts() - The string is displayed followed by new line character.
Note: Scanf() stops reading the characters when it encountered a space.Gets() read space as character too.
Ex:

Console I/O functions:
Keyboard and screen together called console.Console I/O functions classified into
1) Formatted Input/Output function - printf(),scanf(),sprintf(),sscanf().
sprintf() - writes to string.Instead of printing the output on the screen, it stores it in a character array.
sscanf() - reads from string.
2) Unformatted Input/Output function - getch(),getche(),getchar().
Ex:

Output:

File I/O functions:
Standard Files:
There are three file descriptor pointers predefined and opened.
stdin - standard input
stdout - standard output
stderr - standard error
fscanf() - It is like scanf() function, the given file descriptor pointer for the file to be read,the format string,address
Ex: fscanf(stdin,"%d",&m)
getc() - It is like getchar() to read character from opened file.
Ex: int getc(File *)
gets() - reads a string from standard output.
fgets() - It is to read a string from opened file.
fgetc() - reads a character from file.
fprintf() - It is for writing to a opened file in 'w' or 'a' mode.
Ex: fprintf(stdout,"%d",x)
putc() - for writing single character to standard output like putchar().
Ex: void putc(char ,File *); putc(ch,stdout);
puts() - It is like puts, puts(char *)
fputs() - It is to write a string to standard output. fputs(char *,file *).
fputc() - writes a character to file.
fread() - reads a block from a file.
fwrite() - writes a block to file.
fopen() - to open file stream.
fclose() - to close file stream.
fflush() - to clear the output stream buffer.
Mathematical functions:
The math.h header file supports all the mathematical related functions
floor() - returns the nearest integer which is less than or equal to the argument.
round() - returns rounded value.
ceil() - returns the nearest integer which is greater than or equal to the argument.
sqrt() - returns square root of argument value.
pow() - returns the power of number.
trunc() - This function truncates decimal value from floating number and returns an integer value.
Task: C - Program to print below pattern
Code:

Output:
Task: Write a program for finding "Digit Identification if string have any numbers".
Code:

Output:

Task: Write a program for finding "Permutation of a string".
Code:

Output:

Task: Write a program for finding "Replacing the first highest repeated character with user given character".
Code:

Output:



Task: Write a program for finding "Nth non-repeated character in given string".
Code:

Output:


Task: Write a program to print the following pattern
Task: Write a program to sum a digits in given string.
Code:

Output:

There are two ways to call a function in a program.Those are
i) Function by value - The value of variable is passed to the function as a parameter. Different memory will be allocated to both actual and formal parameters. Because, value of actual parameter is copied to formal parameter.
ii) Function by reference - The address of variable is passed to the function as a parameter.
Same memory will be allocated to both actual and formal parameters.
5) Types of function:
System defined - functions which are already defined in C library.
Ex: printf(), scanf(), strcat(), strcpy(), strlen().
User defined - functions which are defined by user.
Task: Write a function to add two numbers
Code:
Output:
Task: Write a function to display multiplication table
Code:
Output:
Input & Output functions:
The standard input - output header file (stdio.h) contains the definition of the functions which are used to display output on screen and to take input from user respectively.
Printf() - This function is used to display output. It will return number of characters printed by it.
Scanf() - This function is used to read an input from user. It will return number of characters read by it.
getchar() - This function reads a character and from the terminal and returns it as an integer.Reads single character at a time.
putchar() - This function displays a character passed to it on the screen and returns the same character. It displays only one character.
gets() - This function reads character entered from keyboard until new line is entered.
puts() - The string is displayed followed by new line character.
Note: Scanf() stops reading the characters when it encountered a space.Gets() read space as character too.
Ex:
Console I/O functions:
Keyboard and screen together called console.Console I/O functions classified into
1) Formatted Input/Output function - printf(),scanf(),sprintf(),sscanf().
sprintf() - writes to string.Instead of printing the output on the screen, it stores it in a character array.
sscanf() - reads from string.
2) Unformatted Input/Output function - getch(),getche(),getchar().
Ex:
Output:
File I/O functions:
Standard Files:
There are three file descriptor pointers predefined and opened.
stdin - standard input
stdout - standard output
stderr - standard error
fscanf() - It is like scanf() function, the given file descriptor pointer for the file to be read,the format string,address
Ex: fscanf(stdin,"%d",&m)
getc() - It is like getchar() to read character from opened file.
Ex: int getc(File *)
gets() - reads a string from standard output.
fgets() - It is to read a string from opened file.
fgetc() - reads a character from file.
fprintf() - It is for writing to a opened file in 'w' or 'a' mode.
Ex: fprintf(stdout,"%d",x)
putc() - for writing single character to standard output like putchar().
Ex: void putc(char ,File *); putc(ch,stdout);
puts() - It is like puts, puts(char *)
fputs() - It is to write a string to standard output. fputs(char *,file *).
fputc() - writes a character to file.
fread() - reads a block from a file.
fwrite() - writes a block to file.
fopen() - to open file stream.
fclose() - to close file stream.
fflush() - to clear the output stream buffer.
Mathematical functions:
The math.h header file supports all the mathematical related functions
floor() - returns the nearest integer which is less than or equal to the argument.
round() - returns rounded value.
ceil() - returns the nearest integer which is greater than or equal to the argument.
sqrt() - returns square root of argument value.
pow() - returns the power of number.
trunc() - This function truncates decimal value from floating number and returns an integer value.
Strings:
One dimensional array of characters terminated by a null character '\0'.
Syntax: char name[]="uma" (or) char name[4]={'u','m','a'};
Ex:
Output:
String Functions:
Code:
Output:
Pointers:
- A pointer is a variable that holds address of another variable.
- Direct address of the memory location.
- These are used to allocate memory dynamically at execution time.
- Always c pointer is initialized to null.The value of null is 0.
- & symbol is used to get the address of variable.
- * symbol is used to get the value of variable that is pointer is pointing to.
- The size of any pointer is 2 byte for 16 bit compiler.
Pointer declaration:
type *var_name
Ex: int *p;
Code:
Output:
Recursion:
Recursion is the process of repeating items in self similar manner (or) process of iterating same task.
Code to find Factorial of Number:
Output:
Code to find Fibonacci series:
Output:
Arrays
An array is a data structure which is used to store a fixed size sequential collection of elements of the same type.
Syntax:
type arr_name[arr_size];
Task 1: Write a array to display our team names.
Code:
Output:
Files:
- File is an object that stores data,information,settings or commands.
- Why we need files because to handle the large quantity of data without any complexities.
- Two types of files are existed.one is text file which contains information, another one is binary file which contains bytes or compiled version of text file.
- File operations are
- Creation of new file
- Opening or accessing an existing file
- Reading from a file
- Writing to a file
- Seeking in a file or moving to a specific location
- Closing a file
- The file pointer points to the structure that contains details of the file like name,size of the file,position,read/write status etc.
- Syntax: FILE *fp
- File modes for opening a file
- r - Reading
- rb - Reading in binary mode
- w - Writing
- wb - Writing in binary mode
- a - appending
- ab - appending in binary mode
- r+ - Reading and writing
- rb+ - Reading and writing in binary mode
- w+ - Reading and writing
- wb+ - Reading and writing in binary mode
- a+ - Reading and appending
- ab+ - Reading and appending in binary mode
Code for writing to a file:
Output:
Code for reading to file:
Output:
Task: C - Program to print below pattern
Code:

Output:



Task: C program to solve Towers of Hanoi problem.

Code:
Output:
Task: Write a program for finding "First non-repeated character in given string".
Input : stone profits
Output: n
Code:

Output:

Input : stone profits
Output: n
Code:
Output:
Task: Write a program for finding "Digit Identification if string have any numbers".
Code:
Output:
Task: Write a program for finding "Permutation of a string".
Code:
Output:
Code:
Output:
Task: Write a program for finding "Nth non-repeated character in given string".
Code:
Output:
Task: Write a program to print the following pattern
1
|
121
|
12321
|
1234321
|
12321
|
121
|
1
|
Code:
Output:
Task: Write a program to sum a digits in given string.
Code:
Output: