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

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

Syntax and Variables

Presented By : Gaurav Juneja

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

Programming in C++ 4. The lexical basis of C++

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

C: How to Program. Week /Mar/05

C Language, Token, Keywords, Constant, variable

Chapter 2 - Introduction to C Programming

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

Data Types and Variables in C language

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

CMSC 104 -Lecture 5 John Y. Park, adapted by C Grasso

Programming and Data Structures

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

A Fast Review of C Essentials Part I

Fundamentals of Programming

Character Set. The character set of C represents alphabet, digit or any symbol used to represent information. Digits 0, 1, 2, 3, 9

Variables in C. CMSC 104, Spring 2014 Christopher S. Marron. (thanks to John Park for slides) Tuesday, February 18, 14

Basic Elements of C. Staff Incharge: S.Sasirekha

ET156 Introduction to C Programming

VARIABLES AND CONSTANTS

DEPARTMENT OF MATHS, MJ COLLEGE

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

2/29/2016. Definition: Computer Program. A simple model of the computer. Example: Computer Program. Data types, variables, constants

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

Fundamental of Programming (C)

Recap. ANSI C Reserved Words C++ Multimedia Programming Lecture 2. Erwin M. Bakker Joachim Rijsdam

LESSON 1. A C program is constructed as a sequence of characters. Among the characters that can be used in a program are:

Introduction to Computing Lecture 01: Introduction to C

.. Cal Poly CPE 101: Fundamentals of Computer Science I Alexander Dekhtyar..

Lecture 02 C FUNDAMENTALS

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

The component base of C language. Nguyễn Dũng Faculty of IT Hue College of Science

INTRODUCTION 1 AND REVIEW

6.096 Introduction to C++ January (IAP) 2009

DETAILED SYLLABUS INTRODUCTION TO C LANGUAGE

BLM2031 Structured Programming. Zeyneb KURT

Chapter 1 & 2 Introduction to C Language

!"#$% &'($) *+!$ 0!'" 0+'&"$.&0-2$ 10.+3&2),&/3+, %&&/3+, C,-"!.&/+"*0.&('1 :2 %*10% *%7)/ 30'&. 0% /4%./

UNIT - I. Introduction to C Programming. BY A. Vijay Bharath

PROGRAMMAZIONE I A.A. 2018/2019

Chapter 2: Overview of C. Problem Solving & Program Design in C

Data types, variables, constants

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

BSM540 Basics of C Language

CS Programming In C

XSEDE Scholars Program Introduction to C Programming. John Lockman III June 7 th, 2012

In this session we will cover the following sub-topics: 1.Identifiers 2.Variables 3.Keywords 4.Statements 5.Comments 6.Whitespaces 7.Syntax 8.

Main Program. C Programming Notes. #include <stdio.h> main() { printf( Hello ); } Comments: /* comment */ //comment. Dr. Karne Towson University

University of Technology. Laser & Optoelectronics Engineering Department. C++ Lab.

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

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

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

Week 3 Lecture 2. Types Constants and Variables

Tokens, Expressions and Control Structures

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

ANSI C Programming Simple Programs

Introduction to C# Applications

Variables. Data Types.

ET156 Introduction to C Programming

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

CMPE-013/L. Introduction to C Programming

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

Introduction to C programming. By Avani M. Sakhapara Asst Professor, IT Dept, KJSCE

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

Writing an ANSI C Program Getting Ready to Program A First Program Variables, Expressions, and Assignments Initialization The Use of #define and

C Concepts - I/O. Lecture 19 COP 3014 Fall November 29, 2017

Computers Programming Course 5. Iulian Năstac

Structured Programming. Jon Macey

Programming. C++ Basics

Lecture 3. More About C

Introduction to the C Programming Language

printf( Please enter another number: ); scanf( %d, &num2);

3. Java - Language Constructs I

C/Java Syntax. January 13, Slides by Mark Hancock (adapted from notes by Craig Schock)

C/Java Syntax. Lecture 02 Summary. Keywords Variable Declarations Data Types Operators Statements. Functions. if, switch, while, do-while, for

C Programming Review CSC 4320/6320

공학프로그래밍언어 (PROGRAMMING LANGUAGE FOR ENGINEERS) -VARIABLE AND DATA TYPES- SPRING 2015, SEON-JU AHN, CNU EE

Basic Types, Variables, Literals, Constants

Number Systems, Scalar Types, and Input and Output

COMP 2355 Introduction to Systems Programming

Learning to Program with Haiku

CS113: Lecture 3. Topics: Variables. Data types. Arithmetic and Bitwise Operators. Order of Evaluation

ME240 Computation for Mechanical Engineering. Lecture 4. C++ Data Types

ANSI C Reserved Words

Non-numeric types, boolean types, arithmetic. operators. Comp Sci 1570 Introduction to C++ Non-numeric types. const. Reserved words.

Creating, Compiling and Executing

Fundamental of C programming. - Ompal Singh

1/25/2018. ECE 220: Computer Systems & Programming. Write Output Using printf. Use Backslash to Include Special ASCII Characters

IECD Institute for Entrepreneurship and Career Development Bharathidasan University, Tiruchirappalli 23.

