Introduction to C Programming: Concepts, Structure, and Examples, Lecture notes of Programming Methodologies

Basic concepts in C programming

Typology: Lecture notes

2018/2019

Uploaded on 02/04/2019

lee-karrine
lee-karrine 🇰🇪

1 document

1 / 6

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Introduction to C programming
A program
This is a set of instructions written down and fed to the computer to help in solving a particular
program.
Or
It’s a concise systematic step by step set of instructions that a computer obey in order to solve a
given problem.
Programming
This is the development of a solution to an identified problem and setting up of related series of
instructions which are then directed to a computer to produce desired results
Programming is the act of logical sequencing and assembling step by step set of instruction that
a computer obey to solve a problem
Programming is divided into two categories
1. Structured programming
A technique for organizing and coding computer programs in which a hierarchy of modules is
used, each having a single entry and a single exit point, and in which control is passed downward
through the structure without unconditional branches to higher levels of the structure.
Three types of control flow are used:
Sequence:-Its where statements are executed one afte the other
Selection/Decision:-One of the two blocks of program code is executed based on test
condition
Loop/repetition/Iteration:-It is where one or more statements are executed repeatedly as
long as specified condition is true
Examples of SP Languages
C, Pascal, COBOL, FORTRAN and Q-basic
2. Object oriented program
Uses visual methodologies in creating programs hence referred to as event driven
OOP employ the use of objects (object is a self obtain thing)
Object is divided into classes OOP employ use of inheritance and polymorphism.
OOP language include:C++,Java,Visual basic,Delphi,Java script,Visual J++,Perl,PHP and Visual
Fox pro
Difference
SP oop
Path is more or less predefined Program is driven by events &executed by the user
Programs are coded and driven Program is driven by event and executed by the user
It employ the use of three control
structures
Employ the use of objects
Why use C?
pf3
pf4
pf5

Partial preview of the text

Download Introduction to C Programming: Concepts, Structure, and Examples and more Lecture notes Programming Methodologies in PDF only on Docsity!

Introduction to C programming

A program

This is a set of instructions written down and fed to the computer to help in solving a particular program.

Or

It’s a concise systematic step by step set of instructions that a computer obey in order to solve a given problem.

Programming

This is the development of a solution to an identified problem and setting up of related series of instructions which are then directed to a computer to produce desired results

Programming is the act of logical sequencing and assembling step by step set of instruction that a computer obey to solve a problem

Programming is divided into two categories

  1. Structured programming

A technique for organizing and coding computer programs in which a hierarchy of modules is used, each having a single entry and a single exit point, and in which control is passed downward through the structure without unconditional branches to higher levels of the structure.

Three types of control flow are used:

  • Sequence:-Its where statements are executed one afte the other
  • Selection/Decision:-One of the two blocks of program code is executed based on test condition
  • Loop/repetition/Iteration:-It is where one or more statements are executed repeatedly as long as specified condition is true

Examples of SP Languages

C, Pascal, COBOL, FORTRAN and Q-basic

  1. Object oriented program

Uses visual methodologies in creating programs hence referred to as event driven

OOP employ the use of objects (object is a self obtain thing)

Object is divided into classes OOP employ use of inheritance and polymorphism.

OOP language include:C++,Java,Visual basic,Delphi,Java script,Visual J++,Perl,PHP and Visual Fox pro

Difference

SP oop Path is more or less predefined Program is driven by events &executed by the user Programs are coded and driven Program is driven by event and executed by the user It employ the use of three control structures

Employ the use of objects

Why use C?

C was initially used for system development work, particularly the programs that make-up the

operating system. C was adopted as a system development language because it produces code that runs nearly as fast as the code written in assembly language. Some examples of the use of C might be −

  • (^) Operating Systems
  • Language Compilers
  • Assemblers
  • Text Editors
  • Print Spoolers
  • Network Drivers
  • Modern Programs
  • Databases
  • Language Interpreters
  • Utilities

Importance of ‘C’ language

C language is a famous programming language due to its qualities. Some qualities are:

  1. It is robust language whose rich setup of built in functions and operator can be used to write any complex program.
  2. (^) Program written in C are efficien t due to several variety of data types and powerful operators.
  3. The C compiler combines the capabilities of an assembly language with the feature of high level language. Therefore it is well suited for writing both system software and business package.
  4. There are only 32 keywords; several standard functions are available which can be used for developing program.
  5. C is portable language ; this means that C programs written for one computer system can be run on another system, with little or no modification.
  6. C language is well suited for structured programming, this requires user to think of a problems in terms of function or modules or block. A collection of these modules make a program debugging and testing easier.
  7. C language has its ability to extend itself. A c program is basically a collection of functions that are supported by the C library. We can continuously add our own functions to the library with the availability of the large number of functions.In India and abroad mostly people use C programming language because it is easy to learn and understand.

Problem Solving includes

  1. Analysis and specification: Understand (define) the problem and what the solution must do.
  2. General Solution (Algorithm)-Specify the required data type and the logical sequences of steps that solves the problem.
  • Pseudocodes.

Basic structure of a structure program using sample program

Program sampe”Hello World”

#include <stdio.h>

int main() {

/* my first program in C */ printf("Hello, World! \n");

return 0; }

Hello World Example

A C program basically consists of the following parts −

  • Preprocessor Commands
  • Functions
  • Variables
  • Statements & Expressions
  • Comments

Let us take a look at the various parts of the above program −

  • The first line of the program #include <stdio.h> is a preprocessor command, which tells a C compiler to include stdio.h file before going to actual compilation.
  • The next line int main() is the main function where the program execution begins.
  • The next line /.../ will be ignored by the compiler and it has been put to add additional comments in the program. So such lines are called comments in the program.
  • The next line printf(...) is another function available in C which causes the message "Hello, World!" to be displayed on the screen.
  • The next line return 0; terminates the main () function and returns the value 0.

Tokens in C

A C program consists of various tokens and a token is a keyword, an identifier, a constant, a string literal, or a symbol. For example, the following C statement consists of five tokens –

printf("Hello, World! \n");

The individual tokens are –

printf ( "Hello, World! \n" ) ;

Semicolons

In a C program, the semicolon is a statement terminator. That is, each individual statement must

be ended with a semicolon. It indicates the end of one logical entity.

Given below are two different statements −

printf("Hello, World! \n"); return 0;

Comments

Comments are like helping text in your C program and they are ignored by the compiler. They start with /* and terminate with the characters */ as shown below −

/* my first program in C */

You cannot have comments within comments and they do not occur within a string or character literals.

Identifiers

A C identifier is a name used to identify a variable, function, or any other user-defined item. An

identifier starts with a letter A to Z, a to z, or an underscore '_' followed by zero or more letters, underscores, and digits (0 to 9).

C does not allow punctuation characters such as @, $, and % within identifiers. C is a case-

sensitive programming language. Thus, Manpower and manpower are two different identifiers in C. Here are some examples of acceptable identifiers −

mohd zara abc move_name a_ myname50 _temp j a23b9 retVal

Keywords

The following list shows the reserved words in C. These reserved words may not be used as constants or variables or any other identifier names.

auto else long switch break enum register typedef case extern return union char float short unsigned const for signed void continue goto sizeof volatile default if static while do int struct _Packed double

Whitespace in C

A line containing only whitespace, possibly with a comment, is known as a blank line, and a C compiler totally ignores it.

Whitespace is the term used in C to describe blanks, tabs, newline characters and comments. Whitespace separates one part of a statement from another and enables the compiler to identify