SlideShare une entreprise Scribd logo
1  sur  20
First Program
 The C program starting point : main().
 main() {} indicates where the program actually starts and
ends.
 In general, braces {} are used throughout C to enclose a block
of statements to be treated as a unit.
 COMMON ERROR: unbalanced number of open and close curly
brackets!
#include <iostream>
int main()
{
/* My first program */
cout<<"Hello World!“<<endl;
return 0;
}
Output :
Hello World!
First Program
 #include <stdio.h>
 Including a header file stdio.h
 Allows the use of printf function
 For each function built into the language, an associated header file
must be included.
 printf() is actually a function (procedure) in C that is used for
printing variables and text
Output :
Hello World!
#include <stdio.h>
int main()
{
/* My first program */
printf("Hello World! n");
return 0;
}
First Program
 Comments
 /* My first program */
 Comments are inserted between “/*” and “*/”
 Or, you can use “//”
 Primarily they serve as internal documentation for program
structure and function.
Output :
Hello World!
#include <iostream>
int main()
{
/* My first program */
cout<<"Hello World!“<<endl;
return 0;
}
Why use comments?
 Documentation of variables, functions and algorithms
 Ex) for each function, explain input and output of the
function, and what the function does.
 Describes the program, author, date, modification
changes, revisions,…
Header Files
 Header files contain definitions of functions and variables
 Preprocessor #include insert the codes of a header file into
the source code.
 Standard header files are provided with each compiler
 To use any of the standard functions, the appropriate header
file should be included.
 Ex) to use cout, cin insert #include <iostream>
 In UNIX, standard header files are generally located in the
/usr/include subdirectory
Header Files
 The use of brackets <> informs the compiler to
search the compiler’s include directories for the
specified file.
#include <iostream>
#include <math>
Second Program
#include <iostream>
int main ()
{
int var1;
int var2;
var1=10;
var2=var1+20;
cout<<“var1 = “<<var1<<endl;
cout<<“var2 = “<<var2<<endl;
return 0;
}
Names in C
 Identifiers (variable name)
 Must begin with a character or underscore(_)
 May be followed by any combination of characters,
underscores, or digits(0-9)
 Case sensitive
 Ex) summary, exit_flag, i, _id, jerry7
 Keywords
 Reserved identifiers that have predefined meaning to the C
compiler. C only has 29 keywords.
 Ex) if , else, char, int, while
Symbolic Constants
 Names given to values that cannot be changed.
 Use preprocessor directive #define
 Symbols which occur in the C program are replaced
by their value before actual compilation
#define N 3000
#define FALSE 0
#define PI 3.14159
#define FIGURE "triangle"
Declaring Variables
 Variable
 Named memory location where data value is stored
 Each variable has a certain type (e.g. int, char, float, …)
 Contents of a variable can change
 Variables must be declared before use in a program
 Declaration of variables should be done at the opening brace of
a function in C. ( it is more flexible in C++ )
 Basic declaration format
 data_type var1, var2, …;
 Examples)
int i,j,k;
float length, height;
Data Types
 char : 1 byte, capable of holding one character (ascii code)
 int : 4 byte (on 32bit computer) integer
 float : single-precision floating point
 double : double-precision floating point
type size min value max value
char 1byte -27 = -128 27-1 = 127
short 2byte -215 = -32,768 215-1 = 32,767
int 4byte -231 = -2,147,483,648 231-1 = 2,147,483,647
long 4byte -231 = -2,147,483,648 231-1 = 2,147,483,647
• Min/Max values are defined in <limit.h> header file
unsigned type
 Use when representing only positive numbers
Data type size min max
unsigned char 1byte 0 28-1 = 255
unsigned short 2 byte 0 216-1 = 65,535
unsigned int 4byte 0 232-1 = 4,294,967,295
Negative integer representation
 signed
 first bit represents the sign of a number
 Rest of bits represent the value of a number
 Negative integer number
 Represented as 2’s complement
number Bit representation
+5 00000101
1’s complement of 5 11111010
2’s complement of 5 11111011
-5 11111011
floating point
 real number : significant number + position of decimal point
 Decimal point(.) can be placed anywhere relative to the
significant digits of the number
 This position is indicated separately in the internal representation
 Advantage of floating point representation
 Support much wider range of values
 Representing 314159265358979.3 vs 3.141592653589793
