UNIT-II
INTRODUCTION TO C PROGRAM & OPERATORS
Topics: First C program - Hello world, How to open a command prompt on
Windows or Linux How to read and print on screen - printf(), scanf(), getchar(),
putchar() Variables and Data types - Variables, Identifiers, data types and sizes,
type conversions, difference between declaration and definition of a variable,
Constants.
Life cycle of a C program (Preprocessing, Compilation, Assembly, Linking, Loading,
Execution), Compiling from the command line, Macros.
Operators – equality and assignment, Compound assignment operators, Increment
and decrement operators, Performance comparison between pre and post
increment/decrement operators, bitwise operators (AND, OR, NOT and XOR),
Logical Operators, comma operator, precedence and associativity, Logical
operators (AND, OR,NOT).
,Why Program Computers?
Computer is a machine. Depends onInstructions to accomplish something.
Correct Instructions results expectedoutput.
Series of meaningful instructions is aprogram.
People who write these instructionsknown as programmers.
Cooking dish certain steps arefollowed. Particularorder, quantity etc.Same in
computer programming Wronginstruction, did not give thedesired result.
On RUNNING/EXECUTING a computerprogram we get desired output
You need a COMPILER to run a programon a computer. It makes a program
understandable to acomputer.Since Computer can understand only 0sand 1s
COMPILATION or BUILD is the processof making a program executable on
acomputer
Program written in high level languagecalled a SOURCE CODE.
Compiler transforms to MACHINE CODE
We shall use Code::Blocks/Online GDB/Dev C++ Compiler for typing, editing,
debuggingand executing a program.
Terminology
Integrated developmentenvironment (IDE) seethe program output in one place
with an IDE .Example:Code::blocks, Eclipse, Visual Studio
Editor
Compilation
Debugging: Removing them called debugging.
Errors in code called as Bugs.
Introduction
We all program our lives in some way.Computer Program is a list ofInstructions
Example Program: ISBT (Inter State BusTerminus) to Graphic Era Univesity,Dehradun
Head west on NH 7 toward ISBT Circle
At ISBT Circle, take the 1st exit ontoNH307 2 Kms
Turn left onto Post Office Rd 1 Kms
Turn right onto Bell Road 200 Metres
Make a left towards Graphic EraUniversity
Let us say your parents give you onethousand rupees per month towards
yourexpenses. Assume you hobby is listening tomusic and you purchase them from
awebsite selling songs. Let us say the cost ofa song is 12 rupees.Now you want to write
a simple program totell you the total cost for say N number ofsongs you download
from the site. If youhad to write the steps they would be…
, Enter “Number of songs you wouldlike to download today?” N
Enter “the cost per song:” 12
Multiply the Number of songs withcost per song: N x 12
Show the final cost: Rs. ______
This sequence of instructions is aprogram.
Now computer programs are similarbut have a SYNTAX and written insome
programming language.
Computer languages may specify thatend of each statement be followed bya
special character like say ; in C
Failure to use the terminating ; leadsto syntax error message
Every language has its unique syntax
Above statements in blue Consolasfont are known as pseudo code
Logic is known as SEMANTICS
// C Program
#include <stdio.h>
int main ()
{
int N_Songs ;
float Song_Cost = 12, Final_Price;
printf(“Enter Number of Songs \n”);
scanf(“%d”, &N_Songs);
Final_Price = N_Songs * Song_Cost;
printf(“Cost of Songs is %0.2f \n”,Final_Price);
return 0;
}
Key parts of a program
Variables and Constant
Name and types of values
Variables vary and contain value(s)
Names for Memory location
Operators = *
Data type Character and Non-Character
Must Declare memory locations to bereserved
, Why Learn C Programming Language?
Close to 50 years still widely used
Language that is the basis for mostmodern languages
Understand the working of acomputer system
Extensive applications. For example,compilers, operating systemsembedded
applications to name afew.
Major portions of OS like Windows,Linux, UNIX still in C. Integration withnew devices
through device drivers in C.
Embedded world mobile phones,washing machines, digital cars,cameras, IoT
applications use C.
Games also due to speed and quickresponse
Few languages have the ability tointeract with hardware like C
Lean, Mean and efficient
Lean, mean efficient language
Features of C language:
Structured
High Level/Middle Level
Robust
Portable
Extensible
Supports pointers
Structured:
C is structured procedure oriented language, it divides the problem into
smaller modules called functions or procedures.
High Level/Middle Level:
Programs written in C must be translated by the Compiler (system
software) into machine level language.
While in middle level languages programmers can write a code that can be
translated by the assemblers into machine code, which helps the programmers
to interact directly with the hardware.
INTRODUCTION TO C PROGRAM & OPERATORS
Topics: First C program - Hello world, How to open a command prompt on
Windows or Linux How to read and print on screen - printf(), scanf(), getchar(),
putchar() Variables and Data types - Variables, Identifiers, data types and sizes,
type conversions, difference between declaration and definition of a variable,
Constants.
Life cycle of a C program (Preprocessing, Compilation, Assembly, Linking, Loading,
Execution), Compiling from the command line, Macros.
Operators – equality and assignment, Compound assignment operators, Increment
and decrement operators, Performance comparison between pre and post
increment/decrement operators, bitwise operators (AND, OR, NOT and XOR),
Logical Operators, comma operator, precedence and associativity, Logical
operators (AND, OR,NOT).
,Why Program Computers?
Computer is a machine. Depends onInstructions to accomplish something.
Correct Instructions results expectedoutput.
Series of meaningful instructions is aprogram.
People who write these instructionsknown as programmers.
Cooking dish certain steps arefollowed. Particularorder, quantity etc.Same in
computer programming Wronginstruction, did not give thedesired result.
On RUNNING/EXECUTING a computerprogram we get desired output
You need a COMPILER to run a programon a computer. It makes a program
understandable to acomputer.Since Computer can understand only 0sand 1s
COMPILATION or BUILD is the processof making a program executable on
acomputer
Program written in high level languagecalled a SOURCE CODE.
Compiler transforms to MACHINE CODE
We shall use Code::Blocks/Online GDB/Dev C++ Compiler for typing, editing,
debuggingand executing a program.
Terminology
Integrated developmentenvironment (IDE) seethe program output in one place
with an IDE .Example:Code::blocks, Eclipse, Visual Studio
Editor
Compilation
Debugging: Removing them called debugging.
Errors in code called as Bugs.
Introduction
We all program our lives in some way.Computer Program is a list ofInstructions
Example Program: ISBT (Inter State BusTerminus) to Graphic Era Univesity,Dehradun
Head west on NH 7 toward ISBT Circle
At ISBT Circle, take the 1st exit ontoNH307 2 Kms
Turn left onto Post Office Rd 1 Kms
Turn right onto Bell Road 200 Metres
Make a left towards Graphic EraUniversity
Let us say your parents give you onethousand rupees per month towards
yourexpenses. Assume you hobby is listening tomusic and you purchase them from
awebsite selling songs. Let us say the cost ofa song is 12 rupees.Now you want to write
a simple program totell you the total cost for say N number ofsongs you download
from the site. If youhad to write the steps they would be…
, Enter “Number of songs you wouldlike to download today?” N
Enter “the cost per song:” 12
Multiply the Number of songs withcost per song: N x 12
Show the final cost: Rs. ______
This sequence of instructions is aprogram.
Now computer programs are similarbut have a SYNTAX and written insome
programming language.
Computer languages may specify thatend of each statement be followed bya
special character like say ; in C
Failure to use the terminating ; leadsto syntax error message
Every language has its unique syntax
Above statements in blue Consolasfont are known as pseudo code
Logic is known as SEMANTICS
// C Program
#include <stdio.h>
int main ()
{
int N_Songs ;
float Song_Cost = 12, Final_Price;
printf(“Enter Number of Songs \n”);
scanf(“%d”, &N_Songs);
Final_Price = N_Songs * Song_Cost;
printf(“Cost of Songs is %0.2f \n”,Final_Price);
return 0;
}
Key parts of a program
Variables and Constant
Name and types of values
Variables vary and contain value(s)
Names for Memory location
Operators = *
Data type Character and Non-Character
Must Declare memory locations to bereserved
, Why Learn C Programming Language?
Close to 50 years still widely used
Language that is the basis for mostmodern languages
Understand the working of acomputer system
Extensive applications. For example,compilers, operating systemsembedded
applications to name afew.
Major portions of OS like Windows,Linux, UNIX still in C. Integration withnew devices
through device drivers in C.
Embedded world mobile phones,washing machines, digital cars,cameras, IoT
applications use C.
Games also due to speed and quickresponse
Few languages have the ability tointeract with hardware like C
Lean, Mean and efficient
Lean, mean efficient language
Features of C language:
Structured
High Level/Middle Level
Robust
Portable
Extensible
Supports pointers
Structured:
C is structured procedure oriented language, it divides the problem into
smaller modules called functions or procedures.
High Level/Middle Level:
Programs written in C must be translated by the Compiler (system
software) into machine level language.
While in middle level languages programmers can write a code that can be
translated by the assemblers into machine code, which helps the programmers
to interact directly with the hardware.