C programing basics 1, Study notes of Computer science

A brief history of the C programming language and its structure. It explains the development of C, its relation to BCPL and B, and its popularity. It also discusses the structure of a C program, including the preprocessor command section, global data declaration section, and function definition section. The document covers topics such as compiling and executing C programs, constants, variables, and data types, including character types, integer types, floating-point types, and void types. It also explains how to define constants and use operators and punctuators in C programs.

Typology: Study notes

2022/2023

Available from 03/07/2023

agnidut-chatterjee
agnidut-chatterjee 🇮🇳

1 document

1 / 70

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
C-Programming
History of C
1. The C Programming Language was designed and developed by Dennis Ritchie at AT&T
Bell Laboratories in early 1970s.
2. The UNIX operating system which was also developed at Bell Laboratories had C as its
standard programming language. The source code for this operating system was coded
almost entirely using C.
3. C is based on the languages BCPL (Basic Combined Programming Language) and B.
4. Many of the features of BCPL and B are found in C.
5. But C differs from BCPL and B in one important aspect. Both BCPL and B are typeless
languages , the only data type it support is machine word which is used to store data in
a program .On the other hand C supports several built-in data types(e.g int, char, float,
etc).
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b
pf3c
pf3d
pf3e
pf3f
pf40
pf41
pf42
pf43
pf44
pf45
pf46

Partial preview of the text

Download C programing basics 1 and more Study notes Computer science in PDF only on Docsity!

C-Programming

History of C

  1. The C Programming Language was designed and developed by Dennis Ritchie at AT&T Bell Laboratories in early 1970s.
  2. The UNIX operating system which was also developed at Bell Laboratories had C as its standard programming language. The source code for this operating system was coded almost entirely using C.
  3. C is based on the languages BCPL (Basic Combined Programming Language) and B.
  4. Many of the features of BCPL and B are found in C.
  5. But C differs from BCPL and B in one important aspect. Both BCPL and B are typeless languages , the only data type it support is machine word which is used to store data in a program .On the other hand C supports several built-in data types(e.g int, char, float, etc).

C-Programming

History of C

  1. For many years C was mostly used within the Bell laboratories.
  2. It became more popular after the release of K & R version of C by Brain Kernighan and Dennis Ritchie in 1978.
  3. In December 1989, first ANSI standard definition of C language was published and came to be known as ANSI C.
  4. Due to tremendous popularity of C around the world, International Standard Organization (ISO) adopted this ANSI standard in 1990. This version was known as C89.
  5. In 1999, the new standard was released which was known as C99.

C-Programming

Structured Programming Structured programming is a powerful and easy approach for developing complex program. In this approach, a program is divided into a set of module, each modules may be sub-divided into a set of sub-modules and so on until they become indivisible unit. Each modules/sub-modules are called functions or subroutines and contain a set of instructions to perform a specific task.

‘Master Module’ controls the execution of the other modules directly or indirectly. This module acts as an entry point as well as exit-point of the program execution i.e the program starts its execution by invoking this master module, it may then invoke the other sub modules whenever necessary.

C-Programming

Structure of C Program A C program consists of a group of functions. A function is subprogram containing a set of statements that perform a specific task. Each functions in a C program implements a module or sub module of the program.

A C program consists of the following sections namely Preprocessor Command Section , Global Data Declaration Section and Function Defination Section.

Function definition section has one or more functions where each function is composed of Data Declaration Sections and Executable Section.

include<stdio.h>

// Global data may be declared here void main( ) { printf(“My First C Program”); }

Constants , Variables and Data Types

Like every programming language, C has been designed to process certain kinds of data consisting of numbers, characters and strings to produce valid output known as information.

These data are internally processed by a sequence of instructions known as program.

Every instructions in a program are formed according to some specific rules known as syntax rules(or grammar ).

Tokens : Every language consists of words and punctuation marks. These words and

punctuation marks are called. Similarly smallest individual units in a C program are known C token.

In C the tokens are classified into following groups: Keywords , Identifiers, Constants, Operators and Punctuators

Constants , Variables and Data Types

Keywords : Keywords are predefined words in C that have special meaning. These are used for special purpose and its meaning can not be changed. Keywords can not be used as identifier. The list of keywords used in C are Keywords auto double int struct break else long switch case enum register typedef char extern return union const float short unsigned continue for signed void default goto sizeof volatile do if static while