type size min max
float 4 byte
(7 significant numbers)
-1.0E+38
(7 significant numbers)
1.0E+38
double 8 byte
(15 significant numbers)
-1.0E+308
(15 significant numbers)
1.0E+308
Ascii Code
Escape character
 Starts with backslash()
 Indicate special meaning and interpretation
Escape character meaning
b backspace
t tab
n newline
r formfeed
" double quote
' single quote
 back slash
code.c
output:
a 97
A 65
1 49
$ 36
+ 43
a 97
A 65
1 49
$ 36
+ 43
getchar() , putchar()
 int getchar()
 Defined in <stdio.h>,
 Get one character input from keyboard and return the ascii value
 int putchar(int c)
 Defined in <stdio.h>
 prints one character provided as a parameter
#include <stdio.h>
int main()
{
int c;
printf(“keyboard input (one character?)”);
c=getchar();
printf(“character input : %cn”,c);
printf(“ascii code : %dn”, c);
return 0;
}
Output :
character input : A
ascii code : 65
korea.c
#include <stdio.h>
int main()
{
short no_univ = 276;
int population = 48295000;
long budget = 237000000000000L;
printf(“korea infon”);
printf(“univ no : %dn”, no_univ);
printf(“population : %dn”, population);
printf(“budget : %dn”, budget);
return 0;
}
Output :
korea info
univ no : 276
putpulation: 48295000
budget: -590360576
Overflow?
 (integer type) overflow
 occurs when storing a value that is bigger than what can be
stored.
 Ex) 2,147,483,647 (= 2
31
-1) + 1 = ?
01111111 11111111 11111111 11111111
+ 00000000 00000000 00000000 00000001
--------------------------------------------------
10000000 00000000 00000000 00000000
#include <stdio.h>
int main()
{
int a=2147483647;
printf("%d,%dn",a,a+1);
return 0;
}

Contenu connexe

Similaire à 7512635.ppt

programming language in c&c++
programming language in c&c++programming language in c&c++
programming language in c&c++Haripritha
 
C++ Unit 1PPT which contains the Introduction and basic o C++ with OOOps conc...
C++ Unit 1PPT which contains the Introduction and basic o C++ with OOOps conc...C++ Unit 1PPT which contains the Introduction and basic o C++ with OOOps conc...
C++ Unit 1PPT which contains the Introduction and basic o C++ with OOOps conc...ANUSUYA S
 
the refernce of programming C notes ppt.pptx
the refernce of programming C notes ppt.pptxthe refernce of programming C notes ppt.pptx
the refernce of programming C notes ppt.pptxAnkitaVerma776806
 
C programming(part 3)
C programming(part 3)C programming(part 3)
C programming(part 3)SURBHI SAROHA
 
Introduction to C Unit 1
Introduction to C Unit 1Introduction to C Unit 1
Introduction to C Unit 1SURBHI SAROHA
 
Input and Output In C Language
Input and Output In C LanguageInput and Output In C Language
Input and Output In C LanguageAdnan Khan
 
introductiontoc-140704113737-phpapp01.pdf
introductiontoc-140704113737-phpapp01.pdfintroductiontoc-140704113737-phpapp01.pdf
introductiontoc-140704113737-phpapp01.pdfSameerKhanPathan7
 
Fundamentals of Programming Constructs.pptx
Fundamentals of  Programming Constructs.pptxFundamentals of  Programming Constructs.pptx
Fundamentals of Programming Constructs.pptxvijayapraba1
 
C Programming ppt for beginners . Introduction
C Programming ppt for beginners . IntroductionC Programming ppt for beginners . Introduction
C Programming ppt for beginners . Introductionraghukatagall2
 

Similaire à 7512635.ppt (20)

programming language in c&c++
programming language in c&c++programming language in c&c++
programming language in c&c++
 
UNIT-II CP DOC.docx
UNIT-II CP DOC.docxUNIT-II CP DOC.docx
UNIT-II CP DOC.docx
 
C++ Unit 1PPT which contains the Introduction and basic o C++ with OOOps conc...
C++ Unit 1PPT which contains the Introduction and basic o C++ with OOOps conc...C++ Unit 1PPT which contains the Introduction and basic o C++ with OOOps conc...
C++ Unit 1PPT which contains the Introduction and basic o C++ with OOOps conc...
 
Fp201 unit2 1
Fp201 unit2 1Fp201 unit2 1
Fp201 unit2 1
 
