Skip to main content

C


C/C++ IDE

  1. Turbo C,C++ IDE for Windows XP
  2. GCC 4.5 for any Linux OS
  3. Dev C,C++ for any Windows OS
  4. C,C++ Emulator for Windows 7

What is a computer?

A computer is a machine that manipulates data according to a set of instructions

What is Language ?

Set of codes used to write a program (i.e)code- collection of program statement

There are 3 level in Language

  1. High level
  2. Low level
  3. Machine language

High level language

  1. Consists of statements
  2. Using English words & mathematical notations
  3. Used in any computer
  4. Easy to understand
  5. Eamples: BASIC,PASCAL,FORTRAN,ADA,ALGO,C++, C#,Java,HTML,etc
  6. C is Mid level language

Low level language

  1. Consists of mnemonics
  2. Mnemonics-shortform of word
  3. Vary from platform to platform
  4. ASSEMBLY LANGUAGE is the best example of the low level language

Machine language

  1. Consists of detailed cryptic instructions
  2. Using only 0’s and 1’s
  3. It can be understand by only computer
  4. We can’t understand

What is a Program ?

  1. Set of instructions
  2. Stored - Processor
  3. Executed sequencely - Computer
  4. Carryout - Specific task
  5. Represented - in Binary codes
  6. Composed – in Alphabets, Digits, Special symbols

Evolution of Languages

C FEATURES

  1. Structured Programming (top down processor)
  2. Flexible & Efficient
  3. It includes features of low level language
  4. Portable ( If same platform of computer)
  5. Faster (1:50 => C:BASIC)
  6. Ability to extend itself, Case sensitive
  7. No. of. Build-in functions
  8. Simple & Small
  9. Compiler / Interpreter
  10. Robust (Build-in functions & Operators)
  11. 32 keywords only & uses American Standard Code for Information Interchange( so available only in English )

Compiler and Interpreter

To change source to object file, compile using compiler or interpret using interpreter (c has both compiler as well as interpreter)
  1. Interpreter:convert program line by line to machine code
  2. Compiler: Convert Whole program to  machine code at one
  3. Linker: convert object code into executable machine language

Internal operations

American Standard Code for Information Interchange

Character set used in C

Short Cut Keys In Turbo C Editor

  1. F1= Help (to call help of the compiler)
  2. F2= Save (to save the program)
  3. F3= Open (to open already created program)
  4. F5= Zooming window
  5. F6= Next window
  6. ALT+X= Exit
  7. ALT+#= Go to n'th line
  8. ALT+F3= Close window
  9. ALT+F5= View last output
  10. ALT+F7= Interpret
  11. ALT+F9= Compile
  12. CTRL+F9= Execute
  13. SHIFT+DEL= Cut
  14. CTRL+INS= Copy
  15. SHIFT+INS= Paste
  16. CTRL+DEL= Clear/Delete
  17. ALT+BACKSPACE= Undo
  18. ALT+first leter of the menu= to open that menu
Eg. ALT+F= FILE MENU

How to write your Hello World Program in C

Programming Structure Of C


Syntax

// Documentation text
#include
return-type main(argument)
{
 //Body of the main function
}

Where

  1. return-types can be void, int, float, char, double, etc
  2. We generally use void as return-type when there is no returning values

FIRST PROGRAM

// This is our first example program
#include
void main()
{
 printf("Hello World");
}

Output:

Welcome to C

Where

  1. //-command line
  2. #-Preprocessor
  3. Include-Directives of C Compiler
  4. <>-Tag
  5. stdio-Standard input output
  6. conio-Console input output
  7. .h-Header files
  8. main-program where to start execution
  9. {}-block

Terminologies

Algorithm:
Steps writing sequencely in simplest way in which program solved
Flowchart
Graphical representation of algorithm where all activities are depicted using symbols as flows
Statement
Single line of the program(made of operator,operands,etc)
Syntax
Refers to the grammer of a programming language

Main Program

  1. It is special function
  2. CALLED by C system
  3. Show starting of the program
  4. So every program must have exactly one main function
  5. If we have more than one, compiler will confuse

COMMENTS

  1. Are non-executable statements
  2. Are used for better understanding

Two types of comments in C

  1. Single line using //
  2. //first program
    
  3. Multi line using /* */
  4. /*first
    program */
    

Header Files

  1. Special source files
  2. Contains library functions
  3. Library functions are pre-written
  4. Used to carry out commonly used routines
  5. To use the library function ,we should include the header file where the function
Syntax:
#include<headerfile.h>
Where
  1. Filename - header file name

