DATA TYPE AND DISPLAY 350142 -Computer Programming Asst. Prof. Dr. Choopan Rattanapoka and Asst. Prof. Dr. Suphot Chunwiphat.

Slides:



Advertisements
Similar presentations
CS0007: Introduction to Computer Programming Console Output, Variables, Literals, and Introduction to Type.
Advertisements

Dale Roberts Basic I/O – printf() CSCI 230 Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Department of.
11-2 Identify the parts of the “main” function, which include Preprocessor Directives main function header main function body which includes Declaration.
Lecture 2 Introduction to C Programming
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
Principles of Programming Fundamental of C Programming Language and Basic Input/Output Function 1.
Types and Variables. Computer Programming 2 C++ in one page!
Structure of a C program
1 Lecture 2  Input-Process-Output  The Hello-world program  A Feet-to-inches program  Variables, expressions, assignments & initialization  printf()
Introduction to C Programming CE Lecture 2 Basics of C programming.
Data types and variables
Chapter 2: Introduction to C++.
C Formatted Input/Output /* Using Integer Conversion Specifiers */ #include int main ( ) { printf( "%d\n", 455 ); printf( "%i\n", 455 ); printf( "%d\n",
Chapter 3: Introduction to C Programming Language C development environment A simple program example Characters and tokens Structure of a C program –comment.
Printing. printf: formatted printing So far we have just been copying stuff from standard-in, files, pipes, etc to the screen or another file. Say I have.
Basic Elements of C++ Chapter 2.
CS 161 Introduction to Programming and Problem Solving Chapter 13 Console IO Herbert G. Mayer, PSU Status 9/8/2014 Initial content copied verbatim from.
By: Mr. Baha Hanene Chapter 3. Learning Outcomes We will cover the learning outcome 02 in this chapter i.e. Use basic data-types and input / output in.
Basic Input - Output. Output functions  printf() – is a library function that displays information on-screen. The statement can display a simple text.
Objectives You should be able to describe: Data Types
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
1 IPC144 Session 11 The C Programming Language. 2 Objectives To format a #define statement correctly To use a #define statement in a C program To construct.
Input & Output: Console
C Tokens Identifiers Keywords Constants Operators Special symbols.
Programming I Introduction Introduction The only way to learn a new programming language is by writing programs in it. The first program to.
C-Language Keywords(C99)
Sales person receive RM200/week plus 9% of their gross sales for that week. Write an algorithms to calculate the sales person’s earning from the input.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Introduction to C Programming Angela Chih-Wei Tang ( 唐 之 瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan 2010 Fall.
Week 1 Algorithmization and Programming Languages.
Copyright © 2012 Pearson Education, Inc. Chapter 2: Introduction to C++
Introduction to C Programming Chapter 2 : Data Input, Processing and Output.
Chapter 3: Formatted Input/Output Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 3 Formatted Input/Output.
BASICS CONCEPTS OF ‘C’.  C Character Set C Character Set  Tokens in C Tokens in C  Constants Constants  Variables Variables  Global Variables Global.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 2: Introduction to C++
3. FORMATTED INPUT/OUTPUT. The printf Function The first argument in a call of printf is a string, which may contain both ordinary characters and conversion.
The character data type char. Character type char is used to represent alpha-numerical information (characters) inside the computer uses 2 bytes of memory.
A first program 1. #include 2. using namespace std; 3. int main() { 4. cout
CSC141 Introduction to Computer Programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture - 6.
CSCI 3133 Programming with C Instructor: Bindra Shrestha University of Houston – Clear Lake.
Chapter 4 Literals, Variables and Constants. #Page2 4.1 Literals Any numeric literal starting with 0x specifies that the following is a hexadecimal value.
Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments.
Minimal standard C program int main(void) { return 0 ; }
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Operating System Discussion Section. The Basics of C Reference: Lecture note 2 and 3 notes.html.
Types Chapter 2. C++ An Introduction to Computing, 3rd ed. 2 Objectives Observe types provided by C++ Literals of these types Explain syntax rules for.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2 Introduction to C++
Lecture 4: C Programming & Data Types Bryan Burlingame 24 Feb 2016.
CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2012 CMPE-013/L Constants and Literals Gabriel Hugh Elkaim Spring 2012.
1 C Syntax and Semantics Dr. Sherif Mohamed Tawfik Lecture Two.
Chapter 3: Formatted Input/Output 1 Chapter 3 Formatted Input/Output.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 2.
+ Note On the Use of Different Data Types Use the data type that conserves memory and still accomplishes the desired purpose. For example, depending on.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
2.1 The Part of a C++ Program. The Parts of a C++ Program // sample C++ program #include using namespace std; int main() { cout
C++ First Steps.
Formatted Input and Output
Chapter Topics The Basics of a C++ Program Data Types
Wel come.
Basic Elements of C++.
Basic Elements of C++ Chapter 2.
Variables In programming, we often need to have places to store data. These receptacles are called variables. They are called that because they can change.
INPUT & OUTPUT scanf & printf.
2.1 Parts of a C++ Program.
Conversion Check your class notes and given examples at class.
Chapter 2: Introduction to C++.
ECE 103 Engineering Programming Chapter 8 Data Types and Constants
Introduction to C Programming
C Programming Lecture-3 Keywords, Datatypes, Constants & Variables
Presentation transcript:

DATA TYPE AND DISPLAY Computer Programming Asst. Prof. Dr. Choopan Rattanapoka and Asst. Prof. Dr. Suphot Chunwiphat

Structure of C Programming Language

Preprocessor Directives  Normally, we use this part for referencing files with.h extension  In C, standard function definitions locate in.h files For example, printf function definition locates in stdio.h file  We can reference.h file with a preprocessor directive #include

Main Function  C program starts at main function  There are many way to write a main function  void main(void) ‏  void main(int argc, char **argv);  int main(void) ‏  int main( )  int main(int argc, char **argv); recommend

C Language Syntax  Number of { and } must be the same  We call statements inside { and } a block  Every statements that don’t follow by a block, need to end up with semicolon “;”  Remember that All of C standard functions name are lowercase

Example: Error in C Program Must be a lowercase p Remove one } Add ;

