C Programming Review CSC 4320/6320

Similar documents
CS 326 Operating Systems C Programming. Greg Benson Department of Computer Science University of San Francisco

C Programming SYLLABUS COVERAGE SYLLABUS IN DETAILS

Variables Data types Variable I/O. C introduction. Variables. Variables 1 / 14

Binghamton University. CS-211 Fall Syntax. What the Compiler needs to understand your program

Lecture 2: C Programming Basic

Review of the C Programming Language for Principles of Operating Systems

C Fundamentals & Formatted Input/Output. adopted from KNK C Programming : A Modern Approach

PERIYAR CENTENARY POLYTECHNIC COLLEGE Periyar Nagar- Vallam Thanjavur

Laboratory 2: Programming Basics and Variables. Lecture notes: 1. A quick review of hello_comment.c 2. Some useful information

CS201 - Introduction to Programming Glossary By

CSCI-243 Exam 1 Review February 22, 2015 Presented by the RIT Computer Science Community

Computer Science & Information Technology (CS) Rank under AIR 100. Examination Oriented Theory, Practice Set Key concepts, Analysis & Summary

C Language, Token, Keywords, Constant, variable

CS 261 Fall C Introduction. Variables, Memory Model, Pointers, and Debugging. Mike Lam, Professor

Short Notes of CS201

Ricardo Rocha. Department of Computer Science Faculty of Sciences University of Porto

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language

A Fast Review of C Essentials Part I

Review of the C Programming Language

Lectures 5-6: Introduction to C

Lectures 5-6: Introduction to C

PRINCIPLES OF OPERATING SYSTEMS

ET156 Introduction to C Programming

Procedures, Parameters, Values and Variables. Steven R. Bagley

CMPE-013/L. Introduction to C Programming

C: How to Program. Week /Mar/05

Programming Fundamentals (CS 302 ) Dr. Ihsan Ullah. Lecturer Department of Computer Science & IT University of Balochistan

UNIT-V. Structures. The general syntax of structure is given below: Struct <tagname> { datatype membername1; datatype membername2; };

C Language Part 1 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

CSE 374 Programming Concepts & Tools


C Introduction. Comparison w/ Java, Memory Model, and Pointers

Chapter 2 - Introduction to C Programming

AMCAT Automata Coding Sample Questions And Answers

INTRODUCTION 1 AND REVIEW

Motivation was to facilitate development of systems software, especially OS development.

C Programming. Course Outline. C Programming. Code: MBD101. Duration: 10 Hours. Prerequisites:

Introduction to the C Programming Language

Presented By : Gaurav Juneja

COP 3275: Chapter 02. Jonathan C.L. Liu, Ph.D. CISE Department University of Florida, USA

Basic Elements of C. Staff Incharge: S.Sasirekha

C Syntax Out: 15 September, 1995

PROGRAMMAZIONE I A.A. 2018/2019

6.096 Introduction to C++ January (IAP) 2009

PROGRAMMAZIONE I A.A. 2017/2018

BLM2031 Structured Programming. Zeyneb KURT

Fundamental Data Types. CSE 130: Introduction to Programming in C Stony Brook University

Chapter IV Introduction to C for Java programmers

COMPUTER APPLICATION

Data Type Fall 2014 Jinkyu Jeong

Computer Science & Engineering 150A Problem Solving Using Computers

Lecture 04 FUNCTIONS AND ARRAYS

CSCI 171 Chapter Outlines

Lab Exam 1 D [1 mark] Give an example of a sample input which would make the function

JTSK Programming in C II C-Lab II. Lecture 3 & 4

Arrays in C. Prof. Indranil Sen Gupta Dept. of Computer Science & Engg. Indian Institute of Technology Kharagpur. Basic Concept

A3-R3: PROGRAMMING AND PROBLEM SOLVING THROUGH 'C' LANGUAGE

Lecture 3: C Programm

#include <stdio.h> int main() { char s[] = Hsjodi, *p; for (p = s + 5; p >= s; p--) --*p; puts(s); return 0;

Introduction to C Programming. Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan

C OVERVIEW. C Overview. Goals speed portability allow access to features of the architecture speed

DECLARATIONS. Character Set, Keywords, Identifiers, Constants, Variables. Designed by Parul Khurana, LIECA.

C & Data Structures syllabus

C OVERVIEW BASIC C PROGRAM STRUCTURE. C Overview. Basic C Program Structure

