C Programming Basics - Intro to Computer Programming - Lecture Slides, Slides of Computer Engineering and Programming

The key points in these lecture slides of intro to computer programming are given as:C Programming Basics, Subset of C , Keywords in C and C , Names of Variables, Keyword List, Program Structure in C, Comment Statements, Pre-Processor Directives, Example Declarations, Data Types

Typology: Slides

2012/2013

Uploaded on 05/06/2013

apsara
apsara 🇮🇳

4.5

(2)

86 documents

1 / 32

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
C Programming Basics
Lecture 5
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20

Partial preview of the text

Download C Programming Basics - Intro to Computer Programming - Lecture Slides and more Slides Computer Engineering and Programming in PDF only on Docsity!

C Programming Basics

Lecture 5

C Program Basics

  • C vs. C++
    • C is a subset of C++. All of features in C are contained in C++
    • C++ adds more libraries with functions for object oriented programming
    • C++ also adds more keywords and some added features.

Some Keywords in C and C++

switch template this throw try typedef union unsigned virtual void volatile while

new operator private protected public register return short signed sizeof static struct

double else enum extern float for friend goto if inline int long

asm auto break case catch char class const continue default delete do

Program Structure in C

  • EACH complete C program is composed of:
    • Comment statements
    • Pre-processor directives
    • Declaration statements
    • One or more functions
    • Executable statements

Comment Statements

  • Formal Comments: /* Comment ….. */
  • Used for detailed description of functions or operations (for our benefit, not compiler’s).
  • Can take multiple lines in source file.
  • Informal Comments (only in C++, not C): // Comment ….. Ends at the end of line
  • Used for quick comments like: int temp; // temporary variable for storing // the input value

Pre-Processor Directives

#include -- header files for library functions

Example: #include <stdio.h>

#define -- define constants and macros Examples: #define e 2. #define pi 3.

Note Space

Note Spaces

Data Types

  • Integer variables: int a, b ;
  • Integer variables, like a or b, store only whole numbers like 3 or 7, not 3.33 or 7.65, and only up to certain maximum values.
  • Floating point variables: float c, d ;
  • Floating point variables, like c or d, store rational numbers, like 3.14159, but only a limited number of digits of precision.

Internal Storage Representation

  • Definitions:
    • Binary digit -- or a "bit", is either a 0 or a 1
    • Byte -- usually a collection of 8 bits together
    • Word -- often a collection of 4 bytes together
  • On the SGI Unix system:
    • an "int" data type takes up 4 bytes (on some systems, an "int" is only 2 bytes)
    • a "float" data type takes up 4 bytes
    • a "double" data type take up 8 bytes
    • a "char" data type takes up 1 byte

Executable Statements

  • Simple Declaring variables int temp ; char a ; Assigning Values temp = 5 ; temp is assigned the value of 5
  • Complex, i.e., Calling Functions

plotxy (x, y) ;

  • Calculations

x = (5. / 2 + 6) * 7 ;

Arithmetic Operators

  • multiply + add / divide - subtract

% remainder, where:

x = 13 % 5 ; /* x will be equal to 3 */

  • An expression can be used almost anywhere a variable of the same type can be used. Ex. expressions: num + 3, a * d - 5, ...

Arithmetic Operators – Order of

Evaluation

For example:

x = 2 * 3 - (4 + 5) + 8 % 7;

x = 2 * 3 - 9 + 8 % 7;

x = 6 - 9 + 8 % 7;

x = 6 - 9 + 1;

x = -3 + 1;

x = -2; Docsity.com

Arithmetic Operators – Order of

Evaluation

Another example:

x = x = (^) 33; 6 / 2 + 1 - 3 + 8 * 4;

x = 6 / (2 + 1) - (3 + 8) * 4;

x = -42;

Assignment Operators

Operator: Example: Meaning:

= x = 5 ; x = 5 ; += x += 5 ; x = x + 5 ; –= x –= 5 ; x = x – 5 ; /= x /= 5 ; x = x / 5 ; *= x *= 5 ; x = x * 5 ; %= x %= 5; x = x % 5 ;

Assignment Operators

Example of assignment operators:

int a = 4, b = 2, c = 36 ; a += b ; /* This adds b to a, a =? */

c /= a + b ; /* What is value of c now? */