Advertisements

Archive

Archive for the ‘Introduction’ Category

C Programming Introduction


Importance of Subject 

 

  • ‘C is the base language of any​ other programming language.​
  •  To be a Good Programmer one ​must know fundamentals of C​ programming language.

History of ‘C’

 

  • ​Root of the morden language is ​ALGOL 1960. It’s first ​computer language to use block ​ structure.​ It gave concept of structured​ programming.​
  • In 1967, Martin Richards developed ​a language, BCPL (Basic Combined Programming Language)
  • In 1970,by Ken Thompson created a language called as ‘B’.​ It used to create early version of Unix.
  • In 1972,by Dennis Ritchie introduced new language called as ‘C’ .​

 

Features Of ‘C’

 

  • It is a robust language.​
  • Programs written in ‘C’ are efficient and fast.​(Because of variety of data types and powerful ​operators)​
  • Highly Portable. (related to OS)​
  • Well suited for structured programming.​
  • Ability to extend itself. ​

Program & Programming Language

Program:- A Set of instructions which ​carried out by processor for some ​Specific input, generates specific output. ​​

Programming language:- A specific ​manner of writing a program with some ​Predefined rules, symbols & their use as ​a part of language.​  i.e. Pascal, C, C++, VC++, JAVA, VB.

 

Basic structure of ‘C’

 

[1] Documentation Section 

  • It has set of comment lines(name of program, author details).​

What is Comment line??​

  • To guide a programmer. To write a note for ​function,operation,logic in between a program.​
  • Non-executable statement.​
  • Can’t be nested.

 e.g.    /* Hello /* abc */ Hi */      ​

[2] Link Section

  • It provides instructions to the compiler to link ​function from the system library.
  • # include  Directive
    • To access the functions which are stored in the​ library, it is necessary to tell the compiler , ​about the file to be accessed.​
    • Syntax:-​ #include<stdio.h> // stdio.h is header file

[3] Definition Section

  • It defines all symbolic constants.​
  •  #define instuction defines value to a symbolic constant.​
  •  #define:-​
    • It is a preprocessor compiler directive, not a statement.​
    • Therefore it should not end with a semicolon.​
    • Generally written in uppercase.​

[4] GLOBAL DECLARATION SECTION​

  • Some variables that are used in more than on function, such variables (global variables) declared in the global declaration section.​
  • It also declares all the user-defined function.

[5] Main() function section

  • Every ‘C’ program must have one main() function section.​
  • It contains two parts​
    • Declaration part:​ It declares all variables used in the executable part.​
    • Executable part:​ It has atleast  one statement.​

 

Link Section

Definition Section

Global Declaration Section

main() function section

{

Declaration part

Executable part

 }

 Subprogram section

Function1

Function2 …         

User defined function

 

HOW TO RUN A PROGRAM?

  • There are two ways to run programs written in a high-level language. ​
  • ​The most common is to compile the program​
  • The other method is to pass the program through an interpreter.

Compiler

Why compiler is require ?​

As machine (a processor) can operate​ On binary code instruction only…..​

If we use higher level language then …For execution of the program we must ​Convert it to lower level / machine level​ Code means, ​

A program that translates source code into object code.

The compiler derives its name from the way it works, looking at the entire piece of source code and collecting and reorganizing the instructions.​

Advantage of compiler​:

  • Programs produced by compilers run much faster than the same programs executed by an interpreter.

Interpreter:​

which analyzes and executes each line of source code without looking at the entire program. ​

Advantage of interpreter:​

  • It can execute a program immediately. ​
  • Compilers require some time before an executable program emerges. ​

C  Compiler  ​

  • Checks for syntax errors if any​ – on success  coverts ‘C source code​ into object code form –​ which is nearer to machine

Types of languages

 

(1) Lower level languages:-​

Languages which are very near to ​machine…. I.e. machine language, ​

Assembly language.​

(2) Higher level languages:-​

Languages which are very near to ​programmer rather than to machine….​

I.e. C++,Visual C++,Visual basic,Java.

C

Data Types in C

 

Each variable in C has an associated data type. Each data type requires different amounts of memory and has some specific operations which can be performed over it. Let us briefly describe them one by one:

Following are the examples of some very common data types used in C:

char: The most basic data type in C. It stores a single character and requires a single byte of memory in almost all compilers.
int: As the name suggests, an int variable is used to store an integer.
float: It is used to store decimal numbers (numbers with floating point value) with single precision.
double: It is used to store decimal numbers (numbers with floating point value) with double precision.