Commonly used Header files

  1. stdio- Standard I/o library functions
  2. conio - DOS console I/o routines
  3. string- String manipulation functions
  4. math- Mathematical functions
  5. stdlib- utility function(conversion routines, search/sort routines, and other miscellany.)
  6. ctype-Character testing & conversion functions

C Tokens

The following are 6 types of tokens in C
  1. Keywords
  2. Constants
  3. Strings
  4. Operators
  5. Identifiers
  6. Special symbols

Keywords

  1. They have fixed predefined meaning
  2. We can't change
  3. 32 key words in ANSI C
  4. Keywords should be in lowercase
  5. Another name reserved words
List of Keywords

Identifiers

  1. Used defined name- variable, function, array.
  2. Consists - sequence of letters,digits,underscore
  3. First character - alphabet/ underscore
  4. Length- max. 31 characters
  5. Can't use Keywords & White spaces
  6. Case sensitive
  7. Valid Example: AO, disks, I, i
  8. Invalid Example: AO/, auto, 56

Constants

Fixed value, do not change even during execution
Types of Constants

Numeric Constant
  1. Must have atleast one digit
  2. May be +ve / -ve
  3. decimal point is allowed only for real constants
  4. Can't use White spaces & Commas
Character Constant:
  1. Must have atleast one character
  2. Can be single Alphabet/Digit/Special symbol
  3. Enclosed within ' ' for single character constants
  4. Enclosed within " " for string constants

Variables

  1. Simply Changeable values
  2. It can take different values at different times during execution

Operators

  1. Symbols that perform operation on data
  2. C supports rich set of built-in operators

Special Symbols

  1. They are such as [ ] { } < > ( ) / \ . , ? " ' ; : ! @ # $ % ^ & * _ - = + | ~ `

String

  1. Sequence of character
  2. Enclosed within double quotes (“”)
  3. Character - letters, numbers, special characters, blank spaces
  4. Case sensitive: 'A' ≠ 'a', 'A' ≠ "A"
Example: "haRIsh", "ha rish", "har@ish", "1har+ish5"

Escape Characters

  1. These are used in output functions
  2. They are not two characters, It is single
  3. They are known as escape sequence, backslash characters

List of Escape Characters

Data Types

Used to specify the type of the date so that we can easily classify our operations

Primitive Data Types: char, int, float, double

Derived Data Types: Array, Pointer, Function

User Defined Data Types: enum, struct, union

Where: signed, unsigned, short, long are Data type modifier

Declaration

All variables must be declared before it is used to reserve memory storage area for that variable. Syntax:
Data-type Variable-list;
Where:
  1. Data Type:int,char,float,double,etc
  2. Variable-List:list of variable separated by commas
Example:
int a;
int harish;
float b,chitra;

Initialization and Value Assignment

Initialization: First assignment of value to the variable is called initializing
  1. To store int numbers dont use decimal point
  2. int a;
    a=5;//Declare and assign by two steps (or)
    int a=5;//Declare and assign by single step
    
  3. To store float number can use decimal points(7 digits after precision)
  4. float a;
    a=5.6549870;//Declare and assign by two steps (or)
    float a=5.6549870;//Declare and assign by single step<
    
  5. To store double numbers can use decimal point (14 digits after precision)
  6. double rs;
    rs=5.01234567890123;// Declare and assign by two steps(or)
    double rs=5.01234567890123;//Declare and assign by single step
    
  7. To store single character
  8. char a;
    a=‘n’;//(or)
    char a=‘n’;
    
  9. To store sequence of character(String)
  10. char a[10];
    a=“harish”;//(or)
    char a[10]=“harish”;
    

Input & Output Functions

  1. To interface the user and computer I/O functions are used
  2. To display message, output, warning
  3. To get input from the user
  4. These are available in appropriate header files such as stdio.h, conio.h, etc

List of Type Specifiers


printf() from stdio.h

Syntax
printf(“control string”,arg-list);
Displaying text message
Example:
printf("What is this");
Output:
What is this
Displaying variable values using type specifier
Example:
int a=5;
printf("result: %d",a);
Output:
result: 5
Note: %- shows the content of the variable
Displaying escape characters
Example:
printf("well \ncome");
Output:
well
come

scanf() from stdio.h

Syntax:
scanf(“control string”,&arg-list);
Reading a value
Example:
int a;
scanf("%d",&a);
Reading no.of values
Example:
int a;
char b;
scanf("%d %c",&a,&b);
Note: &- shows address of the variable

Unformatted I/O

Reading a character

To get a single character from the user
stdio.h
  1. getchar()-display the character while typing the character
