Tuesday, January 29, 2019

Fundamental of C


Character set: Character set are the set of alphabets, numbers and some special characters that are valid in C language. Character set consists of following elements:
1) Alphabets:  A to Z and a to z
2) Digits: 0 1 2 3 4 5 6 7 8 9
3) Special Characters:

,
.
_
(
)
;
$
:
%
[
]
#
?
'
&
{
}
"
^
!
*
/
|
-
\
~
+

Comment: Comments are the statement that are used to declare how the following code works and make code easy to understand. Comment has no effect on programs runs.
Two types of comments:
a.   Line Comment:  A comment denoted by double forward slash (//). Eg.  //This is variable
b.      Block Comment: To comment on some blocks in a program, starting by forward slash asterisk (/*) write multiple lines of comment and ending asterisk forward slash (*/).
Example.  /*   It takes two values and compare between them
            And generate greatest number.   */
White space Characters: Blank space, new line, horizontal tab, carriage return and form feed
Keywords: Keywords are the reserved words used in programming. Each keyword has standard, fixed meaning and that cannot be changed by user.
For example: int money;
Here, int is a keyword that indicates, 'money' is of type integer. 
C programming is case sensitive; all keywords must be written in lowercase. Here is the list of all keywords
auto
double
int
struct
void
signed
continue
goto
break
else
long
switch
while
static 
do
if
case
enum
register 
typedef
volatile
sizeof
default
const
char
extern
return
union
unsigned
for
short
static 
Identifiers: Identifiers are names given to various program elements such as constants, variables, functions, structures, array etc. Each element in the program has its own unique name.
For example:
int money;
int mango_tree;
Here, money is a identifier which denotes a variable of type integer. Similarly, mango_tree is an identifier, which denotes variable of type integer.
Rules for writing identifier
  1. An Identifiers consists of letters (both uppercase and lowercase letters), digits and and underscore '_' only.
  2. The first letter of identifier should be either a letter or an underscore.
Example:  int a, _b; where a and _b are valid identifier.
Tokens: In C source code, the basic element recognized by the compiler is known as tokens. A token is source program text that the compiler does not break down into components elements. Keyword like int, float; constant like a, b; string literals like name, address; operators like &&, ||.
Data types: A program usually contains different types of data types (integer, float, character etc) and need to store the values being used in the program. There are two types of data type
  1. Primary Data Types: The basic fundamental of data having unit features is called primary data type.
Data Types
 Types
Memory Require
Format
void
Void
0 byte

char
Character
1 byte
%c
int
Integer
4/2  bytes
%d
float
Floating
4 bytes
%f
long
Floating number
4 bytes

double
Large floating point number
8 bytes
%lf
long double
Very large floating number
12 bytes
%lf
  1. Secondary Data Types: The data types which is constructed by merging some features of primary data types.
    1. arrays (a[ ])                               2. pointer (*p)               3. structures (struct)
    2. enumeration (enum)                  5. union (union)

Variables and Constants

Variables: Variables are memory location in computer's memory to store data. To indicate the memory location, each variable should be given a unique name called identifier. Variable names are just the symbolic representation of a memory location Examples of variable name: sum, car_no, count etc.

int num; 
Here, num is a variable of integer type.
Three types of variables:

Static variable declaration: Any variable which is declared by using keyword static is called static variable. The value of static variable is remain fixed for the other function but may changes within same function boundary.
Example:
#include <stdio.h>
#include <conio.h>
void func() {
static int x = 0;  /* x is initialized only once across three calls of func() and   the variable will get incremented                  three   times after these calls. The final value of x will be 3. */
printf("%d\n", x); // outputs the value of x
x++;
}

int main() {                   //int argc, char *argv[] inside the main is optional in the particular program
            func();              // prints 0
            func();              // prints 1
            func();              // prints 2
            func(); // prints 3
//          return 0;
            getch();
}

Global variable declaration: Any variable which is declared before main function is called global variable.
Example: int a=20;        //global variables
            main ( )
            {
            int a= 5;
{
}
                       
Local variable declaration: Any variable which is declared within the function is called local variable.
Example:
#include<stdio.h>
#include<conio.h>
 main( )
{
int a=9,b=4,c;  // local variable
c=a+b;
printf("c=%d\n",c);
getch( );
}

Rules for writing variable name in C

  1. Variable name can be composed of letters (both uppercase and lowercase letters), digits and underscore '_' only.
  2. The first letter of a variable should be either a letter or an underscore.
  3. There is no rule for the length of length of a variable.
In C programming, you have to declare variable before using it in the program.
Declaration of variables
The variable declaration syntax is
data_type    variable_name  assignment _operator value_to_variable
Example:       
data_type
variable_name
assignment _operator
value_to_variable
int
a


float
pi
=
3.14

Constants

Constants are the terms that can't be changed during the execution of a program.
There are 2 types of constants
1.      Numeric
a.      Integer Constant: The numeric constant that does not contain decimal point is called integer constant.
Example: 8, -12

b.      Floating-point constant: The numeric constant that contain fractional number (having decimal point). For example: -2.00, 0.0000222

2.      Character
a. Character constant: The constant that contains a single character is called character constant. It is enclosed inside the single quotation mark.  For example: 'a', 'l', 'm', 'F' etc.
      b.    String Constant: The constant that contain alpha-numeric value enclosed in a pair of double-quote marks. For example:
"good"                  //string constant
""                     //null string constant
"      "               //string constant of six white space
"x"                    //string constant having single character.
"Earth is round\n"         //prints string with newline
 
Types of Specifier:
1) Escape Sequence: These nonprintable characters are used to format text on the output screen. Escape sequence are always started with backslash (\) causes "escape" from the normal way the characters are interpreted by the compiler. For example: \n is used for newline.

Escape Sequences
Character
\b
Backspace 
\f
Form feed
\n
Newline
\r
Return / Enter
\t
Horizontal tab
\v
Vertical tab
\\
Backslash                                      (It print single \)
\'
Single quotation mark                   (It print ')
\a
 Bell                                 ( It produces audible sound i.e. beep)
\"
Double quotation mark.                 (It print  ")
\?
Question mark                                 (It print ?)
\0
Null character

2) Format Specifier: The output and input data are display and receive in specific pattern. Format specifier uses the token % and character(s) after it.


Format Specifier
Used by scanf () function
%d, % i
Single integer
%u
Unsigned integer
%f
Floating point number
%x
Hexadecimal number, lower for character
%X
Hexadecimal number, upper for character
%c
Character
%s
String
Example:
printf("Name is %s, Class is %d, Section is %c", "Puja", 12, 'A');
Output: Name is Puja, Class is 12, Section is A

Statement: A smallest executable entity within a program is called statement. Statements are the basic building block of C programming language. These statements enable the computer to carry out some calculation or perform logical comparison between values and variables.
A programming statement may consist of keyword, constant, variables, operators, control statements, data types, library function, and user-defined functions.
Any line written in C editor that normally terminates by a semicolon (;) is called statement
Example: int a=5, 6;
Types of statements
a.      Simple Statement: A simplest executable entity is called simple statement. It is a single line expression which is used to carry out assignment, calculation or test logical decision
b.      Compound statement: A single instruction composed of two or more individual instructions.
This type of statement is used to combine two or more statement in one single line of code.
#include <stdio.h>
#include<conio.h>
            main ( )
{
float pi, r, a; // simple statement
pi = 3.1415; // simple statement
r= 4.5;
a =pi* r*r; // compound statement, calculate and assign value to a
printf("%f is the area of circle having radius %f", a, r);
getch( );
}

c.      Control statement: A statement that affects the flow of execution of program is called control statement. Two types of control statements
1.      Selection (There are five selection control statement. They are if, if-else, nested if-else, if-else-if, and switch)
2.      Iteration (There are 3 types of iteration control statement. They are for, while and do -while)

No comments:

Post a Comment