Thursday, 28 July 2011

understanding structure of program,variables and operators in c

A simple c program

/* Purpose: display the message */


int main()

printf("%s"," welcome to c programming writting");


Anatomy of a C Program

/*program header comment*/

preprocessor directives (if any)

int main ( ) /*it is the main function, program execution begins from here*/
return 0 ;

Program Header Comment

• A comment is descriptive text used to help a
  reader of the program understand its
• All comments must begin with the characters
     /* and end with the characters */
• These are called comment delimiters
• The program header comment always
   comes first.

Preprocessor Directives
• Lines that begin with a # in column 1 are
  called preprocessor directives
• Example: the #include directive
  causes the preprocessor to include a copy of
  the standard input/output header file stdio.h at
  this point in the code.
• This header file was included because it
  contains information about the printf ( )
  function that is used in this program.
  When we write our programs, there are
  libraries of functions to help us so that we
  do not have to write the same code over
  and over again.
• Some of the functions are very complex
  and long. Not having to write them
  ourselves make it easier and faster to
  write programs.
• Using the functions will also make it easier
  to learn to program!

int main ( ) or void main()

• Every program must have a function called
  main. This is where program execution begins.
• main() is placed in the source code file as the
  first function for readability.
• The reserved word “int” indicates that main()
  returns an integer value.
• The parentheses following the reserved word
  “main” indicate that it is a function.
• The reserved word “void” means nothing is

The Function Body

• A left brace (curly bracket) -- { -- begins the
  body of every function. A corresponding
  right brace -- } -- ends the function body.
• The style is to place these braces on
  separate lines in column 1 and to indent the
  entire function body 3 to 5 spaces.

printf (“Hello, World!\n”) ;

• This line is a C statement.
• It is a call to the function printf ( ) with a
  single argument (parameter), namely the
  string “Hello, World!\n”.
• Even though a string may contain many
  characters, the string itself should be
  thought of as a single quantity.
• Notice that this line ends with a semicolon.
  All statements in C end with a semicolon.

return 0 ;

• Because function main() returns an integer value,
there must be a statement that indicates what this
value is.
• The statement
return 0 ;
indicates that main() returns a value of zero to
the operating system.
• A value of 0 indicates that the program successfully
terminated execution.
• Do not worry about this concept now. Just
remember to use the statement.


• The smallest element in the C language is
the token.
• It may be a single character or a sequence
of characters to form a single item.

Tokens are:
• Tokens can be:
– Numeric constants
– Character constants
– String constants
– Keywords
– Names (identifiers)
– Punctuation
– Operators

Numeric Constants

• Numeric constants are an uninterrupted
sequence of digits (and may contain a
period). They never contain a comma.
• Examples:
– 123
– 98.6
– 1000000

Character Constants

• One character from a defined character
• Surrounded on the single quotation mark.
• Examples:
– ‘A’
– ‘a’
– ‘$’
– ‘4’

String Constants
• A sequence characters surrounded by
  double quotation marks.
• Considered a single item.
• Examples:
– “Akv”
– “I like ice cream.”
– “123”
– “D-skY”

• Sometimes called reserved words.
• Are defined as a part of the C language.
• Can not be used for anything else!
• Examples:
– int
– while
– for


• Sometimes called identifiers.
• Can be of anything length, but on the first 31 are
  significant (too long is as bad as too short).
• Are case sensitive:
– abc is different from ABC
• Must begin with a letter and the rest can be
  letters, digits, and underscores.
• There can be one exception to beginning letter
  that variable name can start with underscore( _ )
  but it is used by C library.

• Semicolons, colons, commas,
apostrophes, quotation marks, braces,
brackets, and parentheses.
• ; : , ‘ “ [ ] { } ( )

• There are operators for:
– assignments
– mathematical operations
– relational operations
– Boolean operations
– bitwise operations
– shifting values
– calling functions
– subscripting
– obtaining the size of an object
– obtaining the address of an object
– referencing an object through its address

What Are Variables in C?
• Variables in C have the same meaning as
Variables in algebra. That is, they represent
Some unknown, or variable, value.
x = a + b
z + 2 = 3(y - 5)

• Remember that variables in algebra are
  represented by a single alphabetic

Naming Variables
• Variables in C may be given representations
  containing multiple characters. But there are
  rules for these representations.