C Data types  7 basic data types  Integer number: short, int, long  Floating point number: float, double, long double  character: char  Complex data types (based on basic data types)  array  structure  union  enumerate

Data Types and Sizes TypeMemory SizeRemarks char1 bytecan hold a single character or an integer value ranging from (-128 to 127) short2 bytescan hold an integer value ranging from ( to 32767) int4 bytescan hold an integer value range from (-2,147,483,648 to 2,147,483,647) long4 bytescan hold an integer value range from (-2,147,483,648 to 2,147,483,647) float4 bytesSingle precision floating point value (-3.4x to 3.4x ) double8 bytesDouble precision floating point value (-1.7x to 1.7x )

Character (char) data type  When we store data on memory, we can store only in the form of binary number (0 and 1)  Thus, we can’t store any character (a, b, c, etc.) in memory directly  We need a standard that can convert character into a number that can store on memory and that standard is ASCII  ASCII : American Standard Code for Information Interchange

ASCII Table

Special Character in C (Escape sequence) CharacterMeaningResult \aAlert or bell Produces an audible alert \bBackspace Moves the active position to the previous position on the current line \fForm-feed Moves the active position to the initial position on the next line \nNew-line Moves the active position to the initial position of the next line \rReturn Moves the active position to the initial position of the current line \tHorizontal tab Moves the active position to the next horizontal tabulation position \vVertical tab Moves the active position to the next vertical tabulation position \\Backslash Displays a backslash (\) \’Single quote Displays a single quote (‘) \’’Double quote Displays the double quote (“) \xffA character corresponding to ASCII (ff) Displays a character by using ASCII code (ff) * Though there are two or more characters in an escape sequence, they are considered as a single character.

