16
Structure of ‘c’ Program Constants, Variables and Data-types A Computer Programming and Utility Project Created By: Shubham Thakur

Structure of ‘c’ Program

Embed Size (px)

DESCRIPTION

Describes basics, or just what you'd face in the beginning when you start how to construct a basic C-programming.

Citation preview

Page 1: Structure of ‘c’ Program

Structure of ‘c’ ProgramConstants, Variables and Data-types

A Computer Programming and Utility Project

Created By:Shubham Thakur

Page 2: Structure of ‘c’ Program

IntroductionData needs to be processed for manipulation

by user, and its response needs to be processed by the computer for our interaction.

This processing is via the machine language understood by the computer and its equivalent language that can be accessed by the humans.

Page 3: Structure of ‘c’ Program

Character SetCharacter grouping in C:

White spaces:Blank SpaceHorizontal TabCarriage ReturnNew lineForm Feed

Letters Digits Special Characters

A B C… Z 0 1 2 3 4 5 6 7 8 9

`~!@#$%^_+-=\/?

a b c… z () ][{}|<>:”;’,. &*

Page 4: Structure of ‘c’ Program

Tri-graph Characters

??= # ??( [ ??) ]

??< { ??> } ??! |

??/ \ ??\ ^ ??- ~

Tri-graph Characters: When some symbols are combined (in 3 in left) gives a different output (right) shown in the table.

Page 5: Structure of ‘c’ Program

C Tokens

Keywords

autobreakcaseconst

Identifiers

X999_S93num

g_nd45

Constants

Numeric Constants

Integer Constan

ts

+123199-0330XA3

Real Constan

ts

0.082-9.5+.32

1.5e+5

Character Constants

Single characte

r Constan

ts

‘5’‘e’‘ ‘‘X’

String Constan

ts

‘\a’‘\n’

‘\?’ ‘\0’‘\\’

Strings

“3Ap ple”

“Black”“99” “@#””B49”

Special Symbols

{}:””_@#)

Operators

=+-/!<

<< >>

Page 6: Structure of ‘c’ Program

Syntax#include<stdio.h> //special symbol (#<>)

<headerfile>double answer=0; //keyword

identifier=numeric constantchar name=“apple”; //keyword

identifier=“string”a=b*c; //variable(a)

operator(=,*) constant (b,c)As given in the next example, the words in

white, such as void, int, float etc. are the keywords.

The words in green like main, printf scanf are the strings.

‘a’ is declared as a variable and ‘i’ is an integer constant having assigned the value 0.

Page 7: Structure of ‘c’ Program

Example

Page 8: Structure of ‘c’ Program

VariablesRules:

Begin with letter or an underscore(_);Many compilers approve only first eight letters

of the variable significant, or are read by the compiler.

Cases (up/low) are considered.Must not be a keyword.White spaces aren’t allowed.

Valid variables:_my_name, Void, doublE, iNT, i99, _98, NaMe,

naMe, Name, etc. are different.Invalid variables:

int, float, 99i, my name, 9_8, Na me, 99, 10th, %, price$, n&m, etc.

Page 9: Structure of ‘c’ Program

Data typesANSI C supports 3 classes of data types:Primary/fundamental data types:

Integer type (int), Character type (char), Floating point (float), Double precision floating point (double), void – void type has no values; used to specify types of

functions. Function is void if it returns no value to the calling function, it can also represent any of he standard types.

Derived Data types: long int, long double

User defined data types: Using typedef (keyword), Using enum (keyword).

Page 10: Structure of ‘c’ Program

Declaration of VariablesPrimary type

declaration:data-type v1,v2,…vn;Example:int count;int number, total;double ratio;int and double representsinteger & real type data values.

User Defined type declaration typedef type identifier;Example:typedef int units;typedef float marks;units batch1, batch2;marks m1, m2;Here, units replaces int and marks replaces float.

User Defined type

(enum)enum identifier

{v1,v2…vn};Example:enum day

{M=1,T,W,Th,F,S,Su};

enum day week_st, week_end;

week_st=M;week_end=F;if(week_st==T)Week_end=S;The compiler assigns

T=2, W=3… following M=1.

Page 11: Structure of ‘c’ Program

Declaration of Storage classThe storage class decides the

portion of the program within which the variables are recognized.

Example:int m;main(){int i;float bal;

The variable declared as m is the global variable, because in all the programs within the main will share the same value of m, as it is globally declared outside main(), whereas the ‘i’, ‘bal’ are the local variable as they are declared inside the main function.

There are 4 storage class

specifier:extern: global variable

known to all functions in the file

static: local variable that exists and retains its value even after the control is transferred to the calling function

auto: local variable known only to the function in which it is declared.

register: local variable which is stored in the register.

• Example:static int x;extern long total;register char ch;auto int count;

Page 12: Structure of ‘c’ Program

Assigning values to the variables//The copying of one value or variable to another variable is called

assigning.//Assignment operator (=) is used for such operation, carried out from

right to left.//Example:

char c[40]=“Hello I am a student!”; //’c’ has string length of 40 and has copied “Hello…”b=40; //’40’ value is copied in ‘b’a=b; //value of ‘b’ is copied by ‘a’, or ‘a’ holds the value ‘40’a=90-b; //’40’ value in a is erased and now the value of ‘90-b’ is copied

in ‘a’printf(“%d=a”,a); //50 is printeda=b=30; //’30’ is copied to b, value of b is copied to a;a=b=20=50; //illegal assignmenta=a+1; //the value ‘a’ in right sums with 1 and then is copied to ‘a’ in

left

Page 13: Structure of ‘c’ Program

Reading data from keyboard/*Some data has to be sent to the compiler for its computation, such

intake of values can be done by the syntax:*/int roll; float marks; char grade, name; // local declarationprintf(“Enter Roll. No., name, marks out of 50 and grade of the

student:\n”);scanf(“%d %s %f %c”,&roll,name,&marks,grade);printf(“\nnext comes the data accpted by the compiler:\n”);printf(“%d\n%s\t%f\t%c”,roll,name,marks,grade);/*printf function helps user to interact with the compiler, about the

sequence of entering the data, and sequentially it is accepted by the compiler using the scanf function.*/

/* output:Enter Roll. No., name, marks and grade of the student:50 Alex 45.003 Anext comes the data accepted by the compiler:50Alex 45.003 A*/

Page 14: Structure of ‘c’ Program

Declaring symbolic constants//declaration of symbolic constants are preferred to be mentioned at the start of any program

#define MAX 25 //generally identified with the capitalized letters

#define PI 3.14 //used when some constants are globally accepted and fixed

#define num 31.4 //it’s not wrong to write in lower case letters

#define Min 10 //but upper case letters are preferred

//invalid declarations are:

#Define M AX 25 //white space not allowed plus ‘D’ should be ‘d’ in #define…

#define MAX$ 25; //no symbols and no semicolon in the end

#define MAX 25, MIN 10 //2 declarations within one #define is illegal

#define Zero=0 //assignment operator should not be used

Page 15: Structure of ‘c’ Program

Declaring variables as constants

/*some variables if needed can be made constant throughout the program and hence they will not be modified anymore using const keyword*/

const int class_size=40; //the integer variable ‘class_size’ is declared constant

int a=b=30; //’a’, ‘b’ can be modifieda=class_size; //’a’ value is changed

from 40 to 30class_size=b; //expression becomes

illegal//class_size can now only be used as the right

operand for any operation

Page 16: Structure of ‘c’ Program

Declaring variables as volatile

/*Using the keyword volatile, it can be told to the compiler that the variable’s value can be changed anywhere/anytime in the program by the external sources outside the program*/

volatile int date=40; //date is made volatile/*when volatile is declared on a variable, then the compiler

checks the value of date (variable) every time it encounters eternal interruption*/

/*volatile declaration also lets the same program to change the value of the variable. If it is required that only external sources can modify the variable, and not by the same program, we use const keyword.*/

volatile const int location=100;