1 / 156

UNIT II C PROGRAMMING BASICS

UNIT II C PROGRAMMING BASICS

kali
Télécharger la présentation

UNIT II C PROGRAMMING BASICS

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. UNIT II C PROGRAMMING BASICS • Problem formulation – Problem Solving - Introduction to ‘ C’ programming –fundamentals – structure of a ‘C’ program – compilation and linking processes – Constants, Variables – Data Types – Expressions using operators in ‘C’ – Managing Input and Output operations – Decision Making and Branching – Looping statements – solving simple scientific and statistical problems.

  2. Problem formulation • Effective problem formulation is fundamental to the success of all analysis, but particularly in Command and Control assessment because the problems are often ill-defined and complex, involving many dimensions and a rich context. • Problem formulation involves decomposition of the analytic problem into appropriate dimensions such as structures, functions, mission areas. • Problem formulation is an iterative process that evolves over the course of the study. • It is essential even for small studies or where time is short; it will save time later and help ensure quality.

  3. The problem formulation phase should identify the context of the study and aspects of the problem related issues. • There is no universally acceptable approach to problem formulation. However, best practices exist that can be applied. • First find out what the question is then find out what the real question is.

  4. PROBLEMSOLVING • When we starts reading these and wants to learn how to solve problem by using a computer, it is first of all important to understand what the problem is. • We need to read the problem statement a number of times to ensure that it understands what is asked before attempting to solve the problem. Method of Problem Solving • Recognize and understand the problem. • Accumulate facts. • Select appropriate theory. • Make necessary assumptions. • Solve the problem. • Verify results.

  5. Performing step 5 Solve the problem may involve a computer. • The 5 steps in using a computer as a problem-solving tool • Develop an Algorithm and a Flowchart. • Write the program in a computer language. • Enter the program into the computer. • Test and' debug the program. • Run the program, input data, and get the results from the computer

  6. Introduction to ‘ C’ programming • C was developed by Dennis Ritchie at Bell laboratory in 1972 • It is an upgrade version of languages B and BCPL. • Features of C • It is a structured programming language. • It is highly portable. • It is a middle level language. • It is a case sensitive language. • It uses Top-Down approach. • It is a Free form language.etc,.

  7. Steps in learning C Character Set Tokens Instructions Programs

  8. C Character Set C Character Set Source Character Set Execution Character Set Special Characters Alphabets Digits White Spaces Escape Sequence

  9. C Character Set (Cont) Source Character Set It is used to construct the statements in the program. Executable Character Set These characters are employed at the time of execution i.e. they have effects only when the program is being executed.

  10. Source Character Set

  11. Special characters Comma , Period or dot . Semicolon ; Colon : Apostrophe ‘ Quotation mark “ Exclamation mark ! Vertical bar | Back Slash \ Tilde ~ Underscore - Dollar $ Question mark ?

  12. Ampersand & Caret ^ Asterisk * Minus - Addition + Lesser than < Greater than > Parenthesis () Bracket [] Braces {} Percentage % Hash # Equal to = At the rate @

  13. Executable Character Set

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

  15. C Tokens (Cont) C Tokens Identifiers Eg:main, avg Keywords Eg: int, for Constants Eg:17, 15.5 Strings Eg: “ab” operators Eg: + - spI symbol Eg: # $ %

  16. Executing a C Program Creating the Program Compilation Linking Execution

  17. Executing a C Program (Cont) • Enter the program in a C editor. • Save the program (File  Save) or F2. Use the extension .c for saving the file. Eg: sample.c • Compile the program(Compile  Compile) or Alt+F9. • Run the program(Run  Run) or Ctrl+F9.

  18. Executing C program using UNIX • Enter the program in vi editor. • Save the file using :wq Use the extension .c for saving the file. Eg: sample.c • Compile the program. Eg: ccsample.c (or) gccsample.c • Run the program using a.out.

  19. Structure of C program DOCUMENTATION SECTION PREPROCESSOR SECTION DEFINITION SECTION GLOBAL DECLARATION SECTION main() { Declaration part; Executable Part; } sub program section { Body of the subprogram; }

  20. Documentation Section • It contains the comment lines. • Preprocessor Section • It is used to link library files. • Global Declaration Section • The Global declaration section comes at the beginning of the program and they are visible to all parts of the program. • Declaration Section • It describes the data to be used within the function. • Executable Part • It contains the valid statements.

  21. C Programs • C program may have many functions. • One and only one of the functions MUST BE named main. • main is the starting point for the program. • main and other functions in a program are divided into two sections, declaration section and statement section.

  22. Preprocessor Directives • Special instructions to the preprocessor that tells how to prepare the program for compilation • E.g: include : tells the processor to include information from selected libraries known as header files e.g. <stdio.h>

  23. Comments (Program documentation) • The compiler simply ignores comments when it translates the program into executable code. • To identify a comments, C uses opening /* and closing */ comment tokens.

  24. Comments (Cont) • Comments can appear anywhere in a program. • Comments are also found wherever it is necessary to explain a point about a code. • Comments cannot be nested in C i.e. you cannot have comments inside comments.

  25. C program /* Example program in C*/ Comments # include <stdio.h> Preprocessor Section Global Declaration void main () { Local declaration printf (“Hello World! \n”); Statements } Output : Hello World

  26. C Tokens • Identifiers • Keywords • Constants • Operators • Special symbols

  27. Identifiers • Identifiers are names given to various program elements such as variables, functions and arrays etc,. • Eg: #define N 10 #define a 15 Here N and a are user defined identifiers.

  28. Rules for naming identifier • First character must be alphabetic or underscore. • Must consist only of alphabetic characters, digits, or underscores. • Only the first 31 characters of an identifier are significant and are recognized by the compiler. • Cannot use a keywords or reserved word (e.g. main, include, printf & scanf etc.). • No space are allowed between the identifiers etc,. • C is case sensitive, e.g. My_name my_name.

  29. Examples of Valid and Invalid Names

  30. Variables • Variable is an identifier that is used to represent some specified type of information. • Eg: x=3 • Here x is variable.

  31. Keywords • reserved words. • Cannot be used for anything else. • Examples: • int • while • for etc,.

  32. Keywords Auto register Continue Double typedef For Int Char signed Struct extern void Break return Default Else union Goto Long Const sizeof Switch Float do Case short If Enum unsigned Static While

  33. Constants • It is an entity whose value does not changes during the execution. • Eg: x=3 • Here 3 is a constant.

  34. Types • Numeric constants • Character constant

  35. Constants Constants Numeric Constants Character Constants Integer Constant Real Constant Single Character Constant String Constant

  36. Numeric constants Integer constants • It is formed using a sequence of digits. Decimal - 0 to 9 . Octal - 0 to 7. Hexa - 0 to 9 ,A to F Eg: 10,75 etc.

  37. Rules for defining Integer Constant • It must have at least one digit. • Decimal point are not allowed. • No blank space or commas are allowed. • It can be either positive or negative. Etc,.

  38. Numeric constants Real constants • It is formed using a sequence of digits but it contain decimal point. • length, height, price distance measured in real number Eg: 2.5, 5.11, etc.

  39. Character constants Single character constant • A character constant is a single character they also represented with single digit or a single special symbol which is enclosed in single quotes. • Eg: ‘a’, ‘8’,’_’etc.

  40. Character constants String constants • String constant are sequence of characters enclosed with in double quote. • Eg: “Hello” ,”444”,”a” etc,.

  41. Operators • An operator is a symbol that specifies an operation to be performed on the operands. • Eg: a + b + is an operator. a,b are operands.

  42. Data Types • A Data type is the type of data that are going to access within the program.

  43. StandardData Types • These Standard type can be used to build more complex data types called Derived Types (e.g. pointers, array, union etc.).

  44. Data types

  45. integer • A number without a fraction part : integral number. • C supports three different sizes of the integer data type : • shortint • int • longint

  46. Floating Point • A floating-point type is a number with a fractional part, e.g. 56.78 • Floating point numbers are stored using 4 Byte. • Types • Float • Double • long double

  47. CharacterASCII code value a 97(decimal) or 01100001(binary) x 120(decimal) or 01111000(binary) character • Character are generally stored using 8 bits(1 Byte) of the internal storage.

  48. void • The void type has no values and no operations. • Both the set of values and the set of operations are empty.

  49. Variable’s Declaration • To create a variable, you must specify the type and then its identifier : float price; inta,b; • char code;

  50. Entire Data types in c:

More Related