1 / 19

The First C Program

/* This program prints Hello World! to screen */ #include &lt;stdio.h&gt; int main() { printf(“Hello World!<br>”); return 0; }. The First C Program. Comments are ignored by the compiler. Preprocessor directive stdio.h is the header file containing I/O function declarations.

chaim
Download Presentation

The First C Program

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. /* This program prints Hello World! to screen */ #include <stdio.h> int main() { printf(“Hello World!\n”); return 0; } The First C Program Comments are ignored by the compiler Preprocessor directive stdio.h is the header file containing I/O function declarations Each C program must have one main function Prints Hello World! and ‘\n’ advances the cursor to a newline Each C statement ends with a ‘;’ Problem Solving Using C

  2. A Sequence Structure Example • Write a program to calculate the tax paid by an employee • Pseudocode: • print “A program that computes income tax” • print “Enter gross income:” • read gross_income • tax = 0.0175 gross_income • print “Tax is ” tax “dollars.” Problem Solving Using C

  3. A Sequence Structure Example /********************************************************** Author : Uckan Purpose : Computes tax as 1.75% of gross income. Input from : Terminal keyboard Output to : Screen **********************************************************/ #include <stdio.h> const double TAX_RATE = 0.0175; int main(void) { /* Variable declarations: */ double gross_income; double tax; /* Function body: */ printf("A PROGRAM THAT COMPUTES INCOME TAX\n"); printf("Enter gross income: "); scanf("%lf", &gross_income); tax = TAX_RATE * gross_income; printf(“Tax is %lf dollars.", tax); return 0; } Problem Solving Using C

  4. Dissection of the Program • printf(): Function for printing text, values, etc. • scanf(): Function for reading a value. It will wait for the user to enter a value • tax = TAX_RATE * gross_income; • * Multiplication operator • = Assignment operator • We will explain the purpose of %lf in printf() and scanf() a little later! Problem Solving Using C

  5. C has 6 Kinds of Tokens of Language Elements • Reserved words: const, double, int, return, if, while, … • Identifiers: Programmer defined words for variables and functions. Identifiers must be unique • Constants: Fixed values • String literals: Sequence of characters • Punctuators: [ ] ( ) { } , ; : … * # • Operators: Tokens that result in some kind of operation. Operators act on operands • Unary operators: Require one operand: e.g. Minus: -15 • Binary operators: Require two operands: e.g. Multiplication: 25*4 Problem Solving Using C

  6. Rules for Constructing Identifiers • Identifiers can consist of capital letters, lower case letters, digits, and underscore character ‘_’ • Valid: myVariable, my_variable, _tax, income4 • Invalid: myVariable+yourVariable, oh!no • First character must be a letter or an underscore • Invalid: 2grade • In general, there is no length limitation • No embedded blanks are allowed • Invalid: my grade • Reserved words cannot be used as identifiers • Identifiers are case sensitive • MYGRADE and mygrade are different in C! Problem Solving Using C

  7. Data Types and Declarations • Declaration: Associating a type with a program entity such as a variable. • Data type: A set of data values and a set of operations on those values. • Named constants • const double TAX_RATE=0.0175; • Value of TAX_RATE does not change throughout the program • C has two classes of data types • Built-in (we’ll learn these in CMPE 101) • Programmer defined (see those in CMPE 102) Problem Solving Using C

  8. Fundamental Data Types in C • int: Integer: e.g. 1244, -87 • If 2 bytes are used to store integers, range of integers is from -215 to 215-1 (or -32,768 to 32,767) (Q: What does 1st bit do?) • char: Character: ‘a’, ‘A’, ‘4’ • double: Floating-point: 0.5678, -3.25, 2.0e4, 2.3e-1 • The scientific notation: 2.0e4=2.0104,2.3e-1=2.3 10-1) • float: Floating-point (less precision) • 8 is an integer, 8.0 is a double, 8.0F is a float • void: “Nothing” Note that some of these data types are preceded by short, long, signed, and unsigned: e.g. short int Problem Solving Using C

  9. Derived Data Types • Strings: A sequence of characters • e.g.: “This is a string” • Note double quotation marks “ ” • “a” is a string, ‘a’ is a character • String variables are declared as follows: char student_name[31]; • The string length must not exceed 30 characters since the string must be terminated by the null character ‘\0’ Problem Solving Using C

  10. Input of String Variables • We use the function gets(StringVariable): Get string • Program segment: char student_name[31]; gets(student_name); • gets() waits for the user to enter a string. The string’s length must not exceed the string variable’s declared length (in this case 30) Problem Solving Using C

  11. C Statements • A statement is a specification of an action to be taken as the program executes • Each C statement ends with a semi-colon ; • A compound statement is a list of statements enclosed in curly braces { } • Some types of statements: • Expressions, selections, repetitions, etc. Problem Solving Using C

  12. Data Initialization • char ch; • Just a declaration for the variable ch • char ch=‘a’; • Compile-time initialization of variable ch • ch=‘a’; • Run-time initialization of variable ch Problem Solving Using C

  13. Assignments and Expressions • Expression: Syntactically correct and meaningful combination of operators and operands: • TAX_RATE * gross_income • Assignment operator: = • tax = TAX_RATE * gross_income; • tax and TAX_RATE * gross_income are operands • TAX_RATE * gross_income is also an expression • Variable = Arithmetic Expression • Computes the value for the expression and stores the result in memory cell for the variable • Q: How does the memory cell containing the value of current year change as the following statements are executed? int current_year = 2005; current_year = 2006; Problem Solving Using C

  14. Standard Output Function • printf() • printf() is in fact a call to a standard library function • We’ll look at function calls in detail later • To useit,you will need to include stdio.h header file by typing #include <stdio.h> at the beginning of program • There are two syntactical forms of printf(): • printf(FormatControlString); • e.g. printf(“Hello\n”); • printf(FormatControlString, PrintList); • e.g. int year=2006; printf(“Year is %d”, year); Problem Solving Using C

  15. Format Specifiers for printf() • int: %d • float: %f • double: %lf • char: %c • string: %s double height=1.80; int year=2006; printf(“My height is %lf in year %d.\n”, height, year); Problem Solving Using C

  16. Standard Input Function • scanf() • scanf() is in fact a call to a standard library function • It is used for interactive input • To useit,you will need to include stdio.h header file by typing #include <stdio.h> at the beginning of program • Syntactical forms of scanf(): • scanf(FormatControlString, InputList); • e.g. int age; printf(“%d”, &age); • FormatControlString must consist of format specifiers only Problem Solving Using C

  17. More on scanf() • Each element in InputList in scanf() call must be an address to a memory location • If the element is not an address, it must be made into an address by prefixing the variable name by an ampersand character & • & is called the address operator in C int age; scanf(“%d”, &age); Problem Solving Using C

  18. Format Specifiers for scanf() • int: %d • float: %f • double: %lf • char: %c double height; int year; scanf(“%lf”, &height); scanf(“%d”, &year); scanf(“%lf”, height); /* This is an error!! */ Problem Solving Using C

  19. A Conversion Program in C /************************************************************* Purpose: Converts height from inches to centimeters, and weight from pounds to kilograms *************************************************************/ #include <stdio.h> int main(void) { /* Variable declarations: */ int height_in_inches, weight_in_pounds; double height_in_centimeters, weight_in_kilograms; /* Function body: */ printf("Enter your height in \"inches\": "); scanf("%d", &height_in_inches); height_in_centimeters = 2.54 * height_in_inches; printf("Enter your weight in \"pounds\": "); scanf("%d", &weight_in_pounds); weight_in_kilograms = 0.45359 * weight_in_pounds; printf("your height is %lf centimeters, ", height_in_centimeters); printf("\nand your weight is %lf kilograms.", weight_in_kilograms); return 0; } Problem Solving Using C

More Related