0% found this document useful (0 votes)
0 views

Lecture Notes C (#1-#6)

The document provides an introduction to programming in C, covering topics such as high-level vs low-level languages, the structure of a C program, and the role of compilers and interpreters. It discusses key concepts like data types, variables, operators, and input/output statements, along with practical examples. Additionally, it explains the use of header files and macros in C programming, as well as the compilation process using the GCC compiler.

Uploaded by

rofi.lcbc
Copyright
© © All Rights Reserved
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
0 views

Lecture Notes C (#1-#6)

The document provides an introduction to programming in C, covering topics such as high-level vs low-level languages, the structure of a C program, and the role of compilers and interpreters. It discusses key concepts like data types, variables, operators, and input/output statements, along with practical examples. Additionally, it explains the use of header files and macros in C programming, as well as the compilation process using the GCC compiler.

Uploaded by

rofi.lcbc
Copyright
© © All Rights Reserved
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 8
‘2124, 4:17 PM Introduction to Programming in © - Google Docs Lecture Note Introduction to Programming in C FYUGP BSc-IT Semester - 1 Lecture No(s): #1-#3 Unit 1: Getting started with C programming (10 Lectures) Introduction to programming languages- High-level vs low level languages, compiled vs interpreted languages. Structure of a C rogram. Introduction to Header files. Main function and a simple ogram execution. Compiling and executing a program. C tokens keywords, identifiers, constants, operators. Statements and expressions in C. Basic data types in C - integers, floats, doi ange of values of data types. Variables. Constants - integer constant, veal constant, character constant, string constant. Declaration and initialization of variables and constants. Assigning values to variables. Operators in C - binary and characters. Void. Size and unary operators. Arithmetic, assignment, logical, comparison, bitwise and conditional operators. Order of precedence of operators Associativity of operators. Input and output statements - getchar(), gete(), getch(), putchar(), pute(), puts(), seant(), printf (), format specifiers. Typecasting —_—EEeeeeeee Programming guages are described in terms of their syntax (form) and semantics (meaning), usually defined by a formal lanquage. Languages usually provide fe as a type system, variables, and mechanisms for error handling. An implementation of a programming language is required in order to execute programs, namely an rpreter or a compiler. An interpreter directly exec code, while a compiler produces an executable program.e.g. C, Ctt Java, Python, Golang etc. A High-Level Language is a computer uses English like statements to write the computer most widely programming languages y to understand Lo human bein: Characteristics of high-level languages - er High-level languages Uhey are ea be easily ii evel L as well as comparison to low nguage ert considered as a programmer-friendly language. * It is easy to understand. © It is easy to debug © It is simple in terms of maintenance. err a compiler/interpreter to be translated into ewan Ao 1 al oeteeonat em hitpsildocs.google.comidocumentiavPAWSiw4RUIS48QTaUuWAgGP-CHSOAZXs_duJykVOMiediI7pl=1 48 ‘2124, 4:17 PM Introduction to Programming in © - Google Docs Lecture Note It can be run on different platfor * It can be ported from one location to another. * It is less memory efficient, i.e., it consumes more memory in arison to low-level languages. © Examples of high-level languages include C, Cit, Java, and Python. * It is widely used. A Low-Level Language is also a category nguage in which the computer codes are written in the binary language or machine codes. Because of this, low-level language is e language. The low-level language is ess friendly for human (programmer), but more friendly for machine of computer programming sometimes also kn because the computer processo in the low-level language. characteristics of a low-level language ~ can directly process the codes written * It can be understood easily by the machine. er chine-friendly Language. © It is difgicult to understand. © It is difficult to debug. * Its maintenance is also complex. © It is not portable * It depends on the machine; hence it can't be run on different platforms * Tt re: an assembler that would translate instructions. * Ibis used widely in today's times. High-level vs low level languages: S.No High-Level Language Low-Level Language It can be considered as a It is considered as a 1. | programmer-friendly language machine-friendly language It requires a compiler/interpreter | It requires an assembler to he translated into machine that would translate code instructions. It can be ported to another ewan Ao 2 al oeteeonat em hitpsildocs.google.comidocumentiavPAWSiw4RUIS48QTaUuWAgGP-CHSOAZXs_duJykVOMiediI7pl=1 ‘2124, 4:17 PM Introduction to Programming in © - Google Docs Lecture Note ‘ It is easy to understand. It is difficult to . understand. S.No High-Level Language Low-Level Language t is easy to debi It is difficult to debug. It is less memory efficient, i.e., | It consumes less memory. it consumes moze memory in comparison to low-level languages. Compiler : a compiler is a computer program that translates computer code written in one programming language (the source language) into another language (the target language). The name "compiler" is primarily used for programs thal translate source code from a high-level programming language to a low-level programming language (e.g. assembly language, object code, or machine code) to create an executable program. E.g. C, Ctt, Java etc use compilers. Interpreter interpreter is a computer program that directly executes instructions written in a programming or scripting language, without requiring them previously to have been compiled into a ma language program. E.g. PEP, Python, Ruby, JavaScript ete use compilers. Assignment #1.1: 1. Write the differences between Compiled languages vs interpreted languages. 2. Write the differences between High Level languages vs Low Level languages. Structure of a C program : A typical C program whatever its size consists of functions and variables. A function contains statements that specify the computing operations to be done, and variable store values used during the computation.C functions are like the subroutines and functions of Forton or the procedures of Pascal. Note: For more information refer to the block diagram from class. // Simple program to display “Hello World" // Header file for input output functions #include // Main function: entry point for execution int main() ¢ Disclatmer Those lecture notes ae not intended for any formal pblieations:These are to be used fr educational purposes ony eons Abn et 3 nat toleesionacer tips:ldoos.google-comidocumenti vPdWSiv4RUIS4BQVIaUuWAgGP-CHSOAZXS_dulykVOMVedit?pi=1 ‘2124, 4:17 PM Introduction to Programming in © - Google Docs Lecture Note // writing print statement to print hello world printf("Hello World"); return 5 © The main() function is a special function. Every C program begins executing at the beginning of the main(...) function. This means every program must have a main function somewhere. * main() will usually call other functions to help perform its job. Some are written by the programmers for themselves, some are from libraries. #includecstdio.h> i * This line known as “Header file inclusion” tells the compiler about the Standard input/output library. = Other libraries are also available. a7 © one method of communicating data between functions is for the calling function to provide a list of values, called arguments, to the function it calls.The parentheses after the function name surround the argument list. In the above example main() is defined to be a function that expects no arguments, which is indicated by the empty list (). The statements of a function are enclosed in braces {}. The function main() contains only two statements print#("Hello World"); return 0; * A function is called by naming it, followed by a parenthesized list of arguments, so this example calls the function printé( with the argument "Hello World” printf("Hello World"); Disclotmer Those lature notes ae not intended for any formal publieations:These are to be used for educational purposes ony cas Abn 2 nat toleesionacer tips:idoos.google-comidocumenti vPdWSiv4RUIS4BQVIaUuWAgGP-CHSOAZXS_dulykVOMVedit?pi=1 48 ‘2124, 4:17 PM Introduction to Programming in © - Google Docs Lecture Note Introduction to Programming in C FYUGP BSc-IT Semester - 1 Lecture No(s): #4-#6 Unit 1: Getting started with C programming (10 Lectures) Introduction to programming languages- High-level vs low level cture of a C iled vs languages. St languages, c gram. Introduction to He files. Main function and a simple gram execution. and ex © tokens - keywords, identifiers, constants, Statements and expressions in C, Basic data types in C floats, doub characters. Void nd range of values of data types. Variables Constants — ger constant, real constant, character constant, string cons Declaration and initialization of variables and constants. Assigning values to variables. Op: in C - binary and unary operators. Arithmetic, assignment, logical, comparison, bitwise and conditional ope: S. Order © ecedence of operators. Associativity of operators. Input and output statements - getchar(), gete(), getch(), putchar(), pute(), puts(), seanf(), pzint£(), format specifiers. Typecasting The C Processor: iin. -5 he © preprocessor, often known as cpp, is a macro processor that 5 used automatically by the C compiler to transform your program before compilation. It is called a macro processor because it allows programmers to define macros, which are brief abbreviations for longer construct: ves to be executed The preprocessing language consist and mac ties a © Inclusion of header files can be substituted into your program ‘os to be expanded. Its primary © Macro expansion: You can define macros, which are abbreviations for arbi rary fragments of C code. The preprocessor will replace the macros with their definitions throughout the program. Some macros are automatically defined for you. © Conditional compilation. You can include or exclude parts of the program accord: ous conditions. © Line control. If you use a program to combine or rearrange source files into an intermediate file which is then compiled, you can use line control to inform the compiler where each source line originally came from. ewan Ao 3 al oeteeonat em hitpsildocs.google.comidocumentiavPAWSiw4RUIS48QTaUuWAgGP-CHSOAZXs_duJykVOMiediI7pl=1 ‘2124, 4:17 PM Introduction to Programming in © - Google Docs Lecture Note * Diagnostics. You can detect problems at compile time and issue errors or warnings. Header files: A header file is a file containing C declarations and macro definitions (see Chapter 3 [Macros], page 13) to be shared between several source files. You request the use of a header file in your program by including it, with the C preprocessing directive ‘¥include’. Header files serve two purposes: « system header files declare the interfaces to parts of the operating system. You include them in your program to supply the definitions and declarations you need zo invoke system calls and libraries. #include // for example including standard input/output header file we write #include * Your own header (Custom) files contain declarations for interfaces between the source files of your program. Each time you have @ group of related declarations and macro definitions all or most of which are needed in several different source files, it is 2 good idea to create a header file for then. #include "file" // for example including our header file bscit.h (say) we write #include “bscit.h” Including a header file produces the same results as copying the header file into each source file that needs it. Such copying would be time-consuming and error-prone. With a header file, the related declarations appear in only one place. If they need to be changed, they can be changed in one place, and programs that include the header file will automatically use the new version when next recompiled. The header file eliminates the labor of finding and changing ali the copies as well as the risk that a failure to find one copy will result in inconsistencies within a program. In C, the usual convention is to give header files names that end with .h. It is most portable to use only letters, digits, dashes, and underscores in header file names, and at most one dot. A macro is a fragment of code which has been given a name. Whenever the name is used, it is replaced by the contents of the macro There are two kinds of macros. They differ mostly in what the: look like when they are used: © Object-like macros resemble data objects when used.An object-like macro is a simple identifier which will be replaced by a code fragment. It is called object-like because it looks like a data object in code that uses it. They are most commonly used te give symbolic names to numeric constants.You create macros with the ‘#define’ Disclotmer Those lature notes ae not intended for any formal publieations:These are to be used for educational purposes ony cas Abn 4 nat toleesionacer tips:idoos.google-comidocumenti vPdWSiv4RUIS4BQVIaUuWAgGP-CHSOAZXS_dulykVOMVedit?pi=1 28 ‘2124, 4:17 PM Introduction to Programming in © - Google Docs Lecture Note directive. ‘fdefine’ is followed by the name of the macro and then the token sequence it should be an abbreviation for, which is variously referred to as the macro’s body, expansion or replacement list. For example, fidefine BUFFER_SIZE 1024 defines a macro named BUFFER SIZE as an abbreviation for the token 1024. 7 * Function-like macros reseble function calls.A function-like macro is only expanded if its name appears with a pair of parentheses after it. If you write just the name, it is left alone. This can be useful when you have a function and a macro of the sane name, and you wish to use the function sometimes.Function-like macros can take arguments, just like true functions. As an example, here is a macro that computes the minimum of two numeric values, as it is defined in many C programs, and sone uses. Hdefine min(X, Y) ((x) < (¥) ? OX) : (YY) x = min(a, b)3 //+ x = ((a) < (b) ? (a) t (b)); y= min(t, 2); //> y = (1) < (2) ? (2) + (2))5 Z = min(a + 28, *p);//> z = ((a + 28)<(*p)?(a + 28):(*p)); vo * Be careful: dangers of macro arguments. y 1. By convention, macro names are written in uppercase. Programs are easier to read when it is possible to tell at a glance which 2, The macro’s body ends at the end of the ‘define’ line. You may continue the definition onto multiple lines, if necessary, using backslash-newline. Compiling and executing a program: The compilation is the process of converting the source code of the C language into machine code. As C is a mid-level language, it needs a compiler to convert it into an executable code so that the program can be run on our machine. The C€ program goes through the following phases during compilation: Disclamer: Those lcture notes are not intended for any formal publieations:These are to be used fr educational purposes ony eons Abn et 5 nat toleesionacer tips:ldoos.google-comidocumenti vPdWSiv4RUIS4BQVIaUuWAgGP-CHSOAZXS_dulykVOMVedit?pi=1 ‘2124, 4:17 PM Introduction to Programming in © - Google Docs Lecture Note We first need a compiler and a code editor to compile and run ac Program. The below example is of an Ubuntu machine with GCC compiler. Step-I: Creating a C Source File We first create a C program using an editor and save the file as filename.c $ vi filename.c Step IZ: Compiling using GCC compiler We use the following command in the terminal for compiling our filename.c source file $ gcc filename.c -o filename We can pass many instructions to the GCC compiler to different tasks such as: © The option -Wall enables all compiler’s warning messages. This option is recommended to generate better code. © The option ~o is used to specify the output file name. If we do not use this option, then an output file with the name a.cut is generated. If this option is not used by default an output file named a.out will be generated. If there are no errors in our © program, the executable file of the C program will be generated. Step III: Executing the program After compilation executable is generated and we run the generated executable using the below command. $ ./filename The program will be executed and the output will be shown in the terminal. Note: For writing and executing C program in Windows environment- Follow along the demonstrations in the smart classrooms.The instructor(s) encourage use of Windows Linux SubSystem/Dev C++ IDE/Code Blocks IDE with compilers like GCC/MinGW etc.For Mac, steps are almost similar to steps to be followed in *nix(e.g. Linux) systems Disclatmer Those lecture notes ae not intended for any formal pblieations:These are to be used fr educational purposes ony eons Abn et 6 nat toleesionacer tips:idoos.google-comidocumenti vPdWSiv4RUIS4BQVIaUuWAgGP-CHSOAZXS_dulykVOMVedit?pi=1

You might also like