c.ppt
c.pptc.ppt
c.ppt
 
Theory1&amp;2
Theory1&amp;2Theory1&amp;2
Theory1&amp;2
 
Csdfsadf
CsdfsadfCsdfsadf
Csdfsadf
 
C
CC
C
 
C
CC
C
 
the refernce of programming C notes ppt.pptx
the refernce of programming C notes ppt.pptxthe refernce of programming C notes ppt.pptx
the refernce of programming C notes ppt.pptx
 
C programming(part 3)
C programming(part 3)C programming(part 3)
C programming(part 3)
 
Introduction to C Unit 1
Introduction to C Unit 1Introduction to C Unit 1
Introduction to C Unit 1
 
C programming language
C programming languageC programming language
C programming language
 
Input and Output In C Language
Input and Output In C LanguageInput and Output In C Language
Input and Output In C Language
 
introductiontoc-140704113737-phpapp01.pdf
introductiontoc-140704113737-phpapp01.pdfintroductiontoc-140704113737-phpapp01.pdf
introductiontoc-140704113737-phpapp01.pdf
 
Introduction to c++
Introduction to c++Introduction to c++
Introduction to c++
 
Fundamentals of Programming Constructs.pptx
Fundamentals of  Programming Constructs.pptxFundamentals of  Programming Constructs.pptx
Fundamentals of Programming Constructs.pptx
 
C Theory
C TheoryC Theory
C Theory
 
C Programming ppt for beginners . Introduction
C Programming ppt for beginners . IntroductionC Programming ppt for beginners . Introduction
C Programming ppt for beginners . Introduction
 
C notes.pdf
C notes.pdfC notes.pdf
C notes.pdf
 

Dernier

DDoS In Oceania and the Pacific, presented by Dave Phelan at NZNOG 2024
DDoS In Oceania and the Pacific, presented by Dave Phelan at NZNOG 2024DDoS In Oceania and the Pacific, presented by Dave Phelan at NZNOG 2024
DDoS In Oceania and the Pacific, presented by Dave Phelan at NZNOG 2024APNIC
 
Call Now ☎ 8264348440 !! Call Girls in Sarai Rohilla Escort Service Delhi N.C.R.
Call Now ☎ 8264348440 !! Call Girls in Sarai Rohilla Escort Service Delhi N.C.R.Call Now ☎ 8264348440 !! Call Girls in Sarai Rohilla Escort Service Delhi N.C.R.
Call Now ☎ 8264348440 !! Call Girls in Sarai Rohilla Escort Service Delhi N.C.R.soniya singh
 
All Time Service Available Call Girls Mg Road 👌 ⏭️ 6378878445
All Time Service Available Call Girls Mg Road 👌 ⏭️ 6378878445All Time Service Available Call Girls Mg Road 👌 ⏭️ 6378878445
All Time Service Available Call Girls Mg Road 👌 ⏭️ 6378878445ruhi
 
Call Girls In Ashram Chowk Delhi 💯Call Us 🔝8264348440🔝
Call Girls In Ashram Chowk Delhi 💯Call Us 🔝8264348440🔝Call Girls In Ashram Chowk Delhi 💯Call Us 🔝8264348440🔝
Call Girls In Ashram Chowk Delhi 💯Call Us 🔝8264348440🔝soniya singh
 
Call Girls Dubai Prolapsed O525547819 Call Girls In Dubai Princes$
Call Girls Dubai Prolapsed O525547819 Call Girls In Dubai Princes$Call Girls Dubai Prolapsed O525547819 Call Girls In Dubai Princes$
Call Girls Dubai Prolapsed O525547819 Call Girls In Dubai Princes$kojalkojal131
 
On Starlink, presented by Geoff Huston at NZNOG 2024
On Starlink, presented by Geoff Huston at NZNOG 2024On Starlink, presented by Geoff Huston at NZNOG 2024
On Starlink, presented by Geoff Huston at NZNOG 2024APNIC
 
₹5.5k {Cash Payment}New Friends Colony Call Girls In [Delhi NIHARIKA] 🔝|97111...
₹5.5k {Cash Payment}New Friends Colony Call Girls In [Delhi NIHARIKA] 🔝|97111...₹5.5k {Cash Payment}New Friends Colony Call Girls In [Delhi NIHARIKA] 🔝|97111...
₹5.5k {Cash Payment}New Friends Colony Call Girls In [Delhi NIHARIKA] 🔝|97111...Diya Sharma
 
