c-intro

Introduction to C language

Program:  Program, is a sequence of instructions, written to perform a specified task on a computer.

Programming:  programming is a process that leads from an original formulation of a computing problem to executable computer programs.

  • The act of writing a program is called Programming

Language: It is media through which two object can communicate.

Programming Language: A programming language is a formal constructed language designed to communicate instructions to a machine, particularly a computer.

Type of Languages:

  • High Level Language: High-level language is any programming language that enables development of a program in much simpler programming context and is generally independent of the computer’s hardware architecture.

    •   Highlevel language must be translated into machine language by a compiler or interpreter

    • Ex. C, C++, FORTRAN, BASIC (Beginner’s All-purpose Symbolic Instruction Code)

  • Low Level Language: Low-level programming language is a programming languages that provides little or no abstraction from a computer’s instruction set architecture.

    • Ex. Machine level language or assembly language

Note: Two language are different from each other due to the syntax difference

What is C?

C is a programming language developed at AT & T Bell Laboratory of USA in 1972. It was designed and written by Dennis Ritchie(Father of C).

  • C is language consist of characters, keywords, Instructions and Programs

Similarities of C with English Language:

In English Language:

English_flow

In C Language:

C-flow

History of C:

History-OF-C-Language-watch-in-marathi

Basic Structure of a C Program:

Capture

  • Documentation section : The documentation section consists of a set of comment lines giving the name of the program, the author and other details, which the programmer would like to use later.
  • Link section : The link section provides instructions to the compiler to link functions from the system library.
  • Definition section : The definition section defines all symbolic constants.
  • Global declaration section : There are some variables that are used in more than one function. Such variables are called global variables and are declared in the global declaration section that is outside of all the functions. This section also declares all the user-defined functions.
  • main () function section : Every C program must have one main function section. This section contains two parts; declaration part and executable part
    • Declaration part : The declaration part declares all the variables used in the executable part.
    • Executable part : There is at least one statement in the executable part. These two parts must appear between the opening and closing braces. The program execution begins at the opening brace and ends at the closing brace. The closing brace of the main function is the logical end of the program. All statements in the declaration and executable part end with a semicolon.

  • Subprogram section : The subprogram section contains all the user-defined functions that are called in the main () function. User-defined functions are generally placed immediately after the main () function, although they may appear in any order.

     Note:All section, except the main () function section may be absent when they are not required.

Sample C Program:

#include<stdio.h> <———————-Preprocessing Directive

void main()

{ <——————–Start of a Program

/*………….Printing Starts………….*/

Printf(“Learn at every moment”);

/*………….Printing starts……..*/

} <———————-End of a Program

  • In C many library functions are grouped category-wise and stored in different files known as header files. Ex. stdio.h–>standard input output header file

  • To use the functions defined in the header file that need to be included in the program

  • This can be achieved by the preprocessing directive “#include”

  • “#include” includes the content of header file(stdio.h) at the beginning of program.

  • Explanation of void main in detail is present later.

Compilation flow chart in gcc compiler:

gcc

Process of compiling and running Flow Chart:

compiling_and_running_flow_chart

Why C is called Structured Programming Language?

C is called a structured programming language because to solve a large problem, C programming language divides the problem into smaller modules called functions or procedures or methods each of which handles a particular responsibility. The program which solves the entire problem is a collection of such functions.

Applications of C

C was initially used for system development work, in particular the programs that make-up the operating system. Why use C? Mainly because it produces code that runs nearly as fast as code written in assembly language. Some examples of the use of C might be:

  1. Operating Systems
  2. Language Compilers
  3. Assemblers
  4. Text Editors
  5. Print Spoolers
  6. Network Drivers
  7. Modern Programs
  8. Data Bases
  9. Language Interpreters
  10. Utilities

In recent years C has been used as a general-purpose language because of its popularity with programmers.

Fundamentals of C Language:

  • Every language has some basic fundamentals or elements and contains some grammatical rules.

Character Set:

  • A defined list of characters recognized by the computer hardware and software. Each character is represented by a number. The ASCII character set, for example, uses the numbers 0 through 127 to represent all English characters as well as special control characters

  • The characters that can be used to form words, numbers and expressions those are to be used in C language.

  • The Character in C is grouped into following categories.

    1. Letters

    2. Digits

    3. Special Characters

    4. White Space Characters

c-character-set

C Token:

In C Program the smallest individual unit is known as C Token.

  • C tokens are the basic buildings blocks in C language which are constructed together to write a C program.

  • Each and every smallest individual units in a C program are known as C tokens.

tokens.bmp

C tokens are of six types. They are,

  1. Keywords               (eg: int, while),

  2. Identifiers               (eg: main, total),

  3. Constants              (eg: 10, 20),

  4. Strings                    (eg: “total”, “hello”),

  5. Special symbols  (eg: (), {}),

  6. Operators              (eg: +, /,-,*)

C tokens in a program:

 

