ICS Lecture5 7
ICS Lecture5 7
#include <stdio.h>
{ Library function,
uses write system call
printf (“Hello world”);
Statement terminator
}
Formatted output
More printing … (code and see)
#include <stdio.h>
int main(void)
{
printf ("Hello, World! ") ;
printf ("Hello \n World! \n") ;
}
\n character
Some more printing
#include <stdio.h>
int main(void)
{
printf ("Hello, World! \n") ;
printf ("Hello \n World! \n") ;
printf ("Hell\no \t World! \n") ;
}
\t character
Int main (void) vs void main()
• int main() can be called with any number of arguments,
but int main(void) can only be called without any argument.
• Although it doesn’t make any difference most of the times,
using “int main(void)” is a recommended practice in C.
• Will discuss more when we will discuss functions
Reading values from keyboard
#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.
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?
#include <stdio.h>
int main() {
int age;
float height;
char name[50];
printf("Enter your age: ");
scanf("%d", &age);
printf("Enter your height: ");
scanf("%f", &height);
printf("Enter your name: ");
scanf("%s", name);
// note that this reads only a single word
printf("Your name is %s, you are %d years old, and your height is %.2f meters.\n", name, age, height);
return 0;
}
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.
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,648 to 2,147,483,647
The C Character Set
• The C language alphabet
• Uppercase letters ‘A’ to ‘Z’
• Lowercase letters ‘a’ to ‘z’
• Digits ‘0’ to ‘9’
• Certain special characters:
! # % ^ & * ( )
- _ + = ~ [ ] \
| ; : ‘ “ { } ,
. < > / ? blank
https://tinyurl.com/iitjics138
• 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)
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
{
//statements ***Dangling else problem
}
Dangling else problem
Initialize k=0 and o=0 Initialize k=4, o=5, ch=7
if(ch>=3) if(ch>=3)
if(ch<=10) {
k++; if(ch<=10)
else Print k}
o++; else
Print o
Dangling else problem
if( expression )
{
if( expression1 )
{
statement-block1;
}
else
{
statement-block 2;
}
}
else
{ Any levels of nested if-else is possible.
statement-block 3;
}
Operators in C