𓀤Call On 7877925207 𓀤 Ahmedguda Call Girls Hot Model With Sexy Bhabi Ready Fo...
𓀤Call On 7877925207 𓀤 Ahmedguda Call Girls Hot Model With Sexy Bhabi Ready Fo...𓀤Call On 7877925207 𓀤 Ahmedguda Call Girls Hot Model With Sexy Bhabi Ready Fo...
𓀤Call On 7877925207 𓀤 Ahmedguda Call Girls Hot Model With Sexy Bhabi Ready Fo...Neha Pandey
 
Moving Beyond Twitter/X and Facebook - Social Media for local news providers
Moving Beyond Twitter/X and Facebook - Social Media for local news providersMoving Beyond Twitter/X and Facebook - Social Media for local news providers
Moving Beyond Twitter/X and Facebook - Social Media for local news providersDamian Radcliffe
 
Call Girls In Model Towh Delhi 💯Call Us 🔝8264348440🔝
Call Girls In Model Towh Delhi 💯Call Us 🔝8264348440🔝Call Girls In Model Towh Delhi 💯Call Us 🔝8264348440🔝
Call Girls In Model Towh Delhi 💯Call Us 🔝8264348440🔝soniya singh
 
Best VIP Call Girls Noida Sector 75 Call Me: 8448380779
Best VIP Call Girls Noida Sector 75 Call Me: 8448380779Best VIP Call Girls Noida Sector 75 Call Me: 8448380779
Best VIP Call Girls Noida Sector 75 Call Me: 8448380779Delhi Call girls
 
How is AI changing journalism? (v. April 2024)
How is AI changing journalism? (v. April 2024)How is AI changing journalism? (v. April 2024)
How is AI changing journalism? (v. April 2024)Damian Radcliffe
 
Enjoy Night⚡Call Girls Dlf City Phase 3 Gurgaon >༒8448380779 Escort Service
Enjoy Night⚡Call Girls Dlf City Phase 3 Gurgaon >༒8448380779 Escort ServiceEnjoy Night⚡Call Girls Dlf City Phase 3 Gurgaon >༒8448380779 Escort Service
Enjoy Night⚡Call Girls Dlf City Phase 3 Gurgaon >༒8448380779 Escort ServiceDelhi Call girls
 
Call Now ☎ 8264348440 !! Call Girls in Shahpur Jat Escort Service Delhi N.C.R.
Call Now ☎ 8264348440 !! Call Girls in Shahpur Jat Escort Service Delhi N.C.R.Call Now ☎ 8264348440 !! Call Girls in Shahpur Jat Escort Service Delhi N.C.R.
Call Now ☎ 8264348440 !! Call Girls in Shahpur Jat Escort Service Delhi N.C.R.soniya singh
 
Call Girls Ludhiana Just Call 98765-12871 Top Class Call Girl Service Available
Call Girls Ludhiana Just Call 98765-12871 Top Class Call Girl Service AvailableCall Girls Ludhiana Just Call 98765-12871 Top Class Call Girl Service Available
Call Girls Ludhiana Just Call 98765-12871 Top Class Call Girl Service AvailableSeo
 
Call Girls In Sukhdev Vihar Delhi 💯Call Us 🔝8264348440🔝
Call Girls In Sukhdev Vihar Delhi 💯Call Us 🔝8264348440🔝Call Girls In Sukhdev Vihar Delhi 💯Call Us 🔝8264348440🔝
Call Girls In Sukhdev Vihar Delhi 💯Call Us 🔝8264348440🔝soniya singh
 

Dernier (20)

DDoS In Oceania and the Pacific, presented by Dave Phelan at NZNOG 2024
DDoS In Oceania and the Pacific, presented by Dave Phelan at NZNOG 2024DDoS In Oceania and the Pacific, presented by Dave Phelan at NZNOG 2024
DDoS In Oceania and the Pacific, presented by Dave Phelan at NZNOG 2024
 
Rohini Sector 22 Call Girls Delhi 9999965857 @Sabina Saikh No Advance
Rohini Sector 22 Call Girls Delhi 9999965857 @Sabina Saikh No AdvanceRohini Sector 22 Call Girls Delhi 9999965857 @Sabina Saikh No Advance
Rohini Sector 22 Call Girls Delhi 9999965857 @Sabina Saikh No Advance
 