conio.h
  1. getch() - won’t display the character while typing the character
  2. getche() - display the character while typing the character and echo it again

Writing a character

To display a single character to user
  1. putchar(), putch() - both are used to display the character which is passed through it.
  2. fflush() - method is used to clear the unwanted dummy values in the variable
  3. clrscr() - method is used to clear the last out in the output screen
Note: we usually include our final statement as getch() method in our program,which just makes wait for our respond for exiting from the output.Otherwise we used to see the output by seeing the last output window (alt+f5)
Example 1
#include<stdio.h>
void main()
{
 float a;
 printf("Enter the value for A:");
 scanf("%f",&a);
 printf("Enterd value: %f",a);
}
Output:
Enter the value for A:4.52
Entered value:4.520000
Example 2
#include<conio.h>
void main()
{
 char a;
 Printf("Enter a character: ");
 a=getche();
}
Output:
Enter a character: j
j
Example 3
#include<stdio.h>
void main()
{
 char a;
 printf("Enter A value:");
 a=getchar();
 printf("Entered value: ");
 putchar(a);
}
Output:
Enter A value: j
Entered value: j
Example 4
#include<conio.h>
void main()
{
 char a;
 printf("Enter A value:");
 a=getch();
 printf("Entered value:");
 putch(a);
}
Output:
Enter the value:
Entered value:j

8 TYPES OF OPERATORS

  1. Arithmetic
  2. Relational
  3. Logical
  4. Assignment (short hand)
  5. Increment decrement
  6. Conditional
  7. Bitwise
  8. special

Arithmetic Operator

To perform arithmetic operation such as addition, subtraction, multiplication, division and modulo operation on numeric
 
//Example Program
#include<stdio.h>
void main()
{
 int a=10,b=5,c;
 c=a+b;
 printf("ADD: %d",c);
 c=a-b;
 printf("SUB: %d",c);
 c=a*b;
 printf("MUL: %d",c);
 c=a/b;
 printf("DIV: %d",c);
 c=a%b;
 printf("MOD: %d",c);
}
Output:
ADD:15 SUB:5 MUL:50 DIV:2 MOD:0

Relational Operator

To find relationship between two operands and its result will either -true(1) / false(0)
20>30  false
20<30  true
25>=30 false
25<=25 true
25==30  false
25!=30 true

Logical Operator

To find the relation ship between two or more relational expressions and its result will either true(1) / false(0)
((20>30)&&(20<30))  False
  false          true
((20>30)||(20<30))  True
  false          true
!(20>30)  True
  false

Shorthand Assignment Operator

To simplify certain statement (operator =) Syntax:
variable-name arithmentic = expression
Example:
int a=5,b=6;
a+=b;
printf("a: %d",a);
Output:
a=11;

Increment and Decrement Operator

Increment Operator(++)

Syntax:
variable++ (or) ++variable
Pre Increment Example:
int a=5,b; 
b=++a;
printf("a=%d, b=%d",a , b);
Output:
a=6, b=6
Post Increment Example:
int a=5,b;
b=a++;
printf("a=%d, b=%d",a , b);
Output:
a=5, b=6
Note: we may use only one operand int a=5; a++; printf("a=%d", a); Output:
a=6

Decrement Operator(--)

Syntax:
variable-- (or) --variable
Pre Decrement Example:
int a=5,b; 
b=--a;
printf("a=%d, b=%d",a , b);
Output:
a=4, b=4
Post Decrement Example:
int a=5,b;
b=a--;
printf("a=%d, b=%d",a , b);
Output:
a=4, b=5
Note: we may use only one operand
int a=5;
a--;
printf("a=%d", a);
Output:
a=4

Conditional Operator

Builds simple conditional expression Syntax:
Conditional-Expresion ? True-Statement: False-Statement
Example:
5>6 ? printf("true") : printf("false");
Output:
false

Special Operators

Comma (,)

  1. To link releated Expression
  2. Evaluated from left to right
Example:
int a=5,b=6;

Sizeof

  1. To get the size of the operand
  2. Used to dynamic memory allocation of like array and structures
Example:
int a=5,b;
b=sizeof(a);
printf("Size of int variable a:%d",b);
Output:
Size of int variable a:2

Bitwise Operators

  1. Bit by bit manipulation
  2. Can’t be applied on float or double
  3. Require two integer type operands
  4. Refer table of logical operator