Variables in C  These are names of language objects. These can take many values, but one at a time. Once assigned, their values can be changed during the program execution. Values can be stored in a symbolic name (variable) in the computer memory. Usually variables are named with the description that transmits the idea of what value it hold.  For example,  if a root of a quadratic eq. is to be calculated, the variable in which the value of root should be stored can be named as ‘root’.

Naming a variable  Any variable should be named so as to distinguish one from the other. The following are the rules to name a variable.  It can be of letters, digits, dollar($) and underscores( _ ).  No other special characters are allowed.  First character should be a letter, dollar($) or an underscore.  Upper and lower case letters are significant. Eg. RESULT, Result and result are considered to be three different variables.  Reserved words cannot be identifier names.

Reserved words autobreakcase charconstcontinue defaultdodouble elseenumextern floatforgoto ifinlineint longregisterrestrict returnshortsigned sizeofstaticstruct switchtypedefunion unsignedvoidvolatile while_Bool_Complex _Imaginary

QUIZ 1  Each of the following variable names is correct or not ?  _Hello123  A_T_o_m  50cities  In$ide  CO2_Ho2  I..Love..U  break  Hi!!  Power#3  float

Variable Declaration (1)  Through the declaration statements, C interprets the identifier in a specific format. A declaration also causes storage to be reserved for an identifier. All C variables should be declared before its usage, usually at the beginning of the functions, before any executable statement.  data_type variable_name = initial value ; Optional

Variable Declaration (2)

printf() function  printf function is used to display content of variable or string on the monitor  The content that will be displayed on the monitor is between the (“ and ”) symbols  The principle of printf function is to take data from memory and display them on the monitor

Example: printf  printf(“Hello World”);  Display Hello World on the screen  printf(“Hello World\n”);  Display Hello World and move cursor to a new line  printf(“Hello\tWorld\n”);  Display Hello and some space (tab width) and then follow by World and move cursor to a new line

How to display the contents of variables  We can use the printf() function to print the contents of variables to the monitor screen.  Following lists the characters that used for conversion specification and a percent (%) symbol should precede the conversion character in the printf() function. Conversion CharactersTranslate the content of a variable into %dA decimal integer %fFloating point number %eFloating point number in scientific notation Ex e+02 %cA single character %sA string of character %xAn hexadecimal integer

Example 1 #include int main(int argc, char **argv) { int A = 5; float B = ; printf(“%f %d”, B, A); system(“PAUSE”); return 0; }

Example 2 #include int main(int argc, char **argv) { char X = ‘A’, Y = ‘B’; printf(“%c-ANT \n%c-Bird\n”, X, Y); system(“PAUSE”); return 0; }

QUIZ 2 What is the output of the following program ? int x = 123, y = 456; float z = ; char myChar = ‘x’; printf(“x = %d and y = %d\n”, x, y); printf(“x = %d, y = %d and z = %f\n”, x, y, z); printf(“myChar is %c\n”, myChar); printf(“myChar is %d\n”, myChar); printf(“number = %d\n”, 150);

Field Width (1)  We can simply add a number between the % and the conversion character such as %5d. This number specifies the field width—the number of column to use for the display of the value.  Integer number (%d)  The positive number after % such as %5d means fill the blank with 5 spaces and then fill number from the right position to left position.  The negative number after % such as %-5d means fill the blank with 5 spaces and the fill the number from the left position to the right position.

Field Width (2)  Floating point number (%f)  We can limit the number of digit of fractional number by using %. such as %.2f means we want to display only 2 digits of the fractional number.  We can also control the display field width just like integer number

Example int main(int argc, char **argv) { int a; float b; a = 25; b = ; printf("A = %d\n", a); printf("B = %f\n", b); printf("A = %8d\n", a); printf("B = %8.2f\n", b); system("PAUSE"); }

QUIZ 3 int main(int argc, char **argv) { int a = 5, b = 50, c = 500; float f = ; printf(“a = %6d\n”, a); printf(“b = %6d\n”, b); printf(“c = %6d\n”, c); printf(“f = %6.3f\n”, f); system("PAUSE"); return 0; }