Where

  • main – identifier

  • {,}, (,) – delimiter

  • int – keyword

  • x, y, total – identifier

  • main, {, }, (, ), int, x, y, total – tokens

 Keywords:

Keywords are the reserved words used in programming. Each keywords has fixed meaning and that cannot be changed by user. For example:

Here, int is a keyword that indicates, ‘value’ is variable of type integer.

  • As C is a case sensitive language, all the keywords are written in lower case letters.
  • There are total 32 keywords present in according to ANSI C.
Keywords in C Language by ANSI C
auto double int struct
break else long switch
case enum register typedef
char extern return union
continue for signed void
do if static while
default goto sizeof volatile
const float short unsigned

Identifiers:

Identifiers are names given to C entities, such as variables, functions, array etc. Identifier are created to give unique name to C entities to identify it during the execution of program.

For Example:

Here, value is a identifier which denotes a variable of type integer. Similarly, num is another identifier, which denotes another variable of type float.

Rules for naming an identifier

  1. Identifies must consist of only alphabets, digits or underscore symbol
  2. First character must be alphabet or underscore(_)
  3. Name of identifier cannot be any keyword of c program
  4. Name of identifier is case sensitive i.e. value and Value are two different variables
  5. Only first 32 characters are significant of an identifier name
  6. Special Characters are not allowed in identifier name

Examples of valid identifiers:

AC_80, _ABC, value, _50d, sum

Examples of invalid identifiers:

5cprog, c prog, break, for

Constants:

  • Constants in C refers to the fixed values that do not change during execution of program.
  • Example: 5, 2.8, “codequiz.in”

Types of C Constants

constants.bmpNumeric Constants:

  • These are consist of numeric digits and should have at least one digit
  • No comma or space is allowed here
  • These are either +ve or -ve values

Integer Constant:

Integer constants are the numeric constants without any fractional part or exponential part. There are three types of integer constants in C language: decimal constant(base 10), octal constant(base 8) and hexadecimal constant(base 16) .

Decimal digits: 0 1 2 3 4 5 6 7 8 9

Octal digits: 0 1 2 3 4 5 6 7

Hexadecimal digits: 0 1 2 3 4 5 6 7 8 9 A B C D E F.

For example:

Real Constants/Floating point Constant:

  • Real constants are the numeric constants that has either fractional form or exponent form.

For example:

Note:Here, E-3 represents 10-3. Thus, -0.22E-3 = -0.00022.

  • The general form of exponential notation is given as:

Mantisssa E Exponent

  • The mantissa is either a real number express in decimal notation or an integer
  • The exponent is an integer number with a plus or minus sign
  • No commas or blank space is allowed in the real constant

Character Constants

It is of two types 1.Single character constant 2. String constants

Single character constant:

  • It is a single alphabet, a single digit or a single special symbol enclosed within single quotes.
  • The maximum length of a character constant is 1 character

Ex. ‘5’, ‘y’, ‘:’ , ‘ ‘

Note: Character constant 5 is not same as number 5

String Constants:

  • String constants are  the sequence of characters enclosed within double quotes
  • The Characters may be letters numbers, special characters and blank spaces

Ex. “Hello”, “&+3″, “5+10″, “2015”

Backslash Character Constants(Escape sequences):

  • There are some characters which have special meaning in C language.
  • They should be preceded by backslash symbol to make use of special function of them.
  • Given below is the list of special characters and their purpose.
 
Backslash_character Meaning
\b Backspace
\f Form feed
\n New line
\r Carriage return
\t Horizontal tab
\” Double quote
\’ Single quote
\\ Backslash
\v Vertical tab
\a Alert or bell
\? Question mark
\N Octal constant (N is an octal constant)
\XN Hexadecimal constant (N – hex.dcml cnst)
 Output:

Variables:

  • Variables are named memory locations used to hold some data of particular type.
  • It works like a container to hold some data.
  • The value of a variable may change during the program execution at different time, but it can contain a single value at a time.
  • Naming a variable will follow the rules of identifies
  • Declaration of a variable must be done before they are used in the C Program

Variable-in-Java

Declaration of a Variable:

  • It gives the information about the variable to the compiler.
  • What type of data will be kept in variable is specified in the variable declaration
  • When we declare a variable some space is allocation in in the main memory(RAM) depending upon the type of datatypes

Syntax: datatype variable_name;

Example: int num;

Initialization of Variable:

variable_declaration

  • Assignment of some value to the variable is know as initialization
  • Before the assignment the the variable contains one default value

Example: num = 20;

 

Note: Declaration and initialization can be done at same time

Example: int num=20;

  • More than one variable can be declared and initialized in a single instruction ex. int a, b=4,c,d,e=7;

Valid Variable Name: X1, kh_raise, format

Invalid Variable Name: for, 123, (area), %, 25th

 Data Types:

  • Data types are the keywords, which are used for assigning a type to a variable.
  • It determines amount of space(memory) the variable will take and the bit pattern of storing the data in the RAM.
  • Size of variable, constant and array are determined by data types.