Call Now ☎ 8264348440 !! Call Girls in Sarai Rohilla Escort Service Delhi N.C.R.
Call Now ☎ 8264348440 !! Call Girls in Sarai Rohilla Escort Service Delhi N.C.R.Call Now ☎ 8264348440 !! Call Girls in Sarai Rohilla Escort Service Delhi N.C.R.
Call Now ☎ 8264348440 !! Call Girls in Sarai Rohilla Escort Service Delhi N.C.R.
 
All Time Service Available Call Girls Mg Road 👌 ⏭️ 6378878445
All Time Service Available Call Girls Mg Road 👌 ⏭️ 6378878445All Time Service Available Call Girls Mg Road 👌 ⏭️ 6378878445
All Time Service Available Call Girls Mg Road 👌 ⏭️ 6378878445
 
Call Girls In Ashram Chowk Delhi 💯Call Us 🔝8264348440🔝
Call Girls In Ashram Chowk Delhi 💯Call Us 🔝8264348440🔝Call Girls In Ashram Chowk Delhi 💯Call Us 🔝8264348440🔝
Call Girls In Ashram Chowk Delhi 💯Call Us 🔝8264348440🔝
 
Call Girls Dubai Prolapsed O525547819 Call Girls In Dubai Princes$
Call Girls Dubai Prolapsed O525547819 Call Girls In Dubai Princes$Call Girls Dubai Prolapsed O525547819 Call Girls In Dubai Princes$
Call Girls Dubai Prolapsed O525547819 Call Girls In Dubai Princes$
 
On Starlink, presented by Geoff Huston at NZNOG 2024
On Starlink, presented by Geoff Huston at NZNOG 2024On Starlink, presented by Geoff Huston at NZNOG 2024
On Starlink, presented by Geoff Huston at NZNOG 2024
 
₹5.5k {Cash Payment}New Friends Colony Call Girls In [Delhi NIHARIKA] 🔝|97111...
₹5.5k {Cash Payment}New Friends Colony Call Girls In [Delhi NIHARIKA] 🔝|97111...₹5.5k {Cash Payment}New Friends Colony Call Girls In [Delhi NIHARIKA] 🔝|97111...
₹5.5k {Cash Payment}New Friends Colony Call Girls In [Delhi NIHARIKA] 🔝|97111...
 
@9999965857 🫦 Sexy Desi Call Girls Laxmi Nagar 💓 High Profile Escorts Delhi 🫶
@9999965857 🫦 Sexy Desi Call Girls Laxmi Nagar 💓 High Profile Escorts Delhi 🫶@9999965857 🫦 Sexy Desi Call Girls Laxmi Nagar 💓 High Profile Escorts Delhi 🫶
@9999965857 🫦 Sexy Desi Call Girls Laxmi Nagar 💓 High Profile Escorts Delhi 🫶
 
𓀤Call On 7877925207 𓀤 Ahmedguda Call Girls Hot Model With Sexy Bhabi Ready Fo...
𓀤Call On 7877925207 𓀤 Ahmedguda Call Girls Hot Model With Sexy Bhabi Ready Fo...𓀤Call On 7877925207 𓀤 Ahmedguda Call Girls Hot Model With Sexy Bhabi Ready Fo...
𓀤Call On 7877925207 𓀤 Ahmedguda Call Girls Hot Model With Sexy Bhabi Ready Fo...
 
Moving Beyond Twitter/X and Facebook - Social Media for local news providers
Moving Beyond Twitter/X and Facebook - Social Media for local news providersMoving Beyond Twitter/X and Facebook - Social Media for local news providers
Moving Beyond Twitter/X and Facebook - Social Media for local news providers
 
Call Girls In Model Towh Delhi 💯Call Us 🔝8264348440🔝
Call Girls In Model Towh Delhi 💯Call Us 🔝8264348440🔝Call Girls In Model Towh Delhi 💯Call Us 🔝8264348440🔝
Call Girls In Model Towh Delhi 💯Call Us 🔝8264348440🔝
 
Best VIP Call Girls Noida Sector 75 Call Me: 8448380779
Best VIP Call Girls Noida Sector 75 Call Me: 8448380779Best VIP Call Girls Noida Sector 75 Call Me: 8448380779
Best VIP Call Girls Noida Sector 75 Call Me: 8448380779
 