Different data types also have different ranges upto which they can store numbers.

These ranges may vary from compiler to compiler. Below is list of ranges along with the memory requirement and format specifiers on 32 bit gcc compiler.

 

datatype

We can use the sizeof() operator to check the size of a variable. See the following C program for the usage of the various data types:

#include <stdio.h>
int main()
{
    int a = 1;
    char b ='G';
    double c = 3.14;
    printf("Hello World!\n");
    //printing the variables defined above along with their sizes
    printf("Hello! I am a character. My value is %c and "
           "my size is %lu byte.\n", b,sizeof(char));
    //can use sizeof(b) above as well
    printf("Hello! I am an integer. My value is %d and "
           "my size is %lu  bytes.\n", a,sizeof(int));
    //can use sizeof(a) above as well
    printf("Hello! I am a double floating point variable."
           " My value is %lf and my size is %lu bytes.\n",c,sizeof(double));
    //can use sizeof(c) above as well
    printf("Bye! See you soon. :)\n");
    return 0;
}

Output

Hello World!
Hello! I am a character. My value is G and my size is 1 byte.
Hello! I am an integer. My value is 1 and my size is 4 bytes.
Hello! I am a double floating point variable. My value is 3.140000 and my size i
s 8 bytes.
Bye! See you soon. 🙂

 

Variable

 

  • C variable is a named location in a memory where a program can manipulate the data. This location is used to hold the value of the variable.
  • The value of the C variable may get change in the program.
  • C variable might be belonging to any of the data type like int, float, char etc.

 

Rules for Variable name

  • Variable name must begin with letter or underscore.
  • Variables are case sensitive
  • They can be constructed with digits, letters.
  • No special symbols are allowed other than underscore.
  • sum, height, _value are some examples for variable name

DECLARING & INITIALIZING C VARIABLE:

  • Variables should be declared in the C program before to use.
  • Memory space is not allocated for a variable while declaration. It happens only on variable definition.
  • Variable initialization means assigning a value to the variable.
 Type 
Syntax
Variable declaration
data_type variable_name;
Example: int x, y, z; char flat, ch;
Variable initialization
data_type variable_name = value;
Example: int x = 50, y = 30; char flag = ‘x’, ch=’l’;

THERE ARE THREE TYPES OF VARIABLES IN C PROGRAM THEY ARE,

  1. Local variable
  2. Global variable
  3. Environment variable

DIFFERENCE BETWEEN VARIABLE DECLARATION & DEFINITION IN C:

Variable declaration
Variable definition
Declaration tells the compiler about data type and size of the variable.
Definition allocates memory for the variable.
Variable can be declared many times in a program.
It can happen only one time for a variable in a program.
The assignment of properties and identification to a variable.
Assignments of storage space to a variable.

Input and Output

 

Input

  • scanf(“%d”,&a);
  • Gets an integer value from the user and stored it under the name a (variable)

Output

  • printf(“%d”,a);
  • Prints the value present in variable a on the screen

 

C Programming Operators

 

An operator is a symbol which operates on a value or a variable. For example: + is an operator to perform addition.

C programming has wide range of operators to perform various operations. For better understanding of operators, these operators can be classified as:

  • Arithmetic Operators
  • Increment and Decrement Operators
  • Assignment Operators
  • Relational Operators
  • Logical Operators
  • Conditional Operators
  • Bitwise Operators
  • Special Operators

 

C Arithmetic Operators

 

An arithmetic operator performs mathematical operations such as addition, subtraction and multiplication on numerical values (constants and variables).

Operator Meaning of Operator
+ addition or unary plus
subtraction or unary minus
* multiplication
/ division
% remainder after division( modulo division)

 

Example #1: Arithmetic Operators

// C Program to demonstrate the working of arithmetic operators
#include <stdio.h>
int main()
{
    int a = 9,b = 4, c;
    
    c = a+b;
    printf("a+b = %d \n",c);

    c = a-b;
    printf("a-b = %d \n",c);
    
    c = a*b;
    printf("a*b = %d \n",c);
    
    c=a/b;
    printf("a/b = %d \n",c);
    
    c=a%b;
    printf("Remainder when a divided by b = %d \n",c);
    
    return 0;
}

Read more…

Advertisements