Identifiers Identifiers refers to the name given to various programming elements like variables, functions and arrays. They are user-defined names to specify these programming elements. While defining an identifier, the programmer must follow the following rule:

Constants , Variables and Data Types

There are few character constants which are non-printable constants such as backspace, tab, new line etc. These characters are called non-graphic characters.

To represent these character, we can use escape sequence. An escape sequence refers to a character preceded by a back slash ( \ ) character. For example, ‘\t’ represents a tab space character. The list of such character constants is shown

Escape Sequence Meaning \a Alert (bell) \b Backspace \f Form feed \n New line \r Carriage return \t Horizontal tab \v Vertical tab \ Backslash \0 Null \’ Single quote \” Double quote ? Question mark

Constants , Variables and Data Types

Operators Operators are symbols used to represent various computations such as addition, multiplication etc. The data item on which these operations are performed are called operands.

Punctuators Punctuators, also known as separators are special characters that delimit words or sentences. The various characters used in C as punctuators are shown in the following Table

Delimiters Use : Used for label ; Semi Terminates statement ( ) Parenthesis Used in expressions and Functions [ ] Square Bracket Used for array declaration { } Braces Start and end of compound statements # Hash Used for preprocessor directives , Comma Variable separator

Constants , Variables and Data Types

Some example of valid variable names are :

average , value , x1 , price_rate

Some example of invalid variables are

123 , &amount , area of circle ,

Data Types: A data type is a set of values and a set of operations on those values. Correct specification of data type of an item enable the C compiler to correctly perform operation on that item. The C compiler supports a rich set of data types. The various data type in C can be classified into three categories :

Primary (or Built-in) data types. User-defined data types. Derived data types.

Constants , Variables and Data Types

Constants , Variables and Data Types Floating Point Types A floating point data types is used to store numbers that has fractional part. In C, float data type is used to store real numbers such as 3.56 , -67.85 etc.

If the float data type does not provided sufficient accuracy, we can use another floating point data type known as double.

Data type Size in Bytes Range of values float 4 3.4E-38 to 3.4E+ double 8 1.7E-308 to 1.7E+ long double 10 3.4E-4932 to 3.4E+

Example : float f = 23.45;

Void Types: The void type represents no values. It can be used in following cases.

a] It is used to specify the return type of a function when the function does not return any value to the calling function. b] It is used to specify the type of parameter of a function when the function does not accept any parameter. c] It is also used to define as generic pointer.

Constants , Variables and Data Types

Defining Constant: The first way is to define a constant as a symbol which is to be textually replaced in the program by its value during compilation. Look at the following declarations:

#define PI 3. #define N 20 #define TAX_RATE 0. Here, PI is not a variable, but an alias for the value it represents. This is known as symbolic constant. A symbolic constant can be defined using the preprocessor directive #define.

Another way to define a constant is to use the keyword const. Look at the following declaration: const float PI = 3.14;

In this case, PI is defined as a variable, but const keyword tells the compiler that its value is fixed and must not be changed.

Operators:

Example : int a = 10, b= 3, result_int; float x=10.0, y = 3.0, result_float; Operations Operator Syntax Assignment Result Addition + a+b result_int = a+b; 13 Subtraction - a-b result_int=a-b; 7 Multiplication * ab result_int = ab; 30 Division / a/b result_int = a/b; 3 Modulus % a%b result_int = a%b; 1 Addition + x+y result_float = x+y; 13. Subtraction - x-y result_float=x-y; 7. Multiplication * xy result_float = xy; 30. Division / x/y result_float = x/y; 3.

Points to Remember: a] 10%3 = 1 (remainder). b] -10%3 = - c] 15/2 = 7 , not 7. d] 15/2.0 = 7.5 or 15.0/2 = 7.

Operators:

Relational Operators Relational operator, also known as comparison operator, is used to compare two values and depending on the value of comparison, we take some decision. For example, to compare the total marks obtained by two students, the relational operator is used as the following expression:

total_marks1 < total_marks2. The value of a relational expression is either true or false. These values re represented as 1 or 0 respectively. For example,

100<200 is true (or 1) but 100> 200 is false (or 0)

Operator Meaning Example < Is less than 10<20 gives 1 (true) <= Is less than or equal to 25<=12 gives 0 (false)

Is greater than 9>12 gives 0 (false) = Is greater then or equal to 100>=75 gives 1 (true) == Is equal to 10==10 gives 1 (true) != Is not equal to 10!=10 gives 0 (false)