Docsity

C and Cpp-Programming-Lecture Slides, Slides of Computer Programming

Partial preview of the text

Download C and Cpp-Programming-Lecture Slides and more Slides Computer Programming in PDF only on Docsity!

C and C++

^ Traditionally C programs use the fileextension .C and C++ programs theextension .CPP ^ C is essentially a subset of C++, so youcould use a C++ compiler to run a Cprogram. The two languages areextremely similar. ^ In the labs we will be using a

Microsoft

compiler called Microsoft Visual Studio.

Some programmer jargon  Some words that will be used a lot:  Source code: The stuff you type into thecomputer. The program you are writing.  Compile (build): Taking source code andmaking a program that the computer canunderstand.  Executable: The compiled program that thecomputer can run.  Library: Added functions for Cprogramming to do certain tasks.  Header file: Files ending in .h which areincluded at the start of source code.

Basic Structure of a C Program output: Hello World!

C Program:^ #include <stdio.h>int main(){ printf(“Hello World!”);return 0;}

Example: Hello World

Basic Structure of a C Program

#include

<stdio.h> int^ main(){ printf(“Hello

World!”); return

0; C Program }

:

Example: Hello world Includes standardinput/output library ofprocedures. Read: “Hash-include”

Basic Structure of a C Program

C Program:^ #include <stdio.h>int main(){ printf(“Hello World”);return 0;}

Curly braces mark the beginning

and^ end

of a

Example: block of instructions.

Hello World

Basic Structure of a C Program

C Program:^ #include <stdio.h>int main(){ printf(“Hello World”);return 0;}

Instruction (

function call

)

Example: to output “Hello World”

Hello World

Printf

^ Writes on screen text in quotes ^ Quotes around string ^ Enclosed in parenthesis ^ Ends in semicolon

Control characters

^ \n – new line ^ \t - horizontal tab ^ \r - carriage return ^ \a - sound bell

Library functions

^ printf is a library function printf("SS-CARE \n School

of \n Engineering");

Output

SS-CARESchool

of Engineering

Variables

^ Variables, can be declared anywhere in theprogram. ^ However they are only visible to the programwithin the block of code in which they aredefined.

int^ main()

{ int^ x^

=^ 4; if^ (x>3){

int^ y^

=^ 3; } return

(0) }

Values and Variables

^ Basic Types:^ ^ Integers (signed and unsigned)

^ short ^ int  Floating point numbers ^ float ^ double  Characters (signed and unsigned)  Boolean

Basic Types:

char

^ Characters (

char

’a’ ’z’ ’A’ ’Z’ ’?’ ’@’ ’0’ ’9’

Variables

^ Name & type must be declared beforeuse ^ Allows for error checking ^ Explicit declaration