• Variable names (identifiers) in C
– May only consist of letters, digits, and
– May be as long as you like, but only the first 31
   characters are significant
– May not begin with a digit
– May not be a C reserved word (keyword)

Reserved Words (Keywords) in C
• auto
• case
• const
• default
• double
• enum
• float
• goto
int long
register return
short signed
sizeof static
struct switch
typedef union
unsigned void
volatile while

Naming Conventions

• C programmers generally agree on the
following conventions for naming variables.
– Begin variable names with lowercase letters
– Use meaningful identifiers
– Separate “words” within identifiers with
underscores or mixed upper and lower case.
– Examples: surfaceArea surface_Area
– Be consistent!
Use all uppercase for symbolic constants
(used in #define preprocessor directives).
• Note: symbolic constants are not variables,
but make the program easier to read.
• Examples:
#define PI 3.14159
#define AGE 52

Case Sensitivity

• C is case sensitive
– It matters whether an identifier, such as a
variable name, is uppercase or lowercase.
– Example:
are all seen as different variables by the

Which Are Legal Identifiers?


Declaring Variables

• Before using a variable, you must give the
compiler some information about the variable;
i.e., you must declare it.
• The declaration statement includes the data
type of the variable.
• They must be declared just after the start of
block (i.e. start of a function) and before any
other executable statement.
• Examples of variable declarations:
int meatballs ;
float area ;

• When we declare a variable

– Space is set aside in memory to hold a value of
   the specified data type
– That space is associated with the variable name
– That space is associated with a unique address


Notes About Variables

• You must not use a variable until you
somehow give it a value.
• You can not assume that the variable will
have a value before you give it one.
– Some compilers do, others do not! This is the
source of many errors that are difficult to find.

Using Variables: Assignment

• Variables may have values assigned to them through
the use of an assignment statement.
• Such a statement uses the assignment operator =
• This operator does not denote equality. It assigns
the value of the right-hand side of the statement (the
expression) to the variable on the left-hand side.
• Examples:       
diameter = 5.9 ;
area = length * width ;
Note that only single variables (LValue) may appear
on the left-hand side of the assignment operator.

Getting Input from User

• Every process requires some input from
the user. Variables hold the input values.
• We have a function called scanf( ) that will
allow us to do that.
• The function scanf needs two pieces of
information to display things.
– The data type of input values
– Address where to store the values• scanf( “%f”, &diameter );

Displaying Variables

• Variables hold values that we occasionally
want to show the person using the
• We have a function called printf( ) that will
allow us to do that.
• The function printf needs two pieces of
information to display things.
– How to display it
– What to display• printf( “%f\n”, &diameter );

Both printf and scanf Returns a Value
• We can call printf as
• We also can call a scanf

check out the value of n & m if every thing goes fine?

Good Programming Practices

• Place each variable declaration on its own
   line with a descriptive comment.
• Place a comment before each logical
  “chunk” of code describing what it does.
• Do not place a comment on the same line as
   code (with the exception of variable
• Use spaces around all arithmetic and
   assignment operators.
• Use blank lines to enhance readability.
• Place a blank line between the last
  variable declaration and the first
  executable statement of the program.
• Indent the body of the program 3 to 5
  spaces -- be consistent!
• Comments should explain why you are
   doing something, not what you are doing
a = a + 1 /* add one to a */           /* WRONG */
              /* count new student */ /* RIGHT*/


  1. Oh my goodness! Amazing article dude! Thank you so much, However I am experiencing troubles
    with your RSS. I don't understand the reason why I can't join it.
    Is there anyone else having identical RSS issues?
    Anyone who knows the solution can you kindly respond?

    Also see my website - how to prevent acne scars after popping

  2. WOW just whаt I was looking for. Came heгe
    by searching for aerіal lift

    Here is my ρаge: Bucket Truck

  3. It is perfect time to make some plans for the future and it's time to be happy. I have read this post and if I could I want to suggest you few interesting things or suggestions. Maybe you could write next articles referring to this article. I desire to read more things about it!

    Also visit my website ... zulutrade

  4. Gгеat infο. Lucky mе I ran acrοss your blog by aссiԁеnt (stumbleupοn).

    I've book marked it for later!

    Here is my blog :: how to make money buying and selling cars at an auction

  5. I'm gone to say to my little brother, that he should also pay a quick visit this web site on regular basis to take updated from most recent reports.

    Here is my web site: seo consultant dallas tx