C: Arrays, and strings. Department of Computer Science College of Engineering Boise State University. September 11, /16

Lecture 03 Bits, Bytes and Data Types

#include <stdio.h> int main() { printf ("hello class\n"); return 0; }

C LANGUAGE AND ITS DIFFERENT TYPES OF FUNCTIONS

Unit 7. Functions. Need of User Defined Functions

P.G.TRB - COMPUTER SCIENCE. c) data processing language d) none of the above

C library = Header files + Reserved words + main method

Syntax and Variables

EL6483: Brief Overview of C Programming Language

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #54. Organizing Code in multiple files

AN OVERVIEW OF C, PART 3. CSE 130: Introduction to Programming in C Stony Brook University

IV Unit Second Part STRUCTURES

IMPORTANT QUESTIONS IN C FOR THE INTERVIEW

C Arrays. Group of consecutive memory locations Same name and type. Array name + position number. Array elements are like normal variables

Variables in C. Variables in C. What Are Variables in C? CMSC 104, Fall 2012 John Y. Park

Motivation was to facilitate development of systems software, especially OS development.

Programming in C - Part 2

ELEC 377 C Programming Tutorial. ELEC Operating Systems

CS240: Programming in C

advanced data types (2) typedef. today advanced data types (3) enum. mon 23 sep 2002 defining your own types using typedef

Compiling and Running a C Program in Unix

Programming in C. What is C?... What is C?

Programming in C UVic SEng 265

Chapter 1 & 2 Introduction to C Language

Pointers and Arrays 1

Programming in C. What is C?... What is C?

Structures. Dr. Donald Davendra Ph.D. (Department of Computing Science, Structures FEI VSB-TU Ostrava)

Arrays. CS10001: Programming & Data Structures. Pallab Dasgupta Dept. of Computer Sc. & Engg., Indian Institute of Technology Kharagpur

COMP322 - Introduction to C++ Lecture 02 - Basics of C++

Kurt Schmidt. October 30, 2018

Advanced use of the C language

CS349/SE382 A1 C Programming Tutorial

C Language Part 2 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

QUIZ. 1. Explain the meaning of the angle brackets in the declaration of v below:

B.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University

Aryan College. Fundamental of C Programming. Unit I: Q1. What will be the value of the following expression? (2017) A + 9

Transcription:

C Programming Review CSC 4320/6320

Overview Introduction C program Structure Keywords & C Types Input & Output Arrays Functions Pointers Structures LinkedList Dynamic Memory Allocation Macro Compile & Run C program on Ubuntu Linux

Facts About C Structured Programming language Compiled on various platforms System programming language Examples of the use of C: OS- Unix Language Compilers Assemblers Network Drivers Databases Most State-of-the-art software Language interpreters Case-sensitive language. C- Basically collection of functions.

C Program Structure

/* Document section or comments */ C Program Structure contd.. #include <stdio.h> /* Link section (Preprocessor) */ int total = 0; /* Global declaration, definition section */ int sum (int, int); /* Function declaration section */ int main () /* Main function means start here */ { } printf ("This is a C basic program \n"); total = sum (1, 1); printf ("Sum of two numbers : %d \n", total); /* Library command */ return 0; int sum (int a, int b) /* User defined function */ { return a + b; /* definition section */ } Brackets define code blocks

Keywords & C Types Flow control if, else, return, switch, case, default Loops for, do, while, break, continue Common types int, float, double, char, void structures struct, typedef, union Counting and sizing things enum, sizeof Rare but still useful types extern, signed, unsigned, long, short, static, const

C Input & Output Read Input :scanf function E.g. scanf( %d, &x); Print output: printf function E.g. printf( %d, x); Both printf function and scanf function must be supplied with a format string as 1 st argument.

Scanf Example int i, j; float x, y; scanf("%d%d%f%f", &i, &j, &x, &y); Sample input: 1-20.3-4.0e3 scanf will assign 1, 20, 0.3, and 4000.0 to i, j, x, and y, respectively.

printf Example: int i, j; float x, y; i = 10; j = 20; x = 43.2892f; y = 5527.0f; printf("i = %d, j = %d, x = %f, y = %f\n", i, j, x, y+1); Output: i = 10, j = 20, x = 43.289200, y = 5528.000000

Declare Array: type arrayname [ arraysize ]; type array_name[size] = {a_list_of_value}; Arrays Example 1: Example 2 : double balance[] = {1000.0, 2.0, 3.4, 7.0, 50.0}; Example 3 : float matrixnum[20][25]; // 20 rows 25 columns Example 4 : char chvowel[6] = {'a', 'e', 'i', 'o', 'u', '\0'};

Arrays Note: C doesn t require subscript bounds be checked. No compile time error if subscript goes out of range.

Functions Types: 1.Standard library Function 2.User-defined function

Standard library Function String Standard functions

User-defined Functions

A pointer is a variable that holds memory address of another variable, where, the actual data is stored. type_of_stored_data * pointer_variable_name; Pointers char* chname; int * ntypeofcar; float *fvalue; 1. Indirection operator (*) : gain access of object that pointer points to 2. Address-of-operator (&): gets address of variable

Pointers As Arguments

Compare to Value as Arguments

Pointers as Return Values Returns location instead of value

Pointers pointing to arrays

Pointers to access Strings

A structure is a collection of related data items(members) stored in one place and can be referenced by more than one names. Structure variables can be initialized. The initialization for each variable must be enclosed in braces. Both structure types and variables follow the same scope as normal Structures variables, as do all identifiers. If you define a structure within a function, then you can only use it within that function. specifies a type members

Structures

Structures

LinkedList A linked list is a basic data structure. Linked list parts. Nodes make up linked lists. Nodes are structures made up of data and a pointer to another node. Usually the pointer is called next.

Creating a Node: Allocate space for a new node. Set the next pointer to the value of NULL Set the data value for the node. LinkedList Adding Node to list: If the start node is null then the start node becomes the new node. If start is not null then start becomes the new node s next ] and the start becomes the new node.

LinkedList- Deleting a Node Q

Dynamic Memory Allocation

Dynamic Memory Allocation

Macro: fragment of code given a name. When name is used, it is replaced by contents. MACRO Types: Object-like macros resemble data objects when used function-like macros resemble function calls.

Function Macro Compiled Preprocessed Differentiate Macro & Function translates into ASM call to pass parameters and return values Implementation not hidden Easy to debug C preprocessor translates strings using macro and compiles. Hides implementation Hard to debug large code.

An object-like macro is a simple identifier which will be replaced by a code fragment. It is called object-like because it looks like a data object in code that uses it. They are most commonly used to give symbolic names to numeric constants. Object-like macro (https://gcc.gnu.org/onlinedoc s/cpp/macros.html) You create macros with the #define directive. #define is followed by the name of the macro and then the token sequence it should be an abbreviation for, which is variously referred to as the macro's body, expansion or replacement list. For example, #define BUFFER_SIZE 1024 defines a macro named BUFFER_SIZE as an abbreviation for the token 1024. If somewhere after this #define directive there comes a C statement of the form foo = (char *) malloc (BUFFER_SIZE); then the C preprocessor will recognize and expand the macro BUFFER_SIZE. The C compiler will see the same tokens as it would if you had written foo = (char *) malloc (1024);

Functionlike macro (https://gcc.gnu.org/onlin edocs/cpp/macros.html) You can also define macros whose use looks like a function call. These are called function-like macros. To define a function-like macro, you use the same #define directive, but you put a pair of parentheses immediately after the macro name. For example, #define lang_init() c_init() lang_init() ==> c_init() Function-like macros can take arguments, just like true functions. As an example, here is a macro that computes the minimum of two numeric values, as it is defined in many C programs, and some uses. #define min(x, Y) ((X) < (Y)? (X) : (Y)) x = min(a, b); ==> x = ((a) < (b)? (a) : (b)); y = min(1, 2); ==> y = ((1) < (2)? (1) : (2)); z = min(a + 28, *p); ==> z = ((a + 28) < (*p)? (a + 28) : (*p));

Compile & Run C program on Ubuntu Linux Compiler Command: gcc -o [targetname] [.c file Name]

Useful Links Pass by Value or Pass by Pointer: http://denniskubes.com/2012/08/20/is-c-pass-by-value-or-reference/ Explain list_for_each_entry and list_for_each_entry_safe http://stackoverflow.com/questions/16230524/explain-list-for-each-entry-and-list-for-each-entry-safe Swap function in java http://stackoverflow.com/questions/2393906/how-do-i-make-my-swap-function-in-java Macro https://gcc.gnu.org/onlinedocs/cpp/macros.html