First Program in C With Output. C Language Keywords.

Slides:



Advertisements
Similar presentations
Character Arrays (Single-Dimensional Arrays) A char data type is needed to hold a single character. To store a string we have to use a single-dimensional.
Advertisements

C Characters & Strings Character Review Character Handling Library Initialization String Conversion Functions String Handling Library Standard Input/Output.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Characters and Strings.
Lecture 2 Introduction to C Programming
Introduction to C Programming
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
Chapter 8 Characters and Strings Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 8 - Characters and Strings Outline 8.1Introduction.
1 Lecture 2  Input-Process-Output  The Hello-world program  A Feet-to-inches program  Variables, expressions, assignments & initialization  printf()
Basic C Programming Data Types and Arithmetic Operations 01/30/15.
Chapter 9 Formatted Input/Output Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
0 Chap. 2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations Imperative Programming, B. Hirsbrunner,
1 Character Processing How characters can be treated as small integers? How characters are stored and manipulated in a machine? How use is made of certain.
Chapter 2 Data Types, Declarations, and Displays
Basic Input/Output and Variables Ethan Cerami New York
C Formatted Input/Output /* Using Integer Conversion Specifiers */ #include int main ( ) { printf( "%d\n", 455 ); printf( "%i\n", 455 ); printf( "%d\n",
 2007 Pearson Education, Inc. All rights reserved C Characters and Strings.
Introduction to programming Language C, Data Types, Variables, Constants. Basics of C –Every program consists of one or more functions and must have main.
Programming Variables. Named area in the computer memory, intended to contain values of a certain kind (integers, real numbers, characters etc.) They.
Basic Input - Output. Output functions  printf() – is a library function that displays information on-screen. The statement can display a simple text.
Chapter 2 Getting Started in C Programming
Chapter 2: C Fundamentals Dr. Ameer Ali. Overview C Character set Identifiers and Keywords Data Types Constants Variables and Arrays Declarations Expressions.
C Programming Lecture 3. The Three Stages of Compiling a Program b The preprocessor is invoked The source code is modified b The compiler itself is invoked.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Computer Science, IUPUI
Chapter 5: Data Input and Output Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills
Chapter 9 Formatted Input/Output. Objectives In this chapter, you will learn: –To understand input and output streams. –To be able to use all print formatting.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 8 - Characters and Strings Outline 8.1Introduction 8.2Fundamentals of Strings and Characters 8.3Character.
C Tokens Identifiers Keywords Constants Operators Special symbols.
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.
Program A computer program (also software, or just a program) is a sequence of instructions written in a sequence to perform a specified task with a computer.
Chapter 6 Fundamental Types Dept of Computer Engineering Khon Kaen University.
Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Ivor Horton, Beginning ANSI C++: The Complete Language, 3 rd.
Data & Data Types & Simple Math Operation 1 Data and Data Type Standard I/O Simple Math operation.
Constants Numeric Constants Integer Constants Floating Point Constants Character Constants Expressions Arithmetic Operators Assignment Operators Relational.
Introduction to C Programming Chapter 2 : Data Input, Processing and Output.
Structure of a C program Preprocessor directive (header file) Program statement } Preprocessor directive Global variable declaration Comments Local variable.
Arrays II (Strings). Data types in C Integer : int i; Double: double x; Float: float y; Character: char ch; char cha[10], chb[]={‘h’,’e’,’l’,’l’,’o’};
CS115 FALL Senem KUMOVA-METİN1 The Fundamental Data Types CHAPTER 3.
CSCI 3133 Programming with C Instructor: Bindra Shrestha University of Houston – Clear Lake.
Created by Harry H. Cheng,  2009 McGraw-Hill, Inc. All rights reserved. C for Engineers and Scientists Chapter 3: Number Systems, Scalar Types, and Input.
Copyright 2001 Oxford Consulting, Ltd1 January Building Blocks Overview In this lesson We will study the basic building blocks of a C++ program.
Introducing constants, variables and data types March 31.
Java Programming, Second Edition Chapter Two Using Data Within a Program.
Chapter 7 C supports two fundamentally different kinds of numeric types: (a) integer types - whole numbers (1) signed (2) unsigned (b) floating types –
Variables Symbol representing a place to store information
Module B - Computation1/61 Module-B-Computation Variables Basic Memory Operations Expressions.
CTYPE.H Introduction  The ctype header is used for testing and converting characters.  A control character refers to a character that.
1 MT258 Computer Programming and Problem Solving Tutorial 03.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Computer Science, IUPUI
1 Objects Types, Variables, and Constants Chapter 3.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Computer Science, IUPUI
7. BASIC TYPES. Systems of numeration Numeric Types C’s basic types include integer types and floating types. Integer types can be either signed or unsigned.
© Copyright by Deitel 1 Chapter 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple.
CSCI206 - Computer Organization & Programming
C Characters and Strings
Character Processing How characters can be treated as small integers?
Input/output.
Variable Symbol represents a place to store information
CSCI206 - Computer Organization & Programming
Chapter 8 - Characters and Strings
Visit for more Learning Resources
Chapter 2 - Introduction to C Programming
CSCE 206 Lab Structured Programming in C
CS111 Computer Programming
Character Processing How characters can be treated as small integers?
Introduction to C Programming
C – Programming Language
CSCE 206 Lab Structured Programming in C
DATA TYPES There are four basic data types associated with variables:
Introduction to C Programming
Presentation transcript:

First Program in C With Output

C Language Keywords

Variables In C

Initialize int value in declaration

Using a variable to store value

Meaningful variable name

Define three variables and use assignment operator to assign value int main() { int term; /* term used in two expressions */ int term_2; /* twice term */ int term_3; /* three times term */ term = 3 * 5; term_2 = 2 * term; term_3 = 3 * term; return (0); }

Use printf to output variable

Printing Variable Contents

Finding the limits

#include #include #include int main(void) { printf("Variables of type char store values from %d to %d", CHAR_MIN, CHAR_MAX); printf("\nVariables of type unsigned char store values from 0 to %u", UCHAR_MAX); printf("\nVariables of type short store values from %d to %d", SHRT_MIN, SHRT_MAX ); printf("\nVariables of type unsigned short store values from 0 to %u",USHRT_MAX); printf("\nVariables of type int store values from %d to %d", INT_MIN, INT_MAX); printf("\nVariables of type unsigned int store values from 0 to %u", UINT_MAX); printf("\nVariables of type long store values from %ld to %ld", LONG_MIN, LONG_MA X); printf("\nVariables of type unsigned long store values from 0 to %lu", ULONG_MAX); printf("\nVariables of type long long store values from %lld to %lld", LLONG_MIN, LLO NG_MAX);

printf("\nVariables of type unsigned long long store values from 0 to %llu", U LLONG_MAX); printf("\n\nThe size of the smallest non- zero value of type float is %.3e", FLT_MIN); printf("\nThe size of the largest value of type float is %.3e", FLT_MAX); printf("\nThe size of the smallest non- zero value of type double is %.3e", DBL_MIN); printf("\nThe size of the largest value of type double is %.3e", DBL_MAX); printf("\nThe size of the smallest non- zero value of type long double is %.3Le", LDBL_MIN); printf("\nThe size of the largest value of type long double is %.3Le\n", LDBL_ MAX); printf("\nVariables of type float provide %u decimal digits precision.", FLT_D IG); printf("\nVariables of type double provide %u decimal digits precision.", DBL _DIG); printf("\nVariables of type long double provide %u decimal digits precision.", LDBL_DIG); return 0; }

OUTPUT

Finding the size of a type

Scope of variables

value of i is 1 the value of i is 10 The Value of i is 1 The Value of i is 10

