0% found this document useful (0 votes)
7 views40 pages

4-c Fundamentals

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views40 pages

4-c Fundamentals

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 40

Fundamentals of C

78
First C program – print on screen
#include <stdio.h>
void main()
{
printf ("Hello, World! \n") ;
}

79
More printing … (code and see)
#include <stdio.h>
void main()
{
printf ("Hello, World! ") ;
printf ("Hello \n World! \n") ;
}

80
Some more printing
#include <stdio.h>
void main()
{
printf ("Hello, World! \n") ;
printf ("Hello \n World! \n") ;
printf ("Hell\no \t World! \n") ;
}

81
Reading values from keyboard
#include <stdio.h>
void main()
{
int num ;
scanf ("%d", &num) ;
printf (“No. of students is %d\n”, num) ;
}

82
Centigrade to Fahrenheit
#include <stdio.h>
void main()
{
float cent, fahr;
scanf(“%f”,&cent);
fahr = cent*(9.0/5.0) + 32;
printf( “%f C equals %f F\n”, cent, fahr);
}
83
Largest of two numbers
#include <stdio.h>
void main()
{
int x, y;
scanf(“%d%d”,&x,&y);
if (x>y) printf(“Largest is %d\n”,x);
else printf(“Largest is %d\n”,y);
}
largest-1.c 84
What does this do?
#include <stdio.h>
void main()
{
int x, y;
scanf(“%d%d”,&x,&y);
if (x>y) printf(“Largest is %d\n”,x);
printf(“Largest is %d\n”,y);
}
largest-2.c 85
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

A C program should not contain anything else


86
Structure of a C program
A collection of functions (we will see what they
are later)
Exactly one special function named main must
be present. Program always starts from there
Each function has statements (instructions) for
declaration, assignment, condition check,
looping etc.
Statements are executed one by one

87
Variables
Very important concept for programming
An entity that has a value and is known to the
program by a name
Can store any temporary result while executing a
program
Can have only one value assigned to it at any given
time during the execution of the program
The value of a variable can be changed during the
execution of the program

88
Contd.
Variables stored in memory
Remember that memory is a list of storage
locations, each having a unique address
A variable is like a bin
The contents of the bin is the value of the variable
The variable name is used to refer to the value of
the variable
A variable is mapped to a location of the memory,
called its address

89
Example
#include <stdio.h>
void main( )
{
int x;
int y;
x=1;
y=3;
printf("x = %d, y= %d\n", x, y);
}
90
Variables in Memory
Instruction executed Memory location allocated
to a variable X
X = 10
T
i X = 20 10
m
e
X = X +1

X = X*5

91
Variables in Memory
Instruction executed Memory location allocated
to a variable X
X = 10
T
i X = 20 20
m
e
X = X +1

X = X*5

92
Variables in Memory
Instruction executed Memory location allocated
to a variable X
X = 10
T
i X = 20 21
m
e
X = X +1

X = X*5

93
Variables in Memory
Instruction executed
Memory location allocated
to a variable X
X = 10
T
i X = 20 105
m
e
X = X +1

X = X*5

94
Variables (contd.)

X = 20
X
Y=15 20

X = Y+3 ? Y

Y=X/6

95
Variables (contd.)

X = 20
X
Y=15 20

X = Y+3 15 Y

Y=X/6

96
Variables (contd.)

X = 20
X
Y=15 18

X = Y+3 15 Y

Y=X/6

97
Variables (contd.)

X = 20
X
Y=15 18

X = Y+3 3 Y

Y=X/6

98
Data Types
Each variable has a type, indicates what
type of values the variable can hold
Four common data types in C
int - can store integers (usually 4 bytes)
float - can store single-precision floating
point numbers (usually 4 bytes)
double - can store double-precision floating
point numbers (usually 8 bytes)
char - can store a character (1 byte)
99
Contd.
Must declare a variable (specify its type and
name) before using it anywhere in your program
All variable declarations should be at the
beginning of the main() or other functions
A value can also be assigned to a variable at the
time the variable is declared.
int speed = 30;
char flag = ‘y’;

100
More Data Types in C
Some of the basic data types can be augmented
by using certain data type qualifiers:
short size qualifier
long
signed
unsigned sign qualifier

