BrainDump
  Home arrow BrainDump arrow Page 3 - C: For Beginners
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Silverlight  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
ASP Web Hosting  
ASP.NET Web Hosting 
Windows Web Hosting
 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
BRAINDUMP

C: For Beginners
By: James Payne
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 3 stars3 stars3 stars3 stars3 stars / 6
    2008-01-28

    Table of Contents:
  • C: For Beginners
  • Insert Obligatory First Program Here
  • Working with Numbers
  • Putting on the Puts()

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    C: For Beginners - Working with Numbers


    (Page 3 of 4 )

    The printf function described above is used to print text or strings to the screen. If we typed in the code:


    #include <stdio.h>

    int main (void)

    {

    printf("2+2= ");

    return 0;

    }

    It would simply print:

      2+2=

    and not solve the math. To get the program to perform our math for us, we would write the following:


    #include <stdio.h>

    int main (void)

    {

    printf("2+2= %d", 2+2);

    return 0;

    }

    The result:

      2+2= 4

    The way this code works is it creates a placeholder (in this instance %d) that will hold a value, which we defined after the comma (,): 2+2 (or 4). There are many placeholders (or format specifiers) in C, and we will discuss them more later on. But for now, here is a basic set of them in an awe-inspiring table:


    Placeholder

    What it Holds

    %i and %d

    Integer

    %f

    Float

    %If

    Double

    %s

    String

    %x

    Hexadecimal

    %c

    Character

    We will discuss the various data types in a different article. I just wanted to show you these here to give you an idea of how placeholders operate. An example of another way to use a placeholder is to define where your text will appear. Suppose I wanted to print some text to the left and then some text to the right. I could do so this way:



    #include <stdio.h>

    int main (void)

    {

    printf("%40s","Hey! Where are you?!?n");

    printf("%-40s","I'm over here!");

    printf("%400s","Oh! Now I see you!");

    return 0;

    }

    The %40s tells the program to move the text 40 spaces to the right. The %-40s moves the text to the left. And the %400s moves the text 400 spaces to the right; however, there aren't 400 spaces on a line, so it moves it down a few more lines until four hundred spaces have been entered, then it prints the text. Here is the result:

                                             Hey! Where are you?!?

      I'm over here!




                                                           Oh! Now I see you!


    More BrainDump Articles
    More By James Payne


       · Thanks for dropping by to read my article on Beginning program in C, where we cover...
     

    BRAINDUMP ARTICLES

    - Introduction to Office Live Workspace
    - Using MS Excel for One-way Analysis of Varia...
    - Comparing Data Sets Using Statistical Analys...
    - Import Blogger Posts into WordPress Using Wi...
    - Download WordPress from an FTP Server and Ru...
    - Install and Run WordPress in XAMPP Local Host
    - What Windows 7 Brings to the Table
    - Virtualization and Sandbox Detection
    - Advanced Firebug Techniques in Windows XP Ho...
    - Editing CSS with Firebug in Windows XP Home
    - Using Firebug in Windows XP Home
    - Migrating to Exchange Server 2007
    - Using System Restore on a Non-Bootable PC
    - Finding Logged on Users and More Scripting S...
    - Developing Macro Commands in MS Excel





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 6 Hosted by Hostway
    Stay green...Green IT