C/C++ Basics. Basic Concepts Basic functions of each language: Input, output, math, decision, repetition Types of errors: Syntax errors, logic errors,

Slides:



Advertisements
Similar presentations
C++ Language Fundamentals. 2 Contents 1. Introduction to C++ 2. Basic syntax rules 3. Declaring and using variables.
Advertisements

Overview of programming in C C is a fast, efficient, flexible programming language Paradigm: C is procedural (like Fortran, Pascal), not object oriented.
C Language.
IT 325 OPERATING SYSTEM C programming language. Why use C instead of Java Intermediate-level language:  Low-level features like bit operations  High-level.
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
C Intro.
Advanced Java Concepts Dr. Jeyakesavan Veerasamy
Kernighan/Ritchie: Kelley/Pohl:
CS100A, Fall 1997, Lectures 221 CS100A, Fall 1997 Lecture 22, Tuesday 18 November Introduction To C Goal: Acquire a reading knowledge of basic C. Concepts:
C Slides and captured lecture (video and sound) are available at:
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science Computer Systems Principles C/C++ Emery Berger and Mark Corner University of Massachusetts.
C++ vs. Java: Similiarities & Differences Dr. Jeyakesavan Veerasamy Director of CS UTDesign program & CS Teaching Faculty University.
Guide To UNIX Using Linux Third Edition
Functions in C. Function Terminology Identifier scope Function declaration, definition, and use Parameters and arguments Parameter order, number, and.
Computer Science 210 Computer Organization Introduction to C.
Programming Languages and Paradigms Object-Oriented Programming.
Intro to C++ And Some Tools Opening Discussion zHave any questions come up since last class? Have you had a chance to look over the project.
By Sidhant Garg.  C was developed between by Dennis Ritchie at Bell Laboratories for use with the Unix Operating System.  Unlike previously.
By Noorez Kassam Welcome to JNI. Why use JNI ? 1. You already have significantly large and tricky code written in another language and you would rather.
C Tutorial Session #2 Type conversions More on looping Common errors Control statements Pointers and Arrays C Pre-processor Makefile Debugging.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 12: Pointers continued, C strings.
Dynamic Memory Allocation The process of allocating memory at run time is known as dynamic memory allocation. C does not Inherently have this facility,
Netprog: Java Intro1 Crash Course in Java. Netprog: Java Intro2 Why Java? Network Programming in Java is very different than in C/C++ –much more language.
Game Programming in Java Dr. Jeyakesavan Veerasamy CS faculty, The University of Texas at Dallas Website:
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 13 Introduction to Classes.
Week 14 - Monday.  What did we talk about last time?  Introduction to C++  Input and output  Functions  Overloadable  Default parameters  Pass.
C Programming in Linux Jacob Chan. C/C++ and Java  Portable  Code written in one system and works in another  But in C, there are some libraries that.
Current Assignments Start Reading Chapter 6 Project 3 – Due Thursday, July 24 Contact List Program Homework 6 – Due Sunday, July 20 First part easy true/false.
Copyright © Curt Hill Structured Data What this course is about.
Pointers: Basics. 2 What is a pointer? First of all, it is a variable, just like other variables you studied  So it has type, storage etc. Difference:
C++ Programming Basic Learning Prepared By The Smartpath Information systems
RUN-Time Organization Compiler phase— Before writing a code generator, we must decide how to marshal the resources of the target machine (instructions,
Introduction As programmers, we don’t want to have to implement functions for every possible task we encounter. The Standard C library contains functions.
Introduction to C For the participants of Practical Course „Scientific Computing and Visualization“
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
Topics memory alignment and structures typedef for struct names bitwise & for viewing bits malloc and free (dynamic storage in C) new and delete (dynamic.
Computer Graphics 3 Lecture 1: Introduction to C/C++ Programming Benjamin Mora 1 University of Wales Swansea Pr. Min Chen Dr. Benjamin Mora.
COP 3530 Spring2012 Data Structures & Algorithms Discussion Session Week 2.
C LANGUAGE Characteristics of C · Small size
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
Classes, Arrays & Pointers. Compiler & Linker expectations file1.cppfile2.cppfilen.cpp …. file1.ofile2.ofilen.o …. Linker application (executable) Compiler.
Chapter 1 C++ Basics Review (Section 1.4). Classes Defines the organization of a data user-defined type. Members can be  Data  Functions/Methods Information.
Java & C++ Comparisons How important are classes and objects?? What mechanisms exist for input and output?? Are references and pointers the same thing??
Announcements Assignment 1 due Wednesday at 11:59PM Quiz 1 on Thursday 1.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 11: Pointers.
C is a high level language (HLL)
LECTURE 3 Translation. PROCESS MEMORY There are four general areas of memory in a process. The text area contains the instructions for the application.
Pointers: Basics. 2 Address vs. Value Each memory cell has an address associated with it
Lecture 3 Translation.
Computers’ Basic Organization
Computer Organization and Design Pointers, Arrays and Strings in C
‘C’ Programming Structures and Commands
Winter 2009 Tutorial #6 Arrays Part 2, Structures, Debugger
Computer Science 210 Computer Organization
BRIEF Overview ON THE MAJOR Similarities & Differences
Computer Science 210 Computer Organization
Programming Languages and Paradigms
C Short Overview Lembit Jürimägi.
Programming Paradigms
Computer Science 210 Computer Organization
Computer Science 210 Computer Organization
Computer Organization & Compilation Process
Chapter 11 Introduction to Programming in C
C/C++ Basics.
BRIEF Overview ON THE MAJOR Similarities & Differences
Introduction to C EECS May 2019.
Programming Languages and Paradigms
C Characters and Strings
SPL – PS1 Introduction to C++.
Introduction to C CS 3410.
Presentation transcript:

C/C++ Basics

Basic Concepts Basic functions of each language: Input, output, math, decision, repetition Types of errors: Syntax errors, logic errors, runtime errors. Debugging Machine language, assembly language, high level languages

1-3 Procedural Programming Procedure A Data Element Procedure B

Procedural languages … Global variables Primitive data types, Arrays, Records/Structures lots of functions/procedures/modules Uniqueness of “names” requirement Struct + protection + methods ~= class (OOP)

SW Complexity Size of the application – Lines of Code SW Complexity reality

SW complexity? Lots of lines in code – hard to understand Not many meaningful comments Too many variables – not named well Complex functions – too nested, too lengthy, too many if conditions Inter-dependancies – changes that have to be done together in multiple files When you fix a bug, you make a few more.

SW Complexity Size of the application – Lines of Code SW Complexity ideal

1-8 Object-Oriented Programming Object Attributes (data) typically private to this object Methods (behaviors / procedures) Other objects Programming Interface

C vs. C++ C++ is backward compatible with C C++ supports OOP C++ standard library contains lot more functionality Improved I/O mechanism Lot of new header files C is very efficient, C++ is close too. …

C++ vs. Java: Similarities Both support OOP. Most OOP library contents are similar, however Java continues to grow. Syntax is very close – Java has strong influence of C/C++. Easy to learn the other language when you know one of these.

C++ vs. Java: differences C++Java Write once, compile everywhere  unique executable for each target Write once, run anywhere  same class files will run above all target-specific JREs. No strict relationship between class names and filenames. Typically, a header file and implementation file are used for each class. Strict relationship is enforced, e.g. source code for class PayRoll has to be in PayRoll.java I/O statements use cin and cout, e.g. cin >> x; cout << y; I/O input mechanism is bit more complex, since default mechanism reads one byte at a time (System.in). Output is easy, e.g. System.out.println(x); Pointers, References, and pass by value are supported. No array bound checking. Primitive data types always passed by value. Objects are passed by reference. Array bounds are always checked. Explicit memory management. Supports destructors. Automatic Garbage Collection. Supports operator overloading.Specifically operator overloading was left out.

C standard library stdio.h: scanf(), printf(), getchar(), putchar(), gets(), puts(), … stdlib.h: atof(), atoi(), rand(), srand(), malloc(), free(), … math.h: sin(), cos(), sqrt(), … string.h: strcpy(), strcmp(), … ctype: isdigit(), isalpha(), tolower(), toupper() …

Sample C programs: prog1.c These are in ~veerasam/students/basics #include main() { int in1, out; double in2; puts("Enter values:"); scanf("%d%lf", &in1, &in2); // & means "address of" // %d int, %f float, %lf double out = in1 + in2; printf("Output is %d\n", out); }

Sample C programs: prog2.c #include main() { char line[10]; int out; gets(line); puts(line); out = 2 * atoi(line); printf("%d\n", out); }

Sample C programs: prog3.c #include main() { srand(getpid()); printf("%d\n", rand()); }

Sample C programs: prog4.c #include int add(int x, int y) { return x + y; } main() { int in1, in2, out; puts("Enter values:"); scanf("%d%d", &in1, &in2); out = add(in1, in2); printf("Output is %d\n", out); }

Sample C programs: simple.c #include main() { int i, arr[10]; puts("Let me init the array contents."); for( i=0 ; i<20 ; i++) arr[i] = i; puts("Well, I survived!"); return 0; }

Sample C programs: simple.c #include main() { int i, arr[10]; puts("Let me init the array contents."); for( i=0 ; i<20 ; i++) arr[i] = i; puts("Well, I survived!"); return 0; }

Sample C programs: simple1.c #include int i, array[10], array2[10]; main() { puts("\nArray's contents are"); for( i=0 ; i<10 ; i++) printf("%d\n",array[i]); puts("\nArray2's contents are"); for( i=0 ; i<10 ; i++) printf("%d\n",array2[i]); puts("Let me init the array contents."); for( i=-10 ; i<20 ; i++) array[i] = i; puts("\nArray's contents are"); for( i=0 ; i<10 ; i++) printf("%d\n",array[i]); puts("\nArray2's contents are"); for( i=0 ; i<10 ; i++) printf("%d\n",array2[i]); puts("\nWell, I survived!"); return 0; }

Sample C programs: simple2.c #include int i, array[10], array2[10]; printArray(int *arr) { int i; for(i=0 ; i<10 ; i++) printf("%d\n",arr[i]); } main() { puts("\nArray's contents are"); printArray(array); puts("\nArray2's contents are"); printArray(array2); puts("Let me init array contents."); for( i=-10 ; i<20 ; i++) array[i] = i; puts("\nArray's contents are"); printArray(array); puts("\nArray2's contents are"); printArray(array2); puts("\nWell, I survived!"); return 0; }

Sample C programs: simple3.c #include printArray(int *arr) { int i; for(i=0 ; i<10 ; i++) printf("%d\n",arr[i]); } main() { int i, array[10], array2[10]; puts("\nArray's contents are"); printArray(array); puts("\nArray2's contents are"); printArray(array2); puts("Let me init array contents."); for( i=-10 ; i<20 ; i++) array[i] = i; puts("\nArray's contents are"); printArray(array); puts("\nArray2's contents are"); printArray(array2); puts("\nWell, I survived!"); return 0; }

Sample C programs: simple4.c #include printArray(int *arr) { int i; for(i=0 ; i<10 ; i++) printf("%d\n",arr[i]); } main() { second(); puts("\nWell, I survived!"); return 0; } second() { int i, array[10], array2[10]; puts("\nArray's contents are"); printArray(array); puts("\nArray2's contents are"); printArray(array2); puts("Let me init array contents."); for( i=-10 ; i<20 ; i++) array[i] = i; puts("\nArray's contents are"); printArray(array); puts("\nArray2's contents are"); printArray(array2); }

Things to observe Memory allocation for arrays Array length is not stored with arrays Potentional issues with scanf() and printf() Different behavior when overshooting arrays in heap vs. in stack

Sample C program: multiple files

Compare with Java … file1.java file2.java file3.javafilen.java

C files & runtime environment … file1.c file2.c file3.cfilen.c

Debugging : gdb Compile with –g option to use the debugger. Most used commands: run list [method name] break [line_number] step next continue print [variable]