Typical examples:
short int (usually 2 bytes)
long int (usually 4 bytes)
unsigned int (usually 4 bytes, but no way to store + or
-)
101
Some typical sizes (some of these can vary
depending on type of machine)
Integer data Bit
Minimum value Maximum value
type size
char 8 -27=-128 27-1=127
short int 16 -215=-32768 215-1=32767
int 32 -231=-2147483648 231-1=2147483647
long int 32 -231=-2147483648 231-1=2147483647
-263=- 263-
long long int 64
9223372036854775808 1=9223372036854775807
unsigned char 8 0 28-1=255
unsigned short int 16 0 216-1=65535
unsigned int 32 0 232-1=4294967295
unsigned long int 32 0 232-1=4294967295
unsigned long lon 264-
g int
64 0
1=18446744073709551615
102
Variable Names
Sequence of letters and digits
First character must be a letter or ‘_’
No special characters other than ‘_’
No blank in between
Names are case-sensitive (max and Max are two
different names)
Examples of valid names:
i rank1 MAX max Min class_rank
Examples of invalid names:
a’s fact rec 2sqroot class,rank
103
More Valid and Invalid Identifiers

Valid identifiers Invalid identifiers


X 10abc
abc my-name
simple_interest “hello”
a123 simple interest
LIST (area)
stud_name %rate
Empl_1
Empl_2
avg_empl_salary
C Keywords
Used by the C language, cannot be used
as variable names
Examples:
int, float, char, double, main, if else, for, while.
do, struct, union, typedef, enum, void, return,
signed, unsigned, case, break, sizeof,….
There are others, see textbook…
Example 1
#include <stdio.h>
void main()
{ Three int type variables declared

int x, y, sum;
scanf(“%d%d”,&x,&y); Values assigned
sum = x + y;
printf( “%d plus %d is %d\n”, x, y, sum );
}
106
Example - 2
#include <stdio.h>
void main()
{ Assigns an initial value to d2,
can be changed later
float x, y;
int d1, d2 = 10;
scanf(“%f%f%d”,&x, &y, &d1);
printf( “%f plus %f is %f\n”, x, y, x+y);
printf( “%d minus %d is %d\n”, d1, d2, d1-d2);
}
107
Read-only variables
Variables whose values can be initialized during
declaration, but cannot be changed after that
Declared by putting the const keyword in front of
the declaration
Storage allocated just like any variable
Used for variables whose values need not be
changed

108
Correct
void main() {
const int LIMIT = 10;
int n;
scanf(“%d”, &n); Incorrect: Limit changed
if (n > LIMIT)
printf(“Out of limit”); void main() {
const int Limit = 10;
}
int n;
scanf(“%d”, &n);
Limit = Limit + n;
printf(“New limit is %d”, Limit);
}
109
Constants
Integer constants
Consists of a sequence of digits, with possibly a plus
or a minus sign before it
Embedded spaces, commas and non-digit characters
are not permitted between digits
Floating point constants
Two different notations:
Decimal notation: 25.0, 0.0034, .84, -2.234
Exponential (scientific) notation
3.45e23, 0.123e-12, 123e2
e means “10 to the power of”
110
Contd.
Character constants
Contains a single character enclosed within a pair of
single quote marks.
Examples :: ‘2’, ‘+’, ‘Z’
Some special backslash characters
‘\n’ new line
‘\t’ horizontal tab
‘\’’ single quote
‘\”’ double quote
‘\\’ backslash
‘\0’ null 111
Input: scanf function
Performs input from keyboard
It requires a format string and a list of variables into
which the value received from the keyboard will be
stored
format string = individual groups of characters
(usually ‘%’ sign, followed by a conversion
character), with one character group for each
variable in the list

int a, b; Variable list (note the &


before a variable name)
float c;
scanf(“%d %d %f”, &a, &b, &c);
Format string
112
Commonly used conversion characters
c for char type variable
d for int type variable
f for float type variable
lf for double type variable

Examples
scanf ("%d", &size) ;
scanf ("%c", &nextchar) ;
scanf ("%f", &length) ;
scanf (“%d%d”, &a, &b);
113
Reading a single character
A single character can be read using scanf with
%c
It can also be read using the getchar() function

char c;
c = getchar();

Program waits at the getchar() line until a


character is typed, and then reads it and stores it
in c
114
Output: printf function
Performs output to the standard output device
(typically defined to be the screen)
It requires a format string in which we can
specify:
The text to be printed out
Specifications on how to print the values
printf ("The number is %d\n", num);
The format specification %d causes the value
listed after the format string to be embedded in
the output as a decimal number in place of %d
Output will appear as: The number is 125 115
Contd.
General syntax:
printf (format string, arg1, arg2, …, argn);
format string refers to a string containing
formatting information and data types of the
arguments to be output
the arguments arg1, arg2, … represent list of
variables/expressions whose values are to be
printed
The conversion characters are the same
as in scanf 116
Examples:
printf (“Average of %d and %d is %f”, a, b, avg);
printf (“Hello \nGood \nMorning \n”);
printf (“%3d %3d %5d”, a, b, a*b+2);
printf (“%7.2f %5.1f”, x, y);
Many more options are available for both
printf and scanf
Read from the book
Practice them in the lab
117

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy