SlideShare a Scribd company logo
1 of 21
   Before I explain any   int main()
                           {
    code to you, I            int i;
    would like you to         int sum = 0;

    execute your first         for(i = 0; i<10; i++){
                                   sum += i;
    program by                 }
    copying and
                               printf("Sum = %dn", sum);
    pasting my code.           //system("pause");
                               return 0;
                           }
 Locate the compile button and run
  buttons in your program.
 You may alternatively find the compile
  and run button that executes both at
  once.
 You should notice a black box
  appearing and disappearing very
  quickly. That is because I have allowed
  this code to execute without any pause
  statements.
   Programs typically automatically close
    once the final line of code is reached.

   The line system(“pause”); needs to be
    added to “pause” the program in place

   The system pause should be placed right
    before the return 0; line of any program.
    Return 0 is typically used at the end of a
    program to tell the computer the program
    has ended.
   Code is normally presented omitting the
    system("pause"); statement which
    confuses new programmers. Always
    make a mental note to add it in if you
    are testing new code.
 When you place // before a line of
  code, you comment the code out and
  cause the compiler to skip over the
  code.
 Now you can remove the // before the
  system pause line in the example
  program and run the program again to
  see output. If all is correct, you should
  see the output being Sum = 45.
   I want to draw your attention to the first line:
    int main(){
   This is the beginning of your main function. The
    function ends with the use of }.
   Any time you open a { bracket, you must close it with
    a } bracket.
   As you can see with the main function, you can see
    the entire main function enclosed within the { }
    brackets.
   Additional code is allowed within the brackets. This
    includes code that uses another set of { } as well.
   The last { bracket to be opened is the first one to be
    closed when a } bracket is used.
 The main function is the starting point of
  a program and where a compiler first
  starts to execute your code.
 It is required in order to compile your
  code, therefore it would be a good
  practice to use the following template.
int main(){

     //your new written code goes here

     system("pause");
     return 0;
}
   Our program is going to need a way to hold
    our values
   These values are stored in different types of
    variables.
   Integers, floats, and doubles are the main
    “number” holders of a program.
   A variable in C programming must always be
    declared at the top of the program right under
    int main() { if you want to use the variable in
    your program.
   Unlike some programming languages, you may
    not locally declare a variable anywhere.
 An integer is a number only
  representable in a whole number form.
 Integers: 1,2,3,4,5..
 Not Integers: 1.5, 1.7, 2.5, 3.9, 1003.4...
 Integer math is the calculation between
  two integers. The math is calculated as
  you would normally between any two
  numbers except the decimal value is
  dropped. Ex. 1+3=4, 1+.5=1 , 5/4 = 1
   Double and Float variables allow the use of
    decimal numbers. A float has a smaller
    default precision than a double.
   If you declare a float and then try to store
    the number within the float into a double,
    you may be transferring only part of the
    number.
   A float allows at most about 7 decimal
    places precision.
    A double allows at least 7 places.
   To accurately work beyond 7 decimals you
    may require addition resources.
 I typically use only Doubles when working
  with decimals and will work with only
  Doubles within these tutorials.
 The math calculated with a double or
  float does not use integer math. It would
  be safe to assume the calculated
  answer would be similar to the answer
  given in a math class.
 An integer is declared by typing
  int variablename;
 A float is declared by typing
  float variablename;
 A double is declared by typing
  double variablename;
 The variablename is changeable to
  anything starting with a letter and
  anything that has not been assigned to
  a variable yet.
 Multiple variables of the same type are
  declarable by adding a comma after
  the first variable and then typing the
  second.
 int one, two, three;
 The point of a variable is to eventually
  hold data.
 The variable could be initialized
  immediately with a value.
 int one = 1;
 double duck = 12.5;
   To print out the value a variable is holding
    you use the printf statement
   printf(“The variable one = %d”, one);
   As shown above, the text you want printed
    out is held within the quotation marks. A
    %variabletype is added to signal to the
    computer you want to print out a variable.
    int -> %d float -> %fl double -> %lf
   After the comma, you list the variables in
    order that match up with the %variables
 A double could be printed out but it may
  contain too many decimal places.
 Limit a double by using %.#lf for the
  number of decimal places you want to
  be printed out.
 The key “n” to a computer is
  interpreted as a line break.
 When used in a printf statement, it will
  add a line break into the output.
int main(){

        int one = 5.5;
        float two = 7.5;
        double three = 10.5;

        printf("One equals: %d n", one);
        printf("Two equals: %f n", two);
        printf("Three equals: %lf n ", three);

        three = 15;

        printf("Three equals: %lf ", three);

        system("pause");
        return 0;
}
   Programming Template

   Integers, Floats, and Doubles

   Printf statements

More Related Content

What's hot

Types of Statements in Python Programming Language
Types of Statements in Python Programming LanguageTypes of Statements in Python Programming Language
Types of Statements in Python Programming LanguageExplore Skilled
 
FLOWOFCONTROL-IF..ELSE PYTHON
FLOWOFCONTROL-IF..ELSE PYTHONFLOWOFCONTROL-IF..ELSE PYTHON
FLOWOFCONTROL-IF..ELSE PYTHONvikram mahendra
 
Python - Control Structures
Python - Control StructuresPython - Control Structures
Python - Control StructuresLasithNiro
 
C Programming: Control Structure
C Programming: Control StructureC Programming: Control Structure
C Programming: Control StructureSokngim Sa
 
Basic computer-programming-2
Basic computer-programming-2Basic computer-programming-2
Basic computer-programming-2lemonmichelangelo
 
1584503386 1st chap
1584503386 1st chap1584503386 1st chap
1584503386 1st chapthuhiendtk4
 
MATLAB programming tips 2 - Input and Output Commands
MATLAB programming tips 2 - Input and Output CommandsMATLAB programming tips 2 - Input and Output Commands
MATLAB programming tips 2 - Input and Output CommandsShameer Ahmed Koya
 
Control structure of c language
Control structure of c languageControl structure of c language
Control structure of c languageDigvijaysinh Gohil
 
Basic c# cheat sheet
Basic c# cheat sheetBasic c# cheat sheet
Basic c# cheat sheetAhmed Elshal
 
[ITP - Lecture 10] Switch Statement, Break and Continue Statement in C/C++
[ITP - Lecture 10] Switch Statement, Break and Continue Statement in C/C++[ITP - Lecture 10] Switch Statement, Break and Continue Statement in C/C++
[ITP - Lecture 10] Switch Statement, Break and Continue Statement in C/C++Muhammad Hammad Waseem
 
Visula C# Programming Lecture 3
Visula C# Programming Lecture 3Visula C# Programming Lecture 3
Visula C# Programming Lecture 3Abou Bakr Ashraf
 

What's hot (18)

Types of Statements in Python Programming Language
Types of Statements in Python Programming LanguageTypes of Statements in Python Programming Language
Types of Statements in Python Programming Language
 
FLOWOFCONTROL-IF..ELSE PYTHON
FLOWOFCONTROL-IF..ELSE PYTHONFLOWOFCONTROL-IF..ELSE PYTHON
FLOWOFCONTROL-IF..ELSE PYTHON
 
Python - Control Structures
Python - Control StructuresPython - Control Structures
Python - Control Structures
 
Chapter 3
Chapter 3Chapter 3
Chapter 3
 
C Programming: Control Structure
C Programming: Control StructureC Programming: Control Structure
C Programming: Control Structure
 
Basic computer-programming-2
Basic computer-programming-2Basic computer-programming-2
Basic computer-programming-2
 
1584503386 1st chap
1584503386 1st chap1584503386 1st chap
1584503386 1st chap
 
Lecture 9- Control Structures 1
Lecture 9- Control Structures 1Lecture 9- Control Structures 1
Lecture 9- Control Structures 1
 
MATLAB programming tips 2 - Input and Output Commands
MATLAB programming tips 2 - Input and Output CommandsMATLAB programming tips 2 - Input and Output Commands
MATLAB programming tips 2 - Input and Output Commands
 
Control structure of c language
Control structure of c languageControl structure of c language
Control structure of c language
 
Matlab syntax
Matlab syntaxMatlab syntax
Matlab syntax
 
Lecture 10 - Control Structures 2
Lecture 10 - Control Structures 2Lecture 10 - Control Structures 2
Lecture 10 - Control Structures 2
 
Basic c# cheat sheet
Basic c# cheat sheetBasic c# cheat sheet
Basic c# cheat sheet
 
C language assignment help
C language assignment helpC language assignment help
C language assignment help
 
[ITP - Lecture 11] Loops in C/C++
[ITP - Lecture 11] Loops in C/C++[ITP - Lecture 11] Loops in C/C++
[ITP - Lecture 11] Loops in C/C++
 
Operators
OperatorsOperators
Operators
 
[ITP - Lecture 10] Switch Statement, Break and Continue Statement in C/C++
[ITP - Lecture 10] Switch Statement, Break and Continue Statement in C/C++[ITP - Lecture 10] Switch Statement, Break and Continue Statement in C/C++
[ITP - Lecture 10] Switch Statement, Break and Continue Statement in C/C++
 
Visula C# Programming Lecture 3
Visula C# Programming Lecture 3Visula C# Programming Lecture 3
Visula C# Programming Lecture 3
 

Viewers also liked

Mca java application projects completed list(is)
Mca java  application projects completed list(is)Mca java  application projects completed list(is)
Mca java application projects completed list(is)S3 Infotech IEEE Projects
 
SHOW104: Practical Java
SHOW104: Practical JavaSHOW104: Practical Java
SHOW104: Practical JavaMark Myers
 
Java practical(baca sem v)
Java practical(baca sem v)Java practical(baca sem v)
Java practical(baca sem v)mehul patel
 
Java PRACTICAL file
Java PRACTICAL fileJava PRACTICAL file
Java PRACTICAL fileRACHIT_GUPTA
 

Viewers also liked (6)

Mca java application projects completed list(is)
Mca java  application projects completed list(is)Mca java  application projects completed list(is)
Mca java application projects completed list(is)
 
SHOW104: Practical Java
SHOW104: Practical JavaSHOW104: Practical Java
SHOW104: Practical Java
 
Files
FilesFiles
Files
 
Java practical(baca sem v)
Java practical(baca sem v)Java practical(baca sem v)
Java practical(baca sem v)
 
File Uploading in PHP
File Uploading in PHPFile Uploading in PHP
File Uploading in PHP
 
Java PRACTICAL file
Java PRACTICAL fileJava PRACTICAL file
Java PRACTICAL file
 

Similar to C Programming Basics: Variables, Data Types, and Printf

Programming For As Comp
Programming For As CompProgramming For As Comp
Programming For As CompDavid Halliday
 
Programming For As Comp
Programming For As CompProgramming For As Comp
Programming For As CompDavid Halliday
 
Tutorial basic of c ++lesson 1 eng ver
Tutorial basic of c ++lesson 1 eng verTutorial basic of c ++lesson 1 eng ver
Tutorial basic of c ++lesson 1 eng verQrembiezs Intruder
 
C programming perso notes
C programming perso notesC programming perso notes
C programming perso notesMelanie Tsopze
 
Core programming in c
Core programming in cCore programming in c
Core programming in cRahul Pandit
 
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 decision making and looping.
C decision making and looping.C decision making and looping.
C decision making and looping.Haard Shah
 
Enrich enriching mathematics conversi biner 16 pada sisikomputerize indoensia...
Enrich enriching mathematics conversi biner 16 pada sisikomputerize indoensia...Enrich enriching mathematics conversi biner 16 pada sisikomputerize indoensia...
Enrich enriching mathematics conversi biner 16 pada sisikomputerize indoensia...Dadangsachir WANDA ir.mba
 
C++ Course - Lesson 1
C++ Course - Lesson 1C++ Course - Lesson 1
C++ Course - Lesson 1Mohamed Ahmed
 
Introduction to Basic C programming 01
Introduction to Basic C programming 01Introduction to Basic C programming 01
Introduction to Basic C programming 01Wingston
 
Fundamentals of prog. by rubferd medina
Fundamentals of prog. by rubferd medinaFundamentals of prog. by rubferd medina
Fundamentals of prog. by rubferd medinarurumedina
 
Programming fundamental 02
Programming fundamental 02Programming fundamental 02
Programming fundamental 02Suhail Akraam
 

Similar to C Programming Basics: Variables, Data Types, and Printf (20)

Programming For As Comp
Programming For As CompProgramming For As Comp
Programming For As Comp
 
Programming For As Comp
Programming For As CompProgramming For As Comp
Programming For As Comp
 
Tutorial basic of c ++lesson 1 eng ver
Tutorial basic of c ++lesson 1 eng verTutorial basic of c ++lesson 1 eng ver
Tutorial basic of c ++lesson 1 eng ver
 
Lập trình C
Lập trình CLập trình C
Lập trình C
 
C programming perso notes
C programming perso notesC programming perso notes
C programming perso notes
 
lecture 2.pptx
lecture 2.pptxlecture 2.pptx
lecture 2.pptx
 
Maxbox starter
Maxbox starterMaxbox starter
Maxbox starter
 
Core programming in c
Core programming in cCore programming in c
Core programming in 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 decision making and looping.
C decision making and looping.C decision making and looping.
C decision making and looping.
 
Enrich enriching mathematics conversi biner 16 pada sisikomputerize indoensia...
Enrich enriching mathematics conversi biner 16 pada sisikomputerize indoensia...Enrich enriching mathematics conversi biner 16 pada sisikomputerize indoensia...
Enrich enriching mathematics conversi biner 16 pada sisikomputerize indoensia...
 
C++ Course - Lesson 1
C++ Course - Lesson 1C++ Course - Lesson 1
C++ Course - Lesson 1
 
C programming
C programmingC programming
C programming
 
Introduction to Basic C programming 01
Introduction to Basic C programming 01Introduction to Basic C programming 01
Introduction to Basic C programming 01
 
Lecture 1
Lecture 1Lecture 1
Lecture 1
 
Lecture 1
Lecture 1Lecture 1
Lecture 1
 
Input-output
Input-outputInput-output
Input-output
 
Fundamentals of prog. by rubferd medina
Fundamentals of prog. by rubferd medinaFundamentals of prog. by rubferd medina
Fundamentals of prog. by rubferd medina
 
Programming fundamental 02
Programming fundamental 02Programming fundamental 02
Programming fundamental 02
 
PHP Reviewer
PHP ReviewerPHP Reviewer
PHP Reviewer
 

Recently uploaded

GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machinePadma Pradeep
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitecturePixlogix Infotech
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersThousandEyes
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksSoftradix Technologies
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhisoniya singh
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Alan Dix
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationSafe Software
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...HostedbyConfluent
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Paola De la Torre
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Patryk Bandurski
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...shyamraj55
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 

Recently uploaded (20)

GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC Architecture
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other Frameworks
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 

C Programming Basics: Variables, Data Types, and Printf

  • 1.
  • 2. Before I explain any int main() { code to you, I int i; would like you to int sum = 0; execute your first for(i = 0; i<10; i++){ sum += i; program by } copying and printf("Sum = %dn", sum); pasting my code. //system("pause"); return 0; }
  • 3.  Locate the compile button and run buttons in your program.  You may alternatively find the compile and run button that executes both at once.  You should notice a black box appearing and disappearing very quickly. That is because I have allowed this code to execute without any pause statements.
  • 4. Programs typically automatically close once the final line of code is reached.  The line system(“pause”); needs to be added to “pause” the program in place  The system pause should be placed right before the return 0; line of any program. Return 0 is typically used at the end of a program to tell the computer the program has ended.
  • 5. Code is normally presented omitting the system("pause"); statement which confuses new programmers. Always make a mental note to add it in if you are testing new code.
  • 6.  When you place // before a line of code, you comment the code out and cause the compiler to skip over the code.  Now you can remove the // before the system pause line in the example program and run the program again to see output. If all is correct, you should see the output being Sum = 45.
  • 7.
  • 8. I want to draw your attention to the first line: int main(){  This is the beginning of your main function. The function ends with the use of }.  Any time you open a { bracket, you must close it with a } bracket.  As you can see with the main function, you can see the entire main function enclosed within the { } brackets.  Additional code is allowed within the brackets. This includes code that uses another set of { } as well.  The last { bracket to be opened is the first one to be closed when a } bracket is used.
  • 9.  The main function is the starting point of a program and where a compiler first starts to execute your code.  It is required in order to compile your code, therefore it would be a good practice to use the following template.
  • 10. int main(){ //your new written code goes here system("pause"); return 0; }
  • 11. Our program is going to need a way to hold our values  These values are stored in different types of variables.  Integers, floats, and doubles are the main “number” holders of a program.  A variable in C programming must always be declared at the top of the program right under int main() { if you want to use the variable in your program.  Unlike some programming languages, you may not locally declare a variable anywhere.
  • 12.  An integer is a number only representable in a whole number form.  Integers: 1,2,3,4,5..  Not Integers: 1.5, 1.7, 2.5, 3.9, 1003.4...  Integer math is the calculation between two integers. The math is calculated as you would normally between any two numbers except the decimal value is dropped. Ex. 1+3=4, 1+.5=1 , 5/4 = 1
  • 13. Double and Float variables allow the use of decimal numbers. A float has a smaller default precision than a double.  If you declare a float and then try to store the number within the float into a double, you may be transferring only part of the number.  A float allows at most about 7 decimal places precision.  A double allows at least 7 places.  To accurately work beyond 7 decimals you may require addition resources.
  • 14.  I typically use only Doubles when working with decimals and will work with only Doubles within these tutorials.  The math calculated with a double or float does not use integer math. It would be safe to assume the calculated answer would be similar to the answer given in a math class.
  • 15.  An integer is declared by typing int variablename;  A float is declared by typing float variablename;  A double is declared by typing double variablename;  The variablename is changeable to anything starting with a letter and anything that has not been assigned to a variable yet.
  • 16.  Multiple variables of the same type are declarable by adding a comma after the first variable and then typing the second.  int one, two, three;
  • 17.  The point of a variable is to eventually hold data.  The variable could be initialized immediately with a value.  int one = 1;  double duck = 12.5;
  • 18. To print out the value a variable is holding you use the printf statement  printf(“The variable one = %d”, one);  As shown above, the text you want printed out is held within the quotation marks. A %variabletype is added to signal to the computer you want to print out a variable.  int -> %d float -> %fl double -> %lf  After the comma, you list the variables in order that match up with the %variables
  • 19.  A double could be printed out but it may contain too many decimal places.  Limit a double by using %.#lf for the number of decimal places you want to be printed out.  The key “n” to a computer is interpreted as a line break.  When used in a printf statement, it will add a line break into the output.
  • 20. int main(){ int one = 5.5; float two = 7.5; double three = 10.5; printf("One equals: %d n", one); printf("Two equals: %f n", two); printf("Three equals: %lf n ", three); three = 15; printf("Three equals: %lf ", three); system("pause"); return 0; }
  • 21. Programming Template  Integers, Floats, and Doubles  Printf statements