Docsity

C Programming Fundamentals: Introduction to Variables, Data Types, and Input/Output, Slides of Programming for Engineers

Partial preview of the text

Download C Programming Fundamentals: Introduction to Variables, Data Types, and Input/Output and more Slides Programming for Engineers in PDF only on Docsity!

Programming Fundamentals for Engineers

1. Introduction and C

Fundamentals

Muntaser Abulafi

Dr. Labib Arafeh

Yacoub Sabatin

  1. Pseudo-code:
    1. An outline of a program or algorithm, written in a form that can easily be converted into real programming statements;
    2. E.g. the pseudo-code for a bubble sort routine might be written: while not at end of list compare adjacent elements if second is greater than first switch them get next two elements if elements were switched repeat for entire list
  1. Code the program -- write the program;
  2. Test the program --run it;
  3. Debug the program (fix errors if any);
  4. Document completed program (final pseudo-code, flowchart, print chart, etc.). 2. Flow Charts:
  5. A flow chart is defined as a pictorial representation describing a procedure;
  6. Provides people (Developers, programmers , etc.) with a common language or symbolic reference point; 4 (Use: MS Visio)

5

3. An algorithm is a formula or set of steps for solving a particular problem;

  1. A clear set of rules; and
  2. Have a clear stopping point.
  3. Algorithms can be expressed in any language, from natural languages like English or French or Hindi to programming languages like QBASIC, C, etc. 4. Example : Add Two numbers, display both numbers and the sum: **1. START
  4. Input number1, number
  5. Sum = number1 + number
  6. Display heading message “Number1, Number2, Sum”
  7. Display number1, number2, Sum
  8. End**

5. Pseudo-code: An outline of a program or algorithm, written in a form that can easily be converted into real programming statements; 1. Example : The pseudo-code to add two numbers might be written: Start Input number1, number Sum = number1 + number Display heading message “Number1, Number2, Sum” Display number1, number2, Sum End 2. Can not be compiled nor executed, and there are no real formatting or syntax rules. 6. Caution: Never write your program before either Algorithming, flowcharting or pseudo-coding it. 7

Develop a C program to Compute the Volume and Dimensional weight of a box?

1. Analysis:

1) Outputs: Box_Volume, Box_D_Weight;

2)Inputs:

Height, length, width;

3) Processing:

1) Box_Volume = height * length * width;

2) Box_D_Weight = (Box_volume + 165)/166;

/* A C program to compute the Volume & Dimensional weight Dimensional weight of a box */ _/ STEP 1: START PART /_

#include <stdio.h>

main()

int height, length, width, Box_Volume,

Box_D_Weight; /declaring variables/

_/ STEP 2: Reading Data/_**

printf("Enter height of box: ");

scanf("%d", &height);

printf("Enter length of box: ");

scanf("%d", &length);

10

printf("Enter width of box: ");

scanf("%d", &width);

*/ * STEP 3: Computation */*

Box_Volume = height * length * width;

Box_D_Weight = (Box_Volume + 165) / 166;

_/ step 4 OUTPUT/_**

printf ("Dimensions: %d x %d x %d \n", length,

width, height);

printf ("Volume (cubic inches): %d \n",

Box_Volume); /* printing expression) */

printf ("Dimensional weight (pounds): %d \n",

Box_D_Weight);

_/ STEP 5 END/_**

return 0;

Benefits of learning C

  • (^) You will be able to read and write code for a large number of platforms -- everything from microcontrollers to the most advanced scientific systems can be written in C, and many modern operating systems are written in C.
  • (^) The jump to the object oriented C++ language becomes much easier. C++ is an extension of C, and it is nearly impossible to learn C++ without learning C first.

C History

  • (^) Developed between 1969 and 1973 along with Unix
  • (^) Due mostly to Dennis Ritchie
  • (^) Designed for systems programming
    • (^) Operating systems
    • (^) Utility programs
    • (^) Compilers
    • (^) Filters

‘Hello World’ in C #include <stdio.h> main() { printf(“Hello, world!\n”); return 0; } #include C preprocessor command that lets user includes the code from another file (such as a library) stdio.h standard input/output library #include <stdio.h> allows you to use code from the standard input/output library that contains printf function

  • (^) main Every program must have a function named main which contains statements enclosed in braces { and }
  • (^) printf A function from the standard I/O library that prints a string (f stands for formatted)
  • (^) \n A new line character. The \n inside the string indicates a new line
  • (^) Every function must return a value. Main is a function and returns a value taken by the operating system as the program terminates
  • (^) return 0 Used to signal OS that the program terminated normally

General C program! # directives main() { statements }

  • (^) Statements Commands that are carried out as program runs; - (^) printf is a call statement displaying string on screen : - (^) _printf (“hello”); / These are comments /_ - (^) _printf (“world! \n”); / Can go any where in a C /_
  • Statements end with a semicolon ( ; )

Some definitions

“Hello, World!\n”

  • (^) Character  A single letter, number, punctuation mark, etc.
  • (^) String  Series of characters grouped together;
  • (^) Some special kinds of characters exist:
    • (^) \t – tab character;
    • (^) \n – new line character.