VVVIP Call Girls In Connaught Place ➡️ Delhi ➡️ 9999965857 🚀 No Advance 24HRS...
VVVIP Call Girls In Connaught Place ➡️ Delhi ➡️ 9999965857 🚀 No Advance 24HRS...VVVIP Call Girls In Connaught Place ➡️ Delhi ➡️ 9999965857 🚀 No Advance 24HRS...
VVVIP Call Girls In Connaught Place ➡️ Delhi ➡️ 9999965857 🚀 No Advance 24HRS...
 
How is AI changing journalism? (v. April 2024)
How is AI changing journalism? (v. April 2024)How is AI changing journalism? (v. April 2024)
How is AI changing journalism? (v. April 2024)
 
Rohini Sector 6 Call Girls Delhi 9999965857 @Sabina Saikh No Advance
Rohini Sector 6 Call Girls Delhi 9999965857 @Sabina Saikh No AdvanceRohini Sector 6 Call Girls Delhi 9999965857 @Sabina Saikh No Advance
Rohini Sector 6 Call Girls Delhi 9999965857 @Sabina Saikh No Advance
 
Enjoy Night⚡Call Girls Dlf City Phase 3 Gurgaon >༒8448380779 Escort Service
Enjoy Night⚡Call Girls Dlf City Phase 3 Gurgaon >༒8448380779 Escort ServiceEnjoy Night⚡Call Girls Dlf City Phase 3 Gurgaon >༒8448380779 Escort Service
Enjoy Night⚡Call Girls Dlf City Phase 3 Gurgaon >༒8448380779 Escort Service
 
Call Now ☎ 8264348440 !! Call Girls in Shahpur Jat Escort Service Delhi N.C.R.
Call Now ☎ 8264348440 !! Call Girls in Shahpur Jat Escort Service Delhi N.C.R.Call Now ☎ 8264348440 !! Call Girls in Shahpur Jat Escort Service Delhi N.C.R.
Call Now ☎ 8264348440 !! Call Girls in Shahpur Jat Escort Service Delhi N.C.R.
 
Call Girls Ludhiana Just Call 98765-12871 Top Class Call Girl Service Available
Call Girls Ludhiana Just Call 98765-12871 Top Class Call Girl Service AvailableCall Girls Ludhiana Just Call 98765-12871 Top Class Call Girl Service Available
Call Girls Ludhiana Just Call 98765-12871 Top Class Call Girl Service Available
 
Call Girls In Sukhdev Vihar Delhi 💯Call Us 🔝8264348440🔝
Call Girls In Sukhdev Vihar Delhi 💯Call Us 🔝8264348440🔝Call Girls In Sukhdev Vihar Delhi 💯Call Us 🔝8264348440🔝
Call Girls In Sukhdev Vihar Delhi 💯Call Us 🔝8264348440🔝
 

