C Basics
C Basics
C Programming Language
Language?
Source of Communication between two persons.
Example: Human –To- Human
OR
Source of Communication between User and
Computer is called programming language.
Example: Human –To- Machine
2
Program
3
What is programming?
Series of instructions to a computer to achieve a task
5
What is C?
C
A language written by Dennis
Ritchie in 1972 at AT&T Bell Labs
USA. This was to be the language
that UNIX was written in to become
the first "portable" language.
C is used:
System software, Compilers, Editors
Graphics and Geometry
Databases, operating systems, device drivers
Also used in application programs
7
Software Development Method
Requirement Specification
Problem Definition
Analysis
Refine, Generalize the problem definition
Design
Develop Algorithm: (A formula or set of steps for solving a
particular problem)
Implementation
Write Code
Verification and Testing
Test and Debug the code
8
How do you write a program?
Decide what steps are needed to complete the task
9
Getting started with C
Steps in learning English language
Steps in learning C
Constants
Alphabets Instruction Program
Variables
Digits
Keywords
Special-symbols
10
Sample Pseudo code
Task: add two numbers
Pseudo code (Algorithm) :
1. Start
2. Get two numbers
3. Add them (a + b)
4. Print the answer
5. End
11
What does a flowchart look like?
Pseudo code Start
(Algorithm) :
Get 2 numbers
1. Start
2. Get two numbers
A+B
3. Add them (A + B)
4. Print the answer
5. End
Print answer
End
12
Integrated Development Environments
An integrated development environment (IDE) is a software package
that makes it possible to edit, compile, link, execute, and debug a program
without leaving the environment.
13
13
Program Development in C
Header Files
The files that are specified in the include section is called
as header file
These are precompiled files that has some functions
defined in them
We can call those functions in our program by supplying
parameters
Header file is given an extension .h
C Source file is given an extension .c
15
Main Function
This is the entry point of a program
When a file is executed, the start point is the main function
From main function the flow goes as per the programmers choice.
There may or may not be other functions written by user in a program
Main function is compulsory for any c program
16
Simple C Program
/* A first C Program*/
#include <stdio.h>
void main(void)
{
printf("Hello World");
17
Simple C Program
/* A first C Program*/
Preprocessor directive
void main() Main Function
{ Opening bracket
} Closing Bracket
18
Simple C Program
Line 1: #include <stdio.h>
19
Simple C Program
Line 2: void main(void)
20
Simple C Program
Line 3: {
21
Simple C Program
Line 4: printf("Hello World ");
22
Simple C Program
Line 5: }
This closing bracket denotes the end of the program.
23
Running C Program
Type a program
Save it
Compile the program – This will generate an exe file
(executable)
Run the program (Actually the exe created out of
compilation will run and not the .c file)
In different compiler we have different option for compiling
and running. We give only the concepts.
24
Comment
Single line comment
// (double slash)
– Termination of comment is by pressing enter key
Multi line comment
/*….
…….*/
It is used to increase the readability of the program.
Any number of comments can be given at any place in the program.
Comment cannot be nested
example:
/* A first C Program*/
25
Data Types
Data types specify how we enter data into our programs and what type of data we enter.
C language has some predefined set of data types to handle various kinds of data that we
use in our program. These datatypes have different storage capacities.
Basic data types are listed below:
Type Size(bytes) Range Control String/
format specifier
char or signed char 1 -128 to 127 %c
unsigned char 1 0 to 255 %c
int or signed int 2 -32,768 to 32767 %i or %d
unsigned int 2 0 to 65535 %u
short int or signed short int 1 -128 to 127 %d
long int or signed long int 4 -2,147,483,648 to 2,147,483,647 %ld
unsigned long int 4 0 to 4,294,967,295 %lu
float 4 3.4E-38 to 3.4E+38 %f or %g
double 8 1.7E-308 to 1.7E+308 %lf
long double 10 3.4E-4932 to 1.1E+4932 %lf
void
void type means no value. This is usually used to specify the type of functions. We will learn it later
Note: C program takes 64 Kb memory in RAM on 16 - bit Compiler
26
The C character Set
Digits 0,1,2,3,4,5,6,7,8,9
27
Constants, Variable and keywords
The alphabets, numbers and special symbol when properly
28
Constants
Any information is Constant
Data = Constant = Information
Primary
Integer (2, -34 , 0)
Real (3.4 , 0.06 )
Character ( Single quote, length one)
Secondary (Secondary constant made with the help of
primary constant)
Array
String ( e.g “Rahul” )
Pointer
Union
Structure
Enumerator
Class
Variable
A variable is a name that may be used to store a data value. Unlike constant, variables
are changeable, we can change value of a variable during execution of a program. A
programmer can choose a meaningful variable name. Example : average, height, age,
total etc.
The int, char, float and double are the keywords to represent data types. Commas separate the
variables, in case variables are more than one.
Keywords
Keywords are the words whose meaning has already been explained
to the C compiler
Sometimes called reserved words.
They cannot be used as variable names.
There are only 32 keywords available in c
\n new line
\t tab
\a alert
\\ backslash
\” double quote
\0 Null
\b Backspace
34
Escape Sequence
Escape Description
Sequence
\t Inserts a tab in the text at this point.
\b Inserts a backspace in the text at this point.
\n Inserts a newline in the text at this point.
\r Inserts a carriage return in the text at this point.
use \r to move to the start of the line and overwrite the existing text.
\f Inserts a form feed in the text at this point.
\f is used for page break. You cannot see any effect in the console. But
when you use this character constant in your file then you can see the
difference. (in cmd abc > xyz.doc)
\' Inserts a single quote character in the text at this point.
\" Inserts a double quote character in the text at this point.
\\ Inserts a backslash character in the text at this point.
Delimiters
Language pattern of C uses special kind of symbols, which
are called as delimiters. They are given as under.
Delimiters Symbols Use
Colon : Useful for label
Semicolon ; Terminates statements
Parenthesis () Used in Expression and function
Square brackets [] Used for array declaration
Curly braces {} Scope of statement
Hash # Preprocessor directive
Comma , Variable seperator
Null Character \0 In end of string
Operator and Expressions
Arithmetic operators + , - , * , /, %
Relational operators > , < , = = , >= , <= , !=
Logical operators && , ||
Increment and Decrement operator ++ , --
Assignment Operator =, +=, -=, *=, /=, %=
Bitwise operators & , | , ^ , >> , << and ~
Comma operator ,
Condition operator ?:
Sizeof operator
Operators in C
Unary operators
Increment and decrement operator
Assignment operator
Sizeof operator
Binary operators
Arithmetic operators
Relational operators
Logical operators
Ternary operators
Condition operator
Operator precedence
Precedence Operator Description Associativity
++ -- Suffix/postfix increment and decrement Left-to-right
() Function call
[] Array subscripting
1
. Structure and union member access
Structure and union member access through
->
pointer
++ -- Prefix increment and decrement Right-to-left
+- Unary plus and minus
!~ Logical NOT and bitwise NOT
2 (type) Cast
* Indirection (dereference)
& Address-of
sizeof Size-of
Operator precedence
Precedence Operator Description Associativity
3 */% Multiplication, division, and remainder Left-to-right
4 +- Addition and subtraction
5 << >> Bitwise left shift and right shift
< <= For relational operators < and ≤ respectively
6
> >= For relational operators > and ≥ respectively
7 == != For relational = and ≠ respectively
8 & Bitwise AND
9 ^ Bitwise XOR (exclusive or)
10 | Bitwise OR (inclusive or)
11 && Logical AND
12 || Logical OR
13 ?: Ternary conditional Right-to-left
= Simple assignment
+= -= Assignment by sum and difference
14 *= /= %= Assignment by product, quotient, and remainder
<<= >>= Assignment by bitwise left shift and right shift
&= ^= |= Assignment by bitwise AND, XOR, and OR
15 , Comma Left-to-right