Introduction to C Programming CE00312-1 Lecture 2 Basics of C programming.

Slides:



Advertisements
Similar presentations
Single Variable and a Lot of Variables The declaration int k; float f; reserve one single integer variable called k and one single floating point variable.
Advertisements

C Programming. printf int printf ( const char * format,... ); printf ("Characters: %c \n", 'a'); printf ("Decimals: %d %f\n", 1977, 3.14); specifierOutputExample.
Principles of Programming Fundamental of C Programming Language and Basic Input/Output Function 1.
1 ICS103 Programming in C Lecture 3: Introduction to C (2)
1 9/10/07CS150 Introduction to Computer Science 1 Data Types Section 2.7 – 2.12 CS 150 Introduction to Computer Science I.
Structure of a C program
C Programming Language 4 Developed in 1972 by Dennis Ritchie at AT&T Bell Laboratories 4 Used to rewrite the UNIX operating system 4 Widely used on UNIX.
1 Lecture 2  Input-Process-Output  The Hello-world program  A Feet-to-inches program  Variables, expressions, assignments & initialization  printf()
1 Fundamental Data Types. 2 Declaration All variables must be declared before being used. –Tells the compiler to set aside an appropriate amount of space.
1 9/8/08CS150 Introduction to Computer Science 1 Data Types Section 2.7 – 2.12 CS 150 Introduction to Computer Science I.
Basic C Programming Data Types and Arithmetic Operations 01/30/15.
Data types and variables
1 Key Concepts:  Data types in C.  What is a variable?  Variable Declaration  Variable Initialization  Printf()  Scanf()  Working with numbers in.
CS150 Introduction to Computer Science 1
Input/Output  Input/Output operations are performed using input/output functions  Common input/output functions are provided as part of C’s standard.
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
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.
A Variable is symbolic name that can be given different values. Variables are stored in particular places in the computer ‘s memory. When a variable is.
Input & Output: Console
STRING Dong-Chul Kim BioMeCIS UTA 10/7/
C Tokens Identifiers Keywords Constants Operators Special symbols.
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.
Data & Data Types & Simple Math Operation 1 Data and Data Type Standard I/O Simple Math operation.
BASICS CONCEPTS OF ‘C’.  C Character Set C Character Set  Tokens in C Tokens in C  Constants Constants  Variables Variables  Global Variables Global.
CONSTANTS Constants are also known as literals in C. Constants are quantities whose values do not change during program execution. There are two types.
DATA TYPE AND DISPLAY Computer Programming Asst. Prof. Dr. Choopan Rattanapoka and Asst. Prof. Dr. Suphot Chunwiphat.
CS115 FALL Senem KUMOVA-METİN1 The Fundamental Data Types CHAPTER 3.
Chapter 3 – Variables and Arithmetic Operations. Variable Rules u Must declare all variable names –List name and type u Keep length to 31 characters –Older.
CSCI 3133 Programming with C Instructor: Bindra Shrestha University of Houston – Clear Lake.
Chapter 2 Variables.
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { public static void main(String[]
Constants, Variables and Data types in C The C character Set A character denotes any alphabet, digit or special symbol used to represent information.
Chapter 7 C supports two fundamentally different kinds of numeric types: (a) integer types - whole numbers (1) signed (2) unsigned (b) floating types –
Programming Fundamentals. Summary of previous lectures Programming Language Phases of C++ Environment Variables and Data Types.
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
Operating System Discussion Section. The Basics of C Reference: Lecture note 2 and 3 notes.html.
1 09/03/04CS150 Introduction to Computer Science 1 What Data Do We Have.
Tokens in C  Keywords  These are reserved words of the C language. For example int, float, if, else, for, while etc.  Identifiers  An Identifier is.
Lecture 5 Computer programming -1-. Input \ Output statement 1- Input (cin) : Use to input data from keyboard. Example : cin >> age; 2- Output (cout):
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.
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
CMSC 104, Version 8/061L09VariablesInC.ppt Variables in C Topics Naming Variables Declaring Variables Using Variables The Assignment Statement Reading.
C Building Block Chapter 2. Variables A variable is a space in the computer’s memory set aside for a certain kind of data and given a name for easy reference.
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 3.
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.
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.
Chapter 2 Variables and Constants. Objectives Explain the different integer variable types used in C++. Declare, name, and initialize variables. Use character.
Chapter 2 Variables.
Introduction to the C Language
BASIC ELEMENTS OF A COMPUTER PROGRAM
Programming Fundamentals
Tokens in C Keywords Identifiers Constants
Input/output.
7. BASIC TYPES.
ICS103 Programming in C Lecture 3: Introduction to C (2)
By: Syed Shahrukh Haider
Fundamental Data Types
Introduction to the C Language
IDENTIFIERS CSC 111.
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.
Chapter 2 Variables.
Lectures on Numerical Methods
Fundamental Data Types
Unit 3: Variables in Java
Chapter 2 Variables.
DATA TYPES There are four basic data types associated with variables:
Variables and Constants
Presentation transcript:

Introduction to C Programming CE Lecture 2 Basics of C programming

Overall Structure of a C program # include DEFINITION OF ANY CONSTANTS int main(void) /* some explanatory comment */ { DECLARATIONS STATEMENTS return 0; }

Variables Variables are used to store and retrieve information each variable has a name variable names can consist of letters, numbers and _ (underscore) cannot start with a number no spaces or special characters ($, #) case sensitive use meaningful names!

Data types Information is stored in the computer as a series of 0s and 1s how these bits are interpreted for a given variable depends on its data type length (number of bytes) allowed operations the data type and name of a variable must be declared before it is used

Data Types There are two basic numeric data types in C integer for whole numbers and characters real for numeric values with decimal parts There are several variants of each type There is a type to store a single character which is a special variant of integer type There is no basic data type for strings!!

Data Types Basic Data Types int i,j;4 bytes float x,y;4 bytes double p,q;8 bytes char c;1 byte (ASCII)

Integer data types size is machine dependent is ‘A’ or 65 each character has an integer equivalent can perform integer operations on characters ‘a’ + 3 is ‘d’ ‘d’ - (‘a’ - ‘A’) is ‘D’ convert lower to upper case int is the usual type for decimal integers

Sizeof Operator int i; ‘declare an integer variable called i’ sizeof(int) -Gives 4 sizeof(i) - Gives 4 Additional Data Types long int8 bytes (2 63 – 1); greater range short int 2 bytes; less storage unsigned int 4 bytes; greater range, positive numbers

* typical values - depends on system Integer Data Types

Typical Character Codes

Real data types Also called floating point fractional numbers8.231 powers of 106 x x 10 8 have 2 attributes precision number of significant figures rangelowest and highest number trade off between precision and range

Real data types * typical values - system dependent!!

Formatted input: scanf scanf scans in or reads input the input is stored in one or more variables we need to tell scanf the type of the information being input, and the name of the variable to store it in

Formatted input - scanf char input1; float input2; scanf("%c %f", &input1, &input2); scanf(, ) one conversion code for each variable in list. use double quotes around format string list of variables to be input (any number), separated by commas & before numeric and character variable names

scanf conversion codes %cchar %ddecimal integer %ffloat %lfdouble %Lflong double %sstring the name of the string variable is not preceded by a &

Formatted output: printf char input1; float input2; printf("You entered %c and %f ", input1, input2); printf(, ) one conversion code for each variable in list use quotes around format string can intersperse with text to be output list of variables to be input (any number), separated by commas

printf conversion codes %ccharacterL %d decimal integer 76 %escientific notation e-02 %f floating point %ge- or f- format, whichever is shorter %ooctal integer094 %xhexadecimal integer0x4c %sstringI have a cat

Formatted output formatting can be done in the format string intersperse variables with text use spaces to separate output special characters \nnew line\” double quote \thorizontal tab\v vertical tab field width %3c, %3d, %3f3 characters wide %7.3f7 characters wide, 3 after decimal point

#include #define PI 3.14 int main(void) /* reads the radius of a circle and then calculates its area and circumference*/ { int radius; float circum,area; printf(“input radius: “); scanf(“%d”, &radius); circum=2*PI*radius; area=PI*radius*radius; printf(“\n\nresults:\n\n”); printf(“Circle of radius %d”,radius); printf(“\nCircumference %f and area %f\n”, circum,area); return 0; } Using formatted input & output

Common errors In printf and scanf the format string and variable lists must correspond. The format string in printf and scanf must have double quotes around it Don’t forget to use ‘&’ before integer and character variable names in scanf - but not strings!