7512635.ppt

  • 1. First Program  The C program starting point : main().  main() {} indicates where the program actually starts and ends.  In general, braces {} are used throughout C to enclose a block of statements to be treated as a unit.  COMMON ERROR: unbalanced number of open and close curly brackets! #include <iostream> int main() { /* My first program */ cout<<"Hello World!“<<endl; return 0; } Output : Hello World!
  • 2. First Program  #include <stdio.h>  Including a header file stdio.h  Allows the use of printf function  For each function built into the language, an associated header file must be included.  printf() is actually a function (procedure) in C that is used for printing variables and text Output : Hello World! #include <stdio.h> int main() { /* My first program */ printf("Hello World! n"); return 0; }
  • 3. First Program  Comments  /* My first program */  Comments are inserted between “/*” and “*/”  Or, you can use “//”  Primarily they serve as internal documentation for program structure and function. Output : Hello World! #include <iostream> int main() { /* My first program */ cout<<"Hello World!“<<endl; return 0; }
  • 4. Why use comments?  Documentation of variables, functions and algorithms  Ex) for each function, explain input and output of the function, and what the function does.  Describes the program, author, date, modification changes, revisions,…
  • 5. Header Files  Header files contain definitions of functions and variables  Preprocessor #include insert the codes of a header file into the source code.  Standard header files are provided with each compiler  To use any of the standard functions, the appropriate header file should be included.  Ex) to use cout, cin insert #include <iostream>  In UNIX, standard header files are generally located in the /usr/include subdirectory
  • 6. Header Files  The use of brackets <> informs the compiler to search the compiler’s include directories for the specified file. #include <iostream> #include <math>
  • 7. Second Program #include <iostream> int main () { int var1; int var2; var1=10; var2=var1+20; cout<<“var1 = “<<var1<<endl; cout<<“var2 = “<<var2<<endl; return 0; }
  • 8. Names in C  Identifiers (variable name)  Must begin with a character or underscore(_)  May be followed by any combination of characters, underscores, or digits(0-9)  Case sensitive  Ex) summary, exit_flag, i, _id, jerry7  Keywords  Reserved identifiers that have predefined meaning to the C compiler. C only has 29 keywords.  Ex) if , else, char, int, while
  • 9. Symbolic Constants  Names given to values that cannot be changed.  Use preprocessor directive #define  Symbols which occur in the C program are replaced by their value before actual compilation #define N 3000 #define FALSE 0 #define PI 3.14159 #define FIGURE "triangle"
  • 10. Declaring Variables  Variable  Named memory location where data value is stored  Each variable has a certain type (e.g. int, char, float, …)  Contents of a variable can change  Variables must be declared before use in a program  Declaration of variables should be done at the opening brace of a function in C. ( it is more flexible in C++ )  Basic declaration format  data_type var1, var2, …;  Examples) int i,j,k; float length, height;
  • 11. Data Types  char : 1 byte, capable of holding one character (ascii code)  int : 4 byte (on 32bit computer) integer  float : single-precision floating point  double : double-precision floating point type size min value max value char 1byte -27 = -128 27-1 = 127 short 2byte -215 = -32,768 215-1 = 32,767 int 4byte -231 = -2,147,483,648 231-1 = 2,147,483,647 long 4byte -231 = -2,147,483,648 231-1 = 2,147,483,647 • Min/Max values are defined in <limit.h> header file
  • 12. unsigned type  Use when representing only positive numbers Data type size min max unsigned char 1byte 0 28-1 = 255 unsigned short 2 byte 0 216-1 = 65,535 unsigned int 4byte 0 232-1 = 4,294,967,295
  • 13. Negative integer representation  signed  first bit represents the sign of a number  Rest of bits represent the value of a number  Negative integer number  Represented as 2’s complement number Bit representation +5 00000101 1’s complement of 5 11111010 2’s complement of 5 11111011 -5 11111011
  • 14. floating point  real number : significant number + position of decimal point  Decimal point(.) can be placed anywhere relative to the significant digits of the number  This position is indicated separately in the internal representation  Advantage of floating point representation  Support much wider range of values  Representing 314159265358979.3 vs 3.141592653589793 type size min max float 4 byte (7 significant numbers) -1.0E+38 (7 significant numbers) 1.0E+38 double 8 byte (15 significant numbers) -1.0E+308 (15 significant numbers) 1.0E+308
  • 16. Escape character  Starts with backslash()  Indicate special meaning and interpretation Escape character meaning b backspace t tab n newline r formfeed " double quote ' single quote back slash
  • 17. code.c output: a 97 A 65 1 49 $ 36 + 43 a 97 A 65 1 49 $ 36 + 43
  • 18. getchar() , putchar()  int getchar()  Defined in <stdio.h>,  Get one character input from keyboard and return the ascii value  int putchar(int c)  Defined in <stdio.h>  prints one character provided as a parameter #include <stdio.h> int main() { int c; printf(“keyboard input (one character?)”); c=getchar(); printf(“character input : %cn”,c); printf(“ascii code : %dn”, c); return 0; } Output : character input : A ascii code : 65
  • 19. korea.c #include <stdio.h> int main() { short no_univ = 276; int population = 48295000; long budget = 237000000000000L; printf(“korea infon”); printf(“univ no : %dn”, no_univ); printf(“population : %dn”, population); printf(“budget : %dn”, budget); return 0; } Output : korea info univ no : 276 putpulation: 48295000 budget: -590360576
  • 20. Overflow?  (integer type) overflow  occurs when storing a value that is bigger than what can be stored.  Ex) 2,147,483,647 (= 2 31 -1) + 1 = ? 01111111 11111111 11111111 11111111 + 00000000 00000000 00000000 00000001 -------------------------------------------------- 10000000 00000000 00000000 00000000 #include <stdio.h> int main() { int a=2147483647; printf("%d,%dn",a,a+1); return 0; }