ANSI C supports three classes of data types:

  1. Primary or Fundamental data types
    1. Integer Type
    2. Float Type
    3. Char Type
    4. Void Type
  2. Derived Data Types
    1. Arrays
    2. Pointers
    3. Function
  3. User defined data type
    1. Structures
    2. Union
    3. Enumeration
    4. typedef

 Note: C-99 adds three more data types _Bool, _Complex, _Imaginary

Primary or Fundamental data types

Integer data types:

  • An integer type is a number without a fractional part
  • Keyword int is used for declaring the variable with integer type

Example: int num

  • Here num is a variable of type integer
  • C supports three different sizes of the integer data type: short int, int and long int.
  • sizeof(short int)<= sizeof(int)<= sizeof(long int)
  • The size of int is either 2 bytes(In older PC’s) or 4 bytes. If you consider an integer having size of 4 byte( equal to 32 bits), it can take 232 distinct states as: -231,-231+1, …,-2, -1, 0, 1, 2, …, 231-2, 231-1
Type Storage Size Value Range
int or signed int 2 or 4 bytes -32,768 to 32,767 or -2,147,483,648 to 2,147,483,647
unsigned int 2 or 4 bytes 0 to 65,535 or 0 to 4,294,967,295
short int or signed short int 2 bytes -32,768 to 32,767
unsigned short int 2 bytes 0 to 65,535
long int or signed long int 4 bytes -2,147,483,648 to 2,147,483,647
unsigned long int 4 bytes 0 to 4,294,967,295

 

Float data types:

  • Variables of floating types can hold real values(numbers) such as: 5.34f, -4.934 etc.
  • Keywords either float or double is used for declaring floating type variable.

Example: float num1;

double num3;

  • Here both num1 and num2 are floating point variables
  • When the accuracy provided by a float number is not sufficient, then double data type is used.
  • By default a floating point number is double not float

float-size

Type Storage size Value range Precision
float 4 byte 1.2E-38 to 3.4E+38 6 decimal places
double 8 byte 2.3E-308 to 1.7E+308 15 decimal places
long double 16 byte

Note: Precision describes the number of significant decimal places that a floating values carries.

Character data types:

  • Keyword char is used for declaring the variable of character type.

Example: char var=’C';

Here, var is a variable of type character which is storing a character ‘C’.

  • It requires 1 byte(8 bits) of memory to store a character
  • Total number of possible character 28=256 i.e 0-255

unsignedsigned

Type Storage size Value range
char 1 byte -128 to 127 or 0 to 255
unsigned char 1 byte 0 to 255
signed char 1 byte -128 to 127

Data type void:

  • The data type void actually refers to an object that does not have a value of any type.
  • Void is an empty data type associated with functions and pointers
  • It is also known as generic data type
  • Its size is unknown, thats why it is not used with variables
  • When void is used before a function it means that, the function will not return anything to the calling function.
  • void main() : It says that main function will not return anything to the operating system
  • If void is specified in the function argument, it explicitly says that the function will not take any arguments

Qualifiers:

  • The keywords which are used to modify the properties of a variable are called type qualifiers.
Group Qualifier Default qualifier Types of Qualifiers
1 auto, register, static, extern, auto Storage Class qualifier
2 signed, unsigned signed sign qualifier
3 short, long not short, not long* size qualifier
4 const not const* constant qualifier
5 volatile not volatile* volatile qualifie

 

Size qualifiers:

Size qualifiers alters the size of basic data type. The keywords long and short are two size qualifiers. For example:

Sign qualifiers:

Whether a variable can hold only positive value or both values is specified by sign qualifiers. Keywords signed and unsigned are used for sign qualifiers.

It is not necessary to define variable using keyword signed because, a variable is signed by default. Sign qualifiers can be applied to only int and char data types. For a int variable of size 4 bytes it can hold data from -231 to 231-1 but, if that variable is defined unsigned, it can hold data from 0 to 232 -1.

Constant qualifiers:

Constant qualifiers can be declared with keyword const. An object declared by const cannot be modified.

The value of p cannot be changed in the program.

Volatile qualifiers:

A variable should be declared volatile whenever its value can be changed by some external sources outside program. Keyword volatile is used to indicate volatile variable.

Different way in C to create constant:

1.Using keyword const

const int x=8;

2.Using macro expansion

          #define MAX 10

3.Using enum data type

          enum xxx{RED, GREEN, BLUE}

Basic Terminologies:

  • Data: It is the raw fact of an entity

  • Information: Processed data in known as information
  • Entity: Any object having some properties

data-information

  • File is the logical relation of records

  • Record is the logical relation of fields

  • Field is the logical relation of bits(0,1)

    file-bits

ASCII

  • It stand for American Standard code for information Interchange

  • Today ASCII is one of the most popular and widely supported character encoding Standard

  • American National Standard Institute (ANSI) published ASCII standard in 1963

  • Each computer character and special symbol is having one ASCII value

  • A-Z(65-90), a-z(91-122), 0-9(48-57)

Example:

GIRL-BOY

ascii