Inner variable shadows outer variable

Output

Declare global variables

Define and use Global variables

Local variable shadows global variable.

Static variable

Output

Static versus automatic variables

Output address and value

Using the & operator

Calculate an average using variable argument lists

Adding Comments

Indentation and Code Format

Without Indentation

With Indentation

Clarity

Header Files

IncludeHeader File

Newly Added Headers

External Reference

// Program in file A1.c #include void hell() { int i =60; printf("value o f i %d\n",i); }

// Program in file A2.c #include extern int i; void main() { int i =50; clrscr(); hell(); printf("value of i %d\n",i); }

output The value of variable i is 60

Data Types

Unsigned Variables

Reading Integer’s

Reading integer’s can be done through scanf void main(){ int i; scanf(“%d”,&i); printf(“i=%d”,i); }

#include int main() { printf( "%4d\n", 1 ); printf( "%4d\n", 12 ); printf( "%4d\n", 123 ); printf( "%4d\n", 1234 ); printf( "%4d\n\n", ); printf( "%4d\n", -1 ); printf( "%4d\n", -12 ); printf( "%4d\n", -123 ); printf( "%4d\n", ); printf( "%4d\n", ); return 0; }

#include int main() { printf( "%4d\n", 1 ); printf( "%4d\n", 12 ); printf( "%4d\n", 123 ); printf( "%4d\n", 1234 ); printf( "%4d\n\n", ); printf( "%4d\n", -1 ); printf( "%4d\n", -12 ); printf( "%4d\n", -123 ); printf( "%4d\n", ); printf( "%4d\n", ); return 0; }

Simple Int Calculation

Divide Integer

Sum the integers from 1 to a user- specified number

If both operands i1 and i2 are integers, the expression i1/i2 provides integer division

The atoi() function: read numeric values from the keyboard

Hexadecimal Numbers

Octal Numbers

Save Tab key into a char, you use an escape sequence

Output

#include int main() { clrscr(); // char tab='\x9C'; printf("**********"); printf("%%%\\Hello"); // printf("a%cb",tab); return 0; }

SOME % WILL BE DIMINISHED BECOZ OF “\\” output

#include int main() { clrscr(); // char tab='\x9C'; printf("**********"); printf("\?Hello\?"); // printf("a%cb",tab); return 0; }

Display the ASCII characters and their corresponding codes, from Code 32 on up to Code 127

Overflow in char and unsigned char data type Overflow means you are carrying out an operation such that the value either exceeds the maximum value or is less than the minimum value of the data type. (Definition from C & Data Structures by P.S. Deshpande and O.G. Kakde Charles River Media 2004)

Compare characters to characters in if statement #include void main() { char cResponse = '\0'; clrscr(); printf("\n\tAC Control Unit\n"); printf("\na\tTurn the AC on\n"); printf("b\tTurn the AC off\n"); printf("\nEnter your selection: "); scanf("%c", &cResponse);

Compare char variable in if statement

if (cResponse == 'a') printf("\nAC is now on\n"); if (cResponse == 'b') printf("\nAC is now off\n"); getch(); }

Using functions islower, isupper, tolower, toupper

Using functions isdigit, isalpha, isalnum, and isxdigit

Using functions isspace, iscntrl, ispunct, isprint, isgraph

Floats

C provides two main floating-point representations: float (single precision) and double (double precision ). A floating-point number has a fractional part and a biased exponent. Float occupies 4 bytes and double occupies 8 bytes.

Use the format %f for printing floating numbers

Float Basics

Define float constant value using Macro

Assignment of an integer expression to a floating-point variable. C will automatically perform the conversion from integer to floating point. A similar conversion is performed when a floating-point number is assigned to an integer.

output

To create precision with floating-point numbers

use the %E in printf() to display scientific-notation numbers

Printing floating-point numbers with floating-point conversion specifiers

Read float number and do a calculation