Example AND
a=5,b=6,c
c=a&b;
//a=0000 0000 0011 0101
//b=0000 0000 0011 0110
//c=0000 0000 0011 0100
printf("c=%d",c);
Output:
c=4
Example OR
a=5,b=6,c;
c=a|b;
//a=0000 0000 0011 0101
//b=0000 0000 0011 0110
//c=0000 0000 0011 0111
printf("c=%d",c);
Output:
c=7
Example XOR
a=5,b=6,c;
c=a^b;
//a=0000 0000 0011 0101
//b=0000 0000 0011 0110
//c=0000 0000 0000 0011
printf("c=%d",c);
Output:
c=3
Example NOT
a=5;
c=^a;
//a=0000 0000 0011 0101
//c=1111 1111 1100 1010 overflow
printf("c=%d",c);
Output:
c=-6(over flow)
Example Right shift
a=5,b;
b=a>>2;
//a= 0000 0000 0011 0101
//b= 00 0000 0000 0011 01--
printf("b=%d",b);
Output:
b=1
Example Left shift
a=5,b;
b=a<<2;
//a=0000 0000 0011 0101
//b=---00 0000 0011 0101 00
printf("b=%d",b);
Output:
b=20

Operator Presedence


How to evaluate the equation

Example 1
A=9-12/3+3*2-1
Steps:
A=9-4+3*2-1
A=9-4+6-1
A=5+6-1
A=11-1
A=10
Example 2
A=9-12/(3+3)*(2-1)
Steps:
A=9-12/6*(2-1)
A=9-12/6*1
A=9-2*1
A=9-2
A=7

Expressions

  1. Arithmetic
  2. Relational
  3. Logical
Syntax:
Variable=Expression;

Arithmetic Expressions

It contains collection of constants, variables and arithmetic operator and results a numeric value
x=5*6-6+4/3;

Relational Expresssions

It contains collection of constants, variables and relational operator and results either true(1) or false(0) value
x=(5==6);

Logical Expression

It contains collection of relational expressions and logical operators and results either true(1) or false(0)
x=(a<5)&&(b==2)||(c!=1);

Type Casting

  1. Converting form one type to another type
  2. Small in Big, not Big in Small (memory space)

Types of Type Casting:

  1. Implicit Type Casting
  2. Example:
    int x=5;
    float y=6.2,z;
    z=x+y;//z as floating
    
  3. Explicit Type Casting
  4. Syntax:
    Type (var) (or) (Type) var
    
    Example:
    x=(int)y+.5;//x as floating
    

Comments

Popular posts from this blog

Install Hadoop 2.5.1 on Windows 7 - 64Bit Operating System

This post is about installing Single Node Cluster Hadoop 2.5.1 (latest stable version) on Windows 7 Operating Systems.Hadoop was primarily designed for Linux platform. Hadoop supports for windows from its version 2.2, but we need prepare our platform binaries. Hadoop official website recommend Windows developers to use this build for development environment and not on production, since it is not completely tested success on Windows platform. This post describes the procedure for generating the Hadoop build for Windows platform. Generating Hadoop Build For Windows Platform Step 1:Install Microsoft Windows SDK 7.1 In my case, I have used Windows 7 64 bit Operating System. Download Microsoft Windows SDK 7.1 from Microsoft Official website and install it. While installing Windows SDK,I have faced problem like C++ 2010 Redistribution is already installed. This problem will happen only if we have installed C++ 2010 Redistribution of higher version compared to the Windows SDK.  W

Install Spring Tool Suite on Ubuntu

This post is about installing Spring Tool Suite (STS) on Ubuntu. The Spring Tool Suite is an Eclipse-based development environment that is customized for developing Spring applications. Step 1: Download the latest Spring Tool Suite for Linux from STS official website: http://spring.io/tools/sts/all  Step 2: Extract into any folder which you prefer. My extracted Spring Tool Suite locations is /home/harishshan/springsource Step 3: Create the Menu icon for quick access sudo nano /usr/share/applications/STS.desktop Step 4: Enter the following content [Desktop Entry] Name=SpringSource Tool Suite Comment=SpringSource Tool Suite Exec=/home/harishshan/springsource/sts-3.4.0-RELEASE/STS Icon=/home/harishshan/springsource/sts-3.4.0-RELEASE/icon.xpm StartupNotify=true Terminal=false Type=Application Categories=Development;IDE;Java; Step 5: Now you can check from Quick Menu by typing " Spring "

How to fix Kindle wrong time left in chapter book

The most of kindle reader use the time left in chapter or time left in book options provided at left bottom of the page to estimate completing the book or the current chapter. But sometimes it was not  accurate and displaying 5 min left in chapter instead of 30 min left in chapter. To fix this issue just type ;ReadingTimeReset at search field and enter. Kindly refer the photo for the same.