SlideShare a Scribd company logo
1 of 33
C Programming Language
Mo’meN Ali
Data and C
Bit: the smallest unit of a hardware memory “Binary digit”.
Byte: the computer memory unit, it’s 8-bit for nearly all computers.
Word: the natural computer memory unit for a given processor architecture,
for example 8-bit microcomputers has word size of 8-bit for early IBM
compatibles using 80286 processors are 16-bit which means word size are 16-
bit long, and so on….
/* print1.c-displays some properties of printf() */
#include <stdio.h>
int main(void)
{
int ten = 10;
int two = 2;
printf("Doing it right: ");
printf("%d minus %d is %dn", ten, 2, ten - two );
printf("Doing it wrong: ");
printf("%d minus %d is %dn", ten ); // forgot 2 arguments
return 0;
}
Output
Doing it right: 10 minus 2 is 8
Doing it wrong: 10 minus 10 is 2
Integer
An integer is a number with no fractional part. In C, an integer is never
written with a decimal point. Examples are 2, –23, and 2456. Numbers
such as 3.14, 0.22, and 2.000 are not integers. Integers are stored as
binary numbers. The integer 7, for example, is written 111 in binary.
Therefore, to store this number in an 8-bit byte, just set the first 5 bits
to 0 and the last 3 bits to 1
Float
A floating-point number more or less corresponds to what
mathematicians call a real number. Real numbers include the numbers
between the integers. Some floating-point numbers are 2.75, 3.16E7,
7.00, and 2e–8. Notice that adding a decimal point makes a value a
floating-point value. So 7 is an integer type but 7.00 is a floating-point
type. Obviously, there is more than one way to write a floating-point
number. We will discuss the e-notation more fully later, but, in brief, the
notation 3.16E7 means to multiply 3.16 by 10 to the 7th power; that is,
by 1 followed by 7 zeros. The 7 would be termed the exponent of 10.
/* Weight.c -- your weight in gm */
#include <stdio.h>
int main(void)
{
float weight; /* user weight */
float value; /* rhodium equivalent */
printf(“Are you worth your weight in rhodium.n");
printf(“Let’s check it out.n”);
printf("Please enter your weight in pounds: ");
/* get input from the user */
scanf("%f", &weight);
/* assume rhodium is $770 per ounce */
/* 14.5833 converts pounds into rhodium unit*/
value = 770.0 * weight * 14.5833;
printf("Your weight in rhodium is worth $%.2f.n", value);
printf("You are easily worth that! If rhodium prices drop,n");
printf("eat more to maintain your value.n");
}
Output
Are you worth your weight in rhodium?
Let's check it out.
Please enter your weight in pounds: 150
Your weight in rhodium is worth $1684371.12.
You are easily worth that! If rhodium prices drop,
eat more to maintain your value.
#include <stdio.h>
int main(void)
{
int x = 100;
printf("dec = %d; octal = %o; hex = %xn", x, x, x);
printf("dec = %d; octal = %#o; hex = %#xn", x, x, x);
return 0;
}
Output
dec = 100; octal 144; hex = 64
dec = 100; octal = = 0144; hex = 0x64
Declaring Other Integer Types
long int estine;
long johns;
short int erns;
short ribs;
unsigned int s_count;
unsigned players;
unsigned long headcount;
unsigned short yesvotes;
long long ago;
Integer Overflow
#include <stdio.h>
int main(void)
{
int i = 2147483647;
unsigned int j = 4294967295;
printf("%d %d %dn", i, i+1, i+2);
printf("%u %u %un", j, j+1, j+2);
return 0;
}
Output
2147483647 -2147483648 -2147483647
4294967295 0 1
Printing short, long, long long, and
unsigned Types
To print an unsigned int number, use the %u notation. To print a long value, use
the %ld format specifier. If int and long are the same size on your system, just
%d will suffice, the %ld specifier for long. the %lx to print a long integer in
hexadecimal format and %lo to print in octal format the %hd displays a short
integer in decimal form, and %ho displays a short integer in octal form. Both the
h and l prefixes can be used with u for unsigned types. the %lu for unsigned long
types. the %lld and %llu for the signed long long and unsigned long long,
respectively.
/* print2.c-more printf() properties */
#include <stdio.h>
int main(void)
{
unsigned int un = 3000000000; /* system with 32-bit int */
short end = 200; /* and 16-bit short */
long big = 65537;
long long verybig = 12345678908642;
printf("un = %u and not %dn", un, un);
printf("end = %hd and %dn", end, end);
printf("big = %ld and not %hdn", big, big);
printf("verybig= %lld and not %ldn", verybig, verybig);
return 0;
}
Output
un = 3000000000 and not -1294967296
end = 200 and 200
big = 65537 and not 1
verybig= 12345678908642 and not 1942899938
Match the Type printf() Specifiers
Remember to check to see that you have one format specifier for each value being
displayed in a printf() statement. And also check that the type of each format
specifier matches the type of the corresponding display value
Character Constants and Initialization
char response;
char itable, latan;
char grade = 'A';
char broiled; /* declare a char variable */
broiled = 'T'; /* OK */
broiled = T; /* NO! Thinks T is a variable */
broiled = "T"; /* NO! Thinks "T" is a string */
char grade = 65; /* ok for ASCII, but poor style */
Character Escape Sequences
a Alert (ANSI C).
b Backspace.
f Form feed.
n Newline.
r Carriage return.
t Horizontal tab.
v Vertical tab.
 Backslash ().
‘ Single quote (').
“ Double quote (").
? Question mark (?).
0oo Octal value. (o represents an octal digit.)
xhh Hexadecimal value. (h represents a hexadecimal digit.)
Example
char nerf = 'n';
and then print the variable nerf to advance the printer or screen one line.
/* charcode.c-displays code number for a character */
#include <stdio.h>
int main(void)
{
char ch;
printf("Please enter a character.n");
scanf("%c", &ch); /* user inputs character */
printf("The code for %c is %d.n", ch, ch);
return 0;
}
Output
Please enter a character.
C
The code for C is 67.
Printing Floating-Point Values
The printf() function uses the %f format specifier to print type float and double
numbers using decimal notation, and it uses %e to print them in exponential
notation. If your system supports the C99 hexadecimal format for floating-point
numbers, you can use a or A instead of e or E. The long double type requires the
%Lf, %Le, and %La specifiers to print that type. Note that both float and double
use the %f, %e, or %a specifier for output. That's because C automatically expands
type float values to type double when they are passed as arguments to any
function, such as printf().
/* showf_pt.c -- displays float value in two ways */
#include <stdio.h>
int main(void)
{
float aboat = 32000.0;
double abet = 2.14e9;
long double dip = 5.32e-5;
printf("%f can be written %en", aboat, aboat);
printf("%f can be written %en", abet, abet);
printf("%f can be written %en", dip, dip);
return 0;
}
Output
32000.000000 can be written 3.200000e+04
2140000000.000000 can be written 2.140000e+09
0.000053 can be written 5.320000e-05
Floating-Point Overflow and Underflow
Suppose the biggest possible float value on your system is about 3.4E38 and you
do this:
float toobig = 3.4E38 * 100.0f;
printf("%en", toobig);
What happens? This is an example of overflow—when a calculation leads to a
number too large to be expressed. The behavior for this case used to be undefined,
but now C specifies that too big gets assigned a special value that stands for
infinity and that printf() displays either inf or infinity (or some variation on that
theme) for the value.
/* floaterr.c--demonstrates round-off error */
#include <stdio.h>
int main(void)
{
float a,b;
b = 2.0e20 + 1.0;
a = b - 2.0e20;
printf("%f n", a);
return 0;
}
Output
0.000000 “older gcc on Linux”
-13584010575872.000000 “Turbo C 1.5”
4008175468544.000000 “CodeWarrior 9.0, MSVC++ 7.1”
The sizeof() Operator & the size_t type
• the sizeof() operator returns the size, in bytes, of its operand. The
operand can be a specific data object, such as the name of a variable, or
it can be a type. If it is a type, such as float, the operand must be
enclosed in parentheses.
• C says that sizeof returns a value of type size_t. This is an unsigned
integer type, but not a brand-new type. Instead, like the portable types
(int32_t and so on), it is defined in terms of the standard types
#include <stdio.h>
int main(void)
{
int n = 0;
size_t intsize;
intsize = sizeof (int);
printf("n = %d, n has %ud bytes; all ints have %ud bytes.n", n,
sizeof(n), intsize );
return 0;
}
Output
n= 0, n has 4 bytes; all ints have 4 bytes

More Related Content

What's hot (18)

Programming in C [Module One]
Programming in C [Module One]Programming in C [Module One]
Programming in C [Module One]
 
8 arrays and pointers
8  arrays and pointers8  arrays and pointers
8 arrays and pointers
 
Lecture 6- Intorduction to C Programming
Lecture 6- Intorduction to C ProgrammingLecture 6- Intorduction to C Programming
Lecture 6- Intorduction to C Programming
 
Introduction to C programming
Introduction to C programmingIntroduction to C programming
Introduction to C programming
 
3 character strings and formatted input output
3  character strings and formatted input output3  character strings and formatted input output
3 character strings and formatted input output
 
Cbasic
CbasicCbasic
Cbasic
 
Intro to c chapter cover 1 4
Intro to c chapter cover 1 4Intro to c chapter cover 1 4
Intro to c chapter cover 1 4
 
Csharp_Chap06
Csharp_Chap06Csharp_Chap06
Csharp_Chap06
 
C# overview part 1
C# overview part 1C# overview part 1
C# overview part 1
 
T02 a firstcprogram
T02 a firstcprogramT02 a firstcprogram
T02 a firstcprogram
 
UNIT-II CP DOC.docx
UNIT-II CP DOC.docxUNIT-II CP DOC.docx
UNIT-II CP DOC.docx
 
Lecture03
Lecture03Lecture03
Lecture03
 
1
11
1
 
Input output functions
Input output functionsInput output functions
Input output functions
 
string , pointer
string , pointerstring , pointer
string , pointer
 
Lecture 12 - Recursion
Lecture 12 - Recursion Lecture 12 - Recursion
Lecture 12 - Recursion
 
C programing Tutorial
C programing TutorialC programing Tutorial
C programing Tutorial
 
Cbasic
CbasicCbasic
Cbasic
 

Similar to 2 data and c

Fundamental of C Programming Language and Basic Input/Output Function
  Fundamental of C Programming Language and Basic Input/Output Function  Fundamental of C Programming Language and Basic Input/Output Function
Fundamental of C Programming Language and Basic Input/Output Functionimtiazalijoono
 
Programming in C - interview questions.pdf
Programming in C - interview questions.pdfProgramming in C - interview questions.pdf
Programming in C - interview questions.pdfSergiuMatei7
 
T02 a firstcprogram
T02 a firstcprogramT02 a firstcprogram
T02 a firstcprogramprincepavan
 
C cheat sheet for varsity (extreme edition)
C cheat sheet for varsity (extreme edition)C cheat sheet for varsity (extreme edition)
C cheat sheet for varsity (extreme edition)Saifur Rahman
 
Error correction-and-type-of-error-in-c
Error correction-and-type-of-error-in-cError correction-and-type-of-error-in-c
Error correction-and-type-of-error-in-cMd Nazmul Hossain Mir
 
Programming ppt files (final)
Programming ppt files (final)Programming ppt files (final)
Programming ppt files (final)yap_raiza
 
Programming in C Basics
Programming in C BasicsProgramming in C Basics
Programming in C BasicsBharat Kalia
 
C Programming by Süleyman Kondakci
C Programming by Süleyman KondakciC Programming by Süleyman Kondakci
C Programming by Süleyman KondakciSüleyman Kondakci
 
presentation_data_types_and_operators_1513499834_241350.pptx
presentation_data_types_and_operators_1513499834_241350.pptxpresentation_data_types_and_operators_1513499834_241350.pptx
presentation_data_types_and_operators_1513499834_241350.pptxKrishanPalSingh39
 
C++ Tutorial.docx
C++ Tutorial.docxC++ Tutorial.docx
C++ Tutorial.docxPinkiVats1
 
Core programming in c
Core programming in cCore programming in c
Core programming in cRahul Pandit
 
MANAGING INPUT AND OUTPUT OPERATIONS IN C MRS.SOWMYA JYOTHI.pdf
MANAGING INPUT AND OUTPUT OPERATIONS IN C    MRS.SOWMYA JYOTHI.pdfMANAGING INPUT AND OUTPUT OPERATIONS IN C    MRS.SOWMYA JYOTHI.pdf
MANAGING INPUT AND OUTPUT OPERATIONS IN C MRS.SOWMYA JYOTHI.pdfSowmyaJyothi3
 
Assignment c programming
Assignment c programmingAssignment c programming
Assignment c programmingIcaii Infotech
 

Similar to 2 data and c (20)

Fundamental of C Programming Language and Basic Input/Output Function
  Fundamental of C Programming Language and Basic Input/Output Function  Fundamental of C Programming Language and Basic Input/Output Function
Fundamental of C Programming Language and Basic Input/Output Function
 
C tutoria input outputl
C tutoria input outputlC tutoria input outputl
C tutoria input outputl
 
Programming in C - interview questions.pdf
Programming in C - interview questions.pdfProgramming in C - interview questions.pdf
Programming in C - interview questions.pdf
 
T02 a firstcprogram
T02 a firstcprogramT02 a firstcprogram
T02 a firstcprogram
 
Introduction to Input/Output Functions in C
Introduction to Input/Output Functions in CIntroduction to Input/Output Functions in C
Introduction to Input/Output Functions in C
 
C cheat sheet for varsity (extreme edition)
C cheat sheet for varsity (extreme edition)C cheat sheet for varsity (extreme edition)
C cheat sheet for varsity (extreme edition)
 
C programming
C programmingC programming
C programming
 
Error correction-and-type-of-error-in-c
Error correction-and-type-of-error-in-cError correction-and-type-of-error-in-c
Error correction-and-type-of-error-in-c
 
Presentation1
Presentation1Presentation1
Presentation1
 
input
inputinput
input
 
C Basics
C BasicsC Basics
C Basics
 
Programming ppt files (final)
Programming ppt files (final)Programming ppt files (final)
Programming ppt files (final)
 
C Programming
C ProgrammingC Programming
C Programming
 
Programming in C Basics
Programming in C BasicsProgramming in C Basics
Programming in C Basics
 
C Programming by Süleyman Kondakci
C Programming by Süleyman KondakciC Programming by Süleyman Kondakci
C Programming by Süleyman Kondakci
 
presentation_data_types_and_operators_1513499834_241350.pptx
presentation_data_types_and_operators_1513499834_241350.pptxpresentation_data_types_and_operators_1513499834_241350.pptx
presentation_data_types_and_operators_1513499834_241350.pptx
 
C++ Tutorial.docx
C++ Tutorial.docxC++ Tutorial.docx
C++ Tutorial.docx
 
Core programming in c
Core programming in cCore programming in c
Core programming in c
 
MANAGING INPUT AND OUTPUT OPERATIONS IN C MRS.SOWMYA JYOTHI.pdf
MANAGING INPUT AND OUTPUT OPERATIONS IN C    MRS.SOWMYA JYOTHI.pdfMANAGING INPUT AND OUTPUT OPERATIONS IN C    MRS.SOWMYA JYOTHI.pdf
MANAGING INPUT AND OUTPUT OPERATIONS IN C MRS.SOWMYA JYOTHI.pdf
 
Assignment c programming
Assignment c programmingAssignment c programming
Assignment c programming
 

More from MomenMostafa

5 c control statements looping
5  c control statements looping5  c control statements looping
5 c control statements loopingMomenMostafa
 
1 introducing c language
1  introducing c language1  introducing c language
1 introducing c languageMomenMostafa
 
Storage classes, linkage & memory management
Storage classes, linkage & memory managementStorage classes, linkage & memory management
Storage classes, linkage & memory managementMomenMostafa
 
C programming & data structure [character strings & string functions]
C programming & data structure   [character strings & string functions]C programming & data structure   [character strings & string functions]
C programming & data structure [character strings & string functions]MomenMostafa
 
C programming & data structure [arrays & pointers]
C programming & data structure   [arrays & pointers]C programming & data structure   [arrays & pointers]
C programming & data structure [arrays & pointers]MomenMostafa
 
Embedded System Practical Workshop using the ARM Processor
Embedded System Practical Workshop using the ARM ProcessorEmbedded System Practical Workshop using the ARM Processor
Embedded System Practical Workshop using the ARM ProcessorMomenMostafa
 

More from MomenMostafa (6)

5 c control statements looping
5  c control statements looping5  c control statements looping
5 c control statements looping
 
1 introducing c language
1  introducing c language1  introducing c language
1 introducing c language
 
Storage classes, linkage & memory management
Storage classes, linkage & memory managementStorage classes, linkage & memory management
Storage classes, linkage & memory management
 
C programming & data structure [character strings & string functions]
C programming & data structure   [character strings & string functions]C programming & data structure   [character strings & string functions]
C programming & data structure [character strings & string functions]
 
C programming & data structure [arrays & pointers]
C programming & data structure   [arrays & pointers]C programming & data structure   [arrays & pointers]
C programming & data structure [arrays & pointers]
 
Embedded System Practical Workshop using the ARM Processor
Embedded System Practical Workshop using the ARM ProcessorEmbedded System Practical Workshop using the ARM Processor
Embedded System Practical Workshop using the ARM Processor
 

Recently uploaded

Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)OPEN KNOWLEDGE GmbH
 
Folding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesFolding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesPhilip Schwarz
 
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样umasea
 
Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Andreas Granig
 
Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEEVICTOR MAESTRE RAMIREZ
 
software engineering Chapter 5 System modeling.pptx
software engineering Chapter 5 System modeling.pptxsoftware engineering Chapter 5 System modeling.pptx
software engineering Chapter 5 System modeling.pptxnada99848
 
Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...OnePlan Solutions
 
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataAdobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataBradBedford3
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software DevelopersVinodh Ram
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...MyIntelliSource, Inc.
 
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEOrtus Solutions, Corp
 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityNeo4j
 
chapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptchapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptkotipi9215
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackVICTOR MAESTRE RAMIREZ
 
Unveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsUnveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsAhmed Mohamed
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...stazi3110
 
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio, Inc.
 
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxTier1 app
 

Recently uploaded (20)

Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)
 
Folding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesFolding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a series
 
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
 
Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024
 
Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEE
 
software engineering Chapter 5 System modeling.pptx
software engineering Chapter 5 System modeling.pptxsoftware engineering Chapter 5 System modeling.pptx
software engineering Chapter 5 System modeling.pptx
 
Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...
 
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataAdobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software Developers
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
 
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered Sustainability
 
chapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptchapter--4-software-project-planning.ppt
chapter--4-software-project-planning.ppt
 
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort ServiceHot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStack
 
Unveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsUnveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML Diagrams
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
 
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
 
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
 
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
 

2 data and c

  • 3. Bit: the smallest unit of a hardware memory “Binary digit”. Byte: the computer memory unit, it’s 8-bit for nearly all computers. Word: the natural computer memory unit for a given processor architecture, for example 8-bit microcomputers has word size of 8-bit for early IBM compatibles using 80286 processors are 16-bit which means word size are 16- bit long, and so on….
  • 4. /* print1.c-displays some properties of printf() */ #include <stdio.h> int main(void) { int ten = 10; int two = 2; printf("Doing it right: "); printf("%d minus %d is %dn", ten, 2, ten - two ); printf("Doing it wrong: "); printf("%d minus %d is %dn", ten ); // forgot 2 arguments return 0; }
  • 5. Output Doing it right: 10 minus 2 is 8 Doing it wrong: 10 minus 10 is 2
  • 6. Integer An integer is a number with no fractional part. In C, an integer is never written with a decimal point. Examples are 2, –23, and 2456. Numbers such as 3.14, 0.22, and 2.000 are not integers. Integers are stored as binary numbers. The integer 7, for example, is written 111 in binary. Therefore, to store this number in an 8-bit byte, just set the first 5 bits to 0 and the last 3 bits to 1
  • 7. Float A floating-point number more or less corresponds to what mathematicians call a real number. Real numbers include the numbers between the integers. Some floating-point numbers are 2.75, 3.16E7, 7.00, and 2e–8. Notice that adding a decimal point makes a value a floating-point value. So 7 is an integer type but 7.00 is a floating-point type. Obviously, there is more than one way to write a floating-point number. We will discuss the e-notation more fully later, but, in brief, the notation 3.16E7 means to multiply 3.16 by 10 to the 7th power; that is, by 1 followed by 7 zeros. The 7 would be termed the exponent of 10.
  • 8. /* Weight.c -- your weight in gm */ #include <stdio.h> int main(void) { float weight; /* user weight */ float value; /* rhodium equivalent */ printf(“Are you worth your weight in rhodium.n"); printf(“Let’s check it out.n”); printf("Please enter your weight in pounds: ");
  • 9. /* get input from the user */ scanf("%f", &weight); /* assume rhodium is $770 per ounce */ /* 14.5833 converts pounds into rhodium unit*/ value = 770.0 * weight * 14.5833; printf("Your weight in rhodium is worth $%.2f.n", value); printf("You are easily worth that! If rhodium prices drop,n"); printf("eat more to maintain your value.n"); }
  • 10. Output Are you worth your weight in rhodium? Let's check it out. Please enter your weight in pounds: 150 Your weight in rhodium is worth $1684371.12. You are easily worth that! If rhodium prices drop, eat more to maintain your value.
  • 11. #include <stdio.h> int main(void) { int x = 100; printf("dec = %d; octal = %o; hex = %xn", x, x, x); printf("dec = %d; octal = %#o; hex = %#xn", x, x, x); return 0; }
  • 12. Output dec = 100; octal 144; hex = 64 dec = 100; octal = = 0144; hex = 0x64
  • 13. Declaring Other Integer Types long int estine; long johns; short int erns; short ribs; unsigned int s_count; unsigned players; unsigned long headcount; unsigned short yesvotes; long long ago;
  • 14. Integer Overflow #include <stdio.h> int main(void) { int i = 2147483647; unsigned int j = 4294967295; printf("%d %d %dn", i, i+1, i+2); printf("%u %u %un", j, j+1, j+2); return 0; }
  • 16. Printing short, long, long long, and unsigned Types To print an unsigned int number, use the %u notation. To print a long value, use the %ld format specifier. If int and long are the same size on your system, just %d will suffice, the %ld specifier for long. the %lx to print a long integer in hexadecimal format and %lo to print in octal format the %hd displays a short integer in decimal form, and %ho displays a short integer in octal form. Both the h and l prefixes can be used with u for unsigned types. the %lu for unsigned long types. the %lld and %llu for the signed long long and unsigned long long, respectively.
  • 17. /* print2.c-more printf() properties */ #include <stdio.h> int main(void) { unsigned int un = 3000000000; /* system with 32-bit int */ short end = 200; /* and 16-bit short */ long big = 65537; long long verybig = 12345678908642; printf("un = %u and not %dn", un, un); printf("end = %hd and %dn", end, end); printf("big = %ld and not %hdn", big, big); printf("verybig= %lld and not %ldn", verybig, verybig); return 0; }
  • 18. Output un = 3000000000 and not -1294967296 end = 200 and 200 big = 65537 and not 1 verybig= 12345678908642 and not 1942899938
  • 19. Match the Type printf() Specifiers Remember to check to see that you have one format specifier for each value being displayed in a printf() statement. And also check that the type of each format specifier matches the type of the corresponding display value
  • 20. Character Constants and Initialization char response; char itable, latan; char grade = 'A'; char broiled; /* declare a char variable */ broiled = 'T'; /* OK */ broiled = T; /* NO! Thinks T is a variable */ broiled = "T"; /* NO! Thinks "T" is a string */ char grade = 65; /* ok for ASCII, but poor style */
  • 21. Character Escape Sequences a Alert (ANSI C). b Backspace. f Form feed. n Newline. r Carriage return. t Horizontal tab. v Vertical tab. Backslash (). ‘ Single quote (').
  • 22. “ Double quote ("). ? Question mark (?). 0oo Octal value. (o represents an octal digit.) xhh Hexadecimal value. (h represents a hexadecimal digit.) Example char nerf = 'n'; and then print the variable nerf to advance the printer or screen one line.
  • 23. /* charcode.c-displays code number for a character */ #include <stdio.h> int main(void) { char ch; printf("Please enter a character.n"); scanf("%c", &ch); /* user inputs character */ printf("The code for %c is %d.n", ch, ch); return 0; }
  • 24. Output Please enter a character. C The code for C is 67.
  • 25. Printing Floating-Point Values The printf() function uses the %f format specifier to print type float and double numbers using decimal notation, and it uses %e to print them in exponential notation. If your system supports the C99 hexadecimal format for floating-point numbers, you can use a or A instead of e or E. The long double type requires the %Lf, %Le, and %La specifiers to print that type. Note that both float and double use the %f, %e, or %a specifier for output. That's because C automatically expands type float values to type double when they are passed as arguments to any function, such as printf().
  • 26. /* showf_pt.c -- displays float value in two ways */ #include <stdio.h> int main(void) { float aboat = 32000.0; double abet = 2.14e9; long double dip = 5.32e-5; printf("%f can be written %en", aboat, aboat); printf("%f can be written %en", abet, abet); printf("%f can be written %en", dip, dip); return 0; }
  • 27. Output 32000.000000 can be written 3.200000e+04 2140000000.000000 can be written 2.140000e+09 0.000053 can be written 5.320000e-05
  • 28. Floating-Point Overflow and Underflow Suppose the biggest possible float value on your system is about 3.4E38 and you do this: float toobig = 3.4E38 * 100.0f; printf("%en", toobig); What happens? This is an example of overflow—when a calculation leads to a number too large to be expressed. The behavior for this case used to be undefined, but now C specifies that too big gets assigned a special value that stands for infinity and that printf() displays either inf or infinity (or some variation on that theme) for the value.
  • 29. /* floaterr.c--demonstrates round-off error */ #include <stdio.h> int main(void) { float a,b; b = 2.0e20 + 1.0; a = b - 2.0e20; printf("%f n", a); return 0; }
  • 30. Output 0.000000 “older gcc on Linux” -13584010575872.000000 “Turbo C 1.5” 4008175468544.000000 “CodeWarrior 9.0, MSVC++ 7.1”
  • 31. The sizeof() Operator & the size_t type • the sizeof() operator returns the size, in bytes, of its operand. The operand can be a specific data object, such as the name of a variable, or it can be a type. If it is a type, such as float, the operand must be enclosed in parentheses. • C says that sizeof returns a value of type size_t. This is an unsigned integer type, but not a brand-new type. Instead, like the portable types (int32_t and so on), it is defined in terms of the standard types
  • 32. #include <stdio.h> int main(void) { int n = 0; size_t intsize; intsize = sizeof (int); printf("n = %d, n has %ud bytes; all ints have %ud bytes.n", n, sizeof(n), intsize ); return 0; }
  • 33. Output n= 0, n has 4 bytes; all ints have 4 bytes