Storage class and Scope:

BSM540 Basics of C Language

Lecture 02 Summary. C/Java Syntax 1/14/2009. Keywords Variable Declarations Data Types Operators Statements. Functions

Data Type Fall 2014 Jinkyu Jeong

Review of the C Programming Language for Principles of Operating Systems

Rule 1-3: Use white space to break a function into paragraphs. Rule 1-5: Avoid very long statements. Use multiple shorter statements instead.

Data Types. Data Types. Integer Types. Signed Integers

Unit 4. Input/Output Functions

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

Transcription:

C introduction Variables Variables 1 / 14

Contents Variables Data types Variable I/O Variables 2 / 14

Usage Declaration: t y p e i d e n t i f i e r ; Assignment: i d e n t i f i e r = v a l u e ; Definition (all at once): t y p e i d e n t i f i e r = v a l u e ; Example: i n t number ; / d e c l a r a t i o n / number = 4 2 ; / a s s i g n m e n t / i n t another number = 2 3 ; / d e f i n i t i o n / Variables 3 / 14

Saving lines Multiple declarations i n t number, another number ; Multiple Definitions i n t number = 42, anothernumber = 2 3 ; But be careful: i n t a = 23, b = 2 3 ; i n t a, b = 2 3 ; Avoid multiple variable definitions at one line! Variables 4 / 14

Valid identifiers Consist of English letters (no ß, ä, ö, ü), numbers and underscore ( ) Start with a letter or underscore Are case sensitive (number differs from Number) Must not be reserved words: auto else long switch break enum register typedef case extern return union char float short unsigned const for signed void continue goto sizeof volatile default if static while do int struct double Style: Stay in one language (English recommended) Decide whether to use camelcaseidentifiers or snake case identifiers. Variables 5 / 14

Speaking identifiers 1 / c a l c u l a t e volume o f s q u a r e pyramid / 2 i n t a, b, c ; 3 a = 3 ; 4 b = 2 ; 5 c = (1 / 3) a a b ; 1 / c a l c u l a t e volume o f s q u a r e pyramid / 2 i n t l e n g t h, h e i g h t, volume ; 3 l e n g t h = 3 ; 4 h e i g h t = 2 ; 5 volume = (1 / 3) l e n g t h l e n g t h h e i g h t ; Variables 6 / 14

Use speaking identifiers. Please, use speaking identifiers. 1 1 Seriously, use speaking identifiers. Variables 7 / 14

Integer numbers Keywords: int, short, long Stored as a binary number with fixed length Can be signed(default) or unsigned Actual size of int, short, long depends on architecture Example (64 Bit): i n t a ; / Range : 2.147.483.648 to 2. 1 4 7. 4 8 3. 6 4 7 / u n s i g n e d s h o r t b ; / Range : 0 to 65.535 / Variables 8 / 14

Floating point numbers Keywords: float, double, long double Stored as specified in IEEE 754 Standard TL;DR Special values for,, NaN Useful for fractions and very large numbers Type a decimal point instead of a comma! Example: f l o a t x = 0. 1 2 5 ; / P r e c i s i o n : 7 to 8 d i g i t s / d o u b l e y = 1 1 1 1 1 1. 1 1 1 1 1 1 ; / P r e c i s i o n : 15 to 16 d i g i t s / Variables 9 / 14

Characters Keyword: char Can be signed(default) or unsigned Size: 1 Byte (8 Bit) on almost every architecture Intended to represent a single character Stores its ASCII number (e.g. A 65) You can define a char either by its ASCII number or by its symbol: c h a r a = 6 5 ; c h a r b = A ; / use s i n g l e q u o t a t i o n marks / Variables 10 / 14

printf() with placeholders The string you pass to printf may contain placeholders: i n t a = 3 ; i n t b = 5 ; f l o a t c = 7. 4 ; p r i n t f ( a : %d\n, a ) ; p r i n t f ( b : %d\ nc : %f \n, b, c ) ; Output: a : 3 b : 5 c : 7. 4 You can insert any amount of placeholders. For each placeholder, you have to pass a value of the corresponding type. Variables 11 / 14

Example placeholders The placeholder determines how the value is interpreted. To avoid compiler warnings, only use the following combinations: type description type of argument %c single character char, int (if <= 255) %d decimal number char, int %u unsigned decimal number unsigned char, unsigned int %X hexadecimal number char, int %ld long decimal number long %f floating point number float, double Variables 12 / 14

Variable input scanf() is another useful function from the standard library. Like printf(), it is declared in stdio.h Like printf(), it has a format string with placeholders You can use it to read values of primitive datatypes from the command line Example: i n t i ; s c a n f ( %d, &i ) ; After calling scanf(), the program waits for the user to input a value in the command line. After pressing the return key, that value is stored in i. Variables 13 / 14

Note: scanf() uses the same placeholders as printf() You must type an & before each variable identifier (more about this later) If you read a number (using %d, %u etc.), interpretation Starts at first digit Ends before last non digit character If you use %c, the first character of the user input is interpreted (this may be a as well!) Never trust the user: they may enter a blank line while you expect a number, which means your input variable is still undefined! Variables 14 / 14