ICS Lecture 4-7
ICS Lecture 4-7
यिद आपको अंग्रेजी के कारण कोई समस्या आ रही है तो हमसे संपकर् करने में संकोच न करें
#include <stdio.h>
int main(void) {
int num ;
scanf ("%d", &num) ;
printf (“No. of students is %d\n”, num) ;
}
Common GCC file types
• .c -> C source code file.
• .h -> Header file contained in the program.
• .o -> Target file after compilation.
• .out -> Executable files, which do not have a fixed suffix. The
system distinguishes executable files from inexecutable files
based on file attributes. If the name of an executable file is
not given, GCC generates a file named a.out.
Compiling using GCC
• $ gcc test.c -o test
• ./test
Variables
• Must declare variables before use #include <stdio.h>
int main(void) {
• Variable declaration: int n; float phi;
int num ;
• int - integer data type
scanf ("%d", &num) ;
• float - floating-point data type printf (“No. of students is
• Many other types %d\n”, num) ;
}
• What are variables?
Variables
• Variables are containers for storing data values, like numbers
and characters in the memory. Can we change the content of a
container?
How big container do we need to pack the content? Container type / Datatype
Variables
• A variable is a named storage location in the computer’s
memory.
• If we want to hold some value in memory for later use, we need to
use as variable.
• Variables have 3 characteristics:
• Name – the name we use in our code to refer to the memory location.
• Type – characteristic that defines what legal values the variable can
hold.
• Value – what is actually held inside of the memory location.
Data Types
• Types of Data Types in C - The C programming language has
two basic data types:
• Primary
• Derived
Datatypes: some built-in/primitive datatypes
• The datatype of a variable determines how much space it occupies in
memory and how the bit pattern stored is interpreted.
Datatypes: some built-in/primitive datatypes
• The datatype of a variable determines how much space it occupies in
memory and how the bit pattern stored is interpreted.
What would happen in 32 bit environment
What would happen in 32 bit environment
• In the table, the integer is 16-bit or 2 bytes wide. Thus, the
compiler is also 16-bit or 2 bytes wide.
• If the compiler was 32-bit wide, the int type size would have
been about 32-bits or 4 bytes.
• However, this might not be the case every single time.
• It is also possible that the integer size is 32-bits or 4 bytes
for a 64-bits processor. It entirely depends on the type of
compiler.
What would happen in 32 bit environment
• Int: 2,147,483,647 (\pm)
Pop Quiz - (correct/incorrect)
• int money$owed
•
Pop Quiz - (correct/incorrect)
• int money$owed
• int total_count
•
Pop Quiz - (correct/incorrect)
• int money$owed
• int total_count
• int score2
•
Pop Quiz - (correct/incorrect)
• int money$owed
• int total_count
• int score2
• int 2ndscore
Pop Quiz - (correct/incorrect)
• int money$owed
• int total_count
• int score2
• int 2ndscore
• int long
Pop Quiz - (correct/incorrect)
Read
Sheets
Display No Is sheets a
positive
whole
Error number?
Message Yes
Set Stamps
= Sheets/5
Round Stamps to
nearest whole
number
Display
Stamps
End
Pseudocode
and
Flowchart
Pseudocode
and
Flowchart
Program Development Cycle
3. Write the Code – Implement a solution
• The instructions in a programming language collectively called code.
• Your code should be a translation of your algorithm developed into the
programming language.
• In this class we use C, but there are many other programming languages: C++, C#,
Ruby, Python, Visual Basic, etc.
• This is the major focus of this course, but note that you need to be able
to think algorithmically in order to do this.
• Meaning, you need to be able to logically solve the problem in order to write a
program for it.
Program Development Cycle
4. Testing and Debugging – Locate and remove any errors in
the program
if(expression)
{
//code to be executed
}
If-else: Control Statements (Cont.…)
Syntax
if(expression)
{
//Statements
}
else
{
//Statements
}
• IF without an else is possible.
• Else without an if is not possible.
if-else-if ladder: Control Statements (Cont.…)
if(condition1)
{
//statements
}
else if(condition2)
{
//statements
}
else if(condition3)
{
//statements
}
else
{ ***Dangling else problem
//statements
}
Determine dress
• if (temperature > 40)
• printf(“wear shorts”);
• else if (temperature >25)
• printf(“ideal weather - wear nice dress”);
• else if (if temperature > 10)
• printf(“chilly weather - wear warm cloths”);
• else
• printf(“stay inside”);
Nested if-else: Control Statements (Cont.…)
if( expression )
{
if( expression1 )
{
statement-block1;
}
else
{
statement-block 2;
}
}
else
{ Any levels of nested if-else is possible.
statement-block 3;
}