Clojure – Decision Making ”; Previous Next Decision-making structures require that the programmer specify one or more conditions to be evaluated or tested by the program, along with a statement or statements to be executed if the condition is determined to be true, and optionally, other statements to be executed if the condition is determined to be false. Sr.No. Methods & Description 1 If Statement In Clojure, the condition is an expression which evaluates it to be either true or false. ”If” the condition is true, then statement#1 will be executed, else statement#2 will be executed. 2 If/do Expression The ‘if-do’ expression in Clojure is used to allow multiple expressions to be executed for each branch of the ‘if’ statement. 3 Nested If Statement Multiple ”if” statements embedded inside each other. 4 Case Statement Clojure offers the ‘case’ statement which is similar to the ‘switch’ statement available in the Java programming language. 5 Cond Statement Clojure offers another evaluation statement called the ‘cond’ statement. This statement takes a set of test/expression pairs. Print Page Previous Next Advertisements ”;
Category: Computer Programming
Clojure – REPL
Clojure – REPL ”; Previous Next REPL (read-eval-print loop) is a tool for experimenting with Clojure code. It allows you to interact with a running program and quickly try out if things work out as they should. It does this by presenting you with a prompt where you can enter the code. It then reads your input, evaluates it, prints the result, and loops, presenting you with a prompt again. This process enables a quick feedback cycle that isn’t possible in most other languages. Starting a REPL Session A REPL session can be started in Leiningen by typing the following command in the command line. lein repl This will start the following REPL window. You then start evaluating Clojure commands in the REPL window as required. To start a REPL session in Eclipse, click the Menu option, go to Run As → Clojure Application. This will start a new REPL session in a separate window along with the console output. Conceptually, REPL is similar to Secure Shell (SSH). In the same way that you can use SSH to interact with a remote server, Clojure REPL allows you to interact with a running Clojure process. This feature can be very powerful because you can even attach a REPL toa live production app and modify your program as it runs. Special Variables in REPL REPL includes some useful variables, the one widely used is the special variable *1, *2, and *3. These are used to evaluate the results of the three most recent expressions. Following example shows how these variables can be used. user => “Hello” Hello user => “World” World user => (str *2 *1) HelloWorld In the above example, first two strings are being sent to the REPL output window as “Hello” and “World” respectively. Then the *2 and *1 variables are used to recall the last 2 evaluated expressions. Print Page Previous Next Advertisements ”;
Clojure – Loops
Clojure – Loops ”; Previous Next So far we have seen statements which are executed one after the other in a sequential manner. Additionally, statements are provided in Clojure to alter the flow of control in a program’s logic. They are then classified into flow of control statements which we will see in detail. Sr.No. Loops & Description 1 While Statement The ”while” statement is executed by first evaluating the condition expression (a Boolean value), and if the result is true, then the statements in the while loop are executed. 2 Doseq Statement The ‘doseq’ statement is similar to the ‘for each’ statement which is found in many other programming languages. The doseq statement is basically used to iterate over a sequence. 3 Dotimes Statement The ‘dotimes’ statement is used to execute a statement ‘x’ number of times. 4 Loop Statement The loop special form is not like a ‘for’ loop. The usage of loop is the same as the let binding. However, loop sets a recursion point Print Page Previous Next Advertisements ”;
Clojure – Variables
Clojure – Variables ”; Previous Next In Clojure, variables are defined by the ‘def’ keyword. It’s a bit different wherein the concept of variables has more to do with binding. In Clojure, a value is bound to a variable. One key thing to note in Clojure is that variables are immutable, which means that in order for the value of the variable to change, it needs to be destroyed and recreated again. Following are the basic types of variables in Clojure. short − This is used to represent a short number. For example, 10. int − This is used to represent whole numbers. For example, 1234. long − This is used to represent a long number. For example, 10000090. float − This is used to represent 32-bit floating point numbers. For example, 12.34. char − This defines a single character literal. For example, ‘/a’. Boolean − This represents a Boolean value, which can either be true or false. String − These are text literals which are represented in the form of chain of characters. For example, “Hello World”. Variable Declarations Following is the general syntax of defining a variable. Syntax (def var-name var-value) Where ‘var-name’ is the name of the variable and ‘var-value’ is the value bound to the variable. Example Following is an example of variable declaration. (ns clojure.examples.hello (:gen-class)) ;; This program displays Hello World (defn Example [] ;; The below code declares a integer variable (def x 1) ;; The below code declares a float variable (def y 1.25) ;; The below code declares a string variable (def str1 “Hello”) ;; The below code declares a boolean variable (def status true)) (Example) Naming Variables The name of a variable can be composed of letters, digits, and the underscore character. It must begin with either a letter or an underscore. Upper and lowercase letters are distinct because Clojure, just like Java is a case-sensitive programming language. Example Following are some examples of variable naming in Clojure. (ns clojure.examples.hello (:gen-class)) ;; This program displays Hello World (defn Example [] ;; The below code declares a Boolean variable with the name of status (def status true) ;; The below code declares a Boolean variable with the name of STATUS (def STATUS false) ;; The below code declares a variable with an underscore character. (def _num1 2)) (Example) Note − In the above statements, because of the case sensitivity, status and STATUS are two different variable defines in Clojure. The above example shows how to define a variable with an underscore character. Printing variables Since Clojure uses the JVM environment, you can also use the ‘println’ function. The following example shows how this can be achieved. Example Live Demo (ns clojure.examples.hello (:gen-class)) ;; This program displays Hello World (defn Example [] ;; The below code declares a integer variable (def x 1) ;; The below code declares a float variable (def y 1.25) ;; The below code declares a string variable (def str1 “Hello”) (println x) (println y) (println str1)) (Example) Output The above program produces the following output. 1 1.25 Hello Print Page Previous Next Advertisements ”;
C Library –
C library – <inttypes.h> ”; Previous Next The C library header <inttypes.h> provides various macros for formatting and working with numeric data type(int). These macros are useful when we are using printf() and scanf() function. Library Macros Here, we are going to explore some important key macros of inttypes.h − Macros of output format specifier − printf() function PRIxMAX: This is printf specifier for intmax_t which is equivalent to %x(hexadecimal). PRIiMAX: This is printf specifier for intmax_t which is equivalent to %i(integer). PRIuLEAST32: This is printf specifier for uint_least32_t which is equivalent to %u(unsigned). Macros of input format specifier − scanf() function Similar to printf() function, here are some macros which is used for reading input− SCNxMAX: This is scanf specifier for intmax_t which is equivalent to %x(hexadecimal). SCNiMAX: This is scanf specifier for intmax_t which is equivalent to %i(integer). SCNuLEAST32: This is scanf specifier for uint_least32_t which is equivalent to %u(unsigned). String function Macros Below are list of some macros which supports string function − strtoimax(str, endptr, base): It is equivalent to strtol() for intmax_t. strtoumax(str, endptr, base): It is equivalent to strtoul() for uintmax_t. wcstoimax(wcs, endptr, base): It is equivalent to wcstol() for intmax_t. wcstoumax(wcs, endptr, base): It is equivalent to wcstoul() for uintmax_t. Example 1 Following is C library header <inttypes.h> which provide the program for printing integer value with a fixed width. #include <stdio.h> #include <inttypes.h> int main() { int32_t num1 = 67543; int64_t num2 = 894432590; printf(“Formatted output using inttypes.h:n”); printf(“num1: %” PRId32 “n”, num1); printf(“num2: %” PRId64 “n”, num2); return 0; } Output On execution of above code, we get the following result − Formatted output using inttypes.h: num1: 67543 num2: 894432590 Example 2 Below the program calculate the factorial of a number using unintmax_t under the header inttypes.h. #include <stdio.h> #include <inttypes.h> uintmax_t factorial(uintmax_t n) { if (n == 0) return 1; else return n * factorial(n – 1); } int main() { uintmax_t num = 5; printf(“Factorial of %” PRIuMAX ” = %” PRIuMAX “n”, num, factorial(num)); return 0; } Output After executing the code, we get the following result − Factorial of 5 = 120 Example 3 Below the program reads a 64-bit unsigned integer from the user using SCNu64. It then prints the entered value using PRIu64. #include <stdio.h> #include <inttypes.h> int main() { uint64_t large_number = 9876543210; printf(“The entered number: %” PRIu64 “n”, large_number); return 0; } Output The above code produces the following result − The entered number: 9876543210 Print Page Previous Next Advertisements ”;
C Library –
C library – <stdbool.h> ”; Previous Next The C library <stdbool.h> header supports the bool data types. The bool can store the value as true(0) or false(1) which is common requirement in various programming languages. There are three ways to perform the implementation of this header − The stdbool.h − This is C header which support the boolean variable. The Enumeration (enum) type − This is special type of data which is defined by the user. This includes either integral constant or integer. Declare the boolean values − The value can be defined as true or false. Example 1 Following is the simple C library header <stdbool> to see the conversion of boolean value in integer form. #include <stdbool.h> #include <stdio.h> int main() { // Declaration of boolean data types bool x = true; bool y = false; printf(“True : %dn”, x); printf(“False : %d”, y); return 0; } Output The above code produces the following output − True : 1 False : 0 Example 2 Below the program create an enumeration(enum) type to represent boolean values explicitly. #include <stdio.h> enum Bool { FALSE, TRUE }; int main() { enum Bool isTrue = TRUE; enum Bool isFalse = FALSE; // Rest of your code… printf(“isTrue: %dn”, isTrue); printf(“isFalse: %dn”, isFalse); // Rest of your program logic… return 0; } Output The above code produces the following output − isTrue: 1 isFalse: 0 Example 3 Here, we directly declare the boolean value using integer constants which shows the 0 for false and 1 for true. #include <stdio.h> int main() { int isTrue = 1; // true int isFalse = 0; // false printf(“isTrue: %dn”, isTrue); printf(“isFalse: %dn”, isFalse); return 0; } Output The above code produces the following output − isTrue: 1 isFalse: 0 Print Page Previous Next Advertisements ”;
C Library –
C Library – <signal.h> ”; Previous Next The signal.h header defines a variable type sig_atomic_t, two function calls, and several macros to handle different signals reported during a program”s execution. Library Variables Following is the variable type defined in the header signal.h − Sr.No. Variable & Description 1 sig_atomic_t This is of int type and is used as a variable in a signal handler. This is an integral type of an object that can be accessed as an atomic entity, even in the presence of asynchronous signals. Library Macros Following are the macros defined in the header signal.h and these macros will be used in two functions listed below. The SIG_ macros are used with the signal function to define signal functions. Sr.No. Macro & Description 1 SIG_DFL Default signal handler. 2 SIG_ERR Represents a signal error. 3 SIG_IGN Signal ignore. The SIG macros are used to represent a signal number in the following conditions − Sr.No. Macro & Description 1 SIGABRT Abnormal program termination. 2 SIGFPE Floating-point error like division by zero. 3 SIGILL Illegal operation. 4 SIGINT Interrupt signal such as ctrl-C. 5 SIGSEGV Invalid access to storage like segment violation. 6 SIGTERM Termination request. Library Functions Following are the functions defined in the header signal.h − Sr.No. Function & Description 1 void (*signal(int sig, void (*func)(int)))(int) This function sets a function to handle signal i.e. a signal handler. 2 int raise(int sig) This function causes signal sig to be generated. The sig argument is compatible with the SIG macros. Print Page Previous Next Advertisements ”;
C Library –
C Library – <stdio.h> ”; Previous Next The stdio.h header defines three variable types, several macros, and various functions for performing input and output. Library Variables Following are the variable types defined in the header stdio.h − Sr.No. Variable & Description 1 size_t This is the unsigned integral type and is the result of the sizeof keyword. 2 FILE This is an object type suitable for storing information for a file stream. 3 fpos_t This is an object type suitable for storing any position in a file. Library Macros Following are the macros defined in the header stdio.h − Sr.No. Macro & Description 1 NULL This macro is the value of a null pointer constant. 2 _IOFBF, _IOLBF and _IONBF These are the macros which expand to integral constant expressions with distinct values and suitable for the use as third argument to the setvbuf function. 3 BUFSIZ This macro is an integer, which represents the size of the buffer used by the setbuf function. 4 EOF This macro is a negative integer, which indicates that the end-of-file has been reached. 5 FOPEN_MAX This macro is an integer, which represents the maximum number of files that the system can guarantee to be opened simultaneously. 6 FILENAME_MAX This macro is an integer, which represents the longest length of a char array suitable for holding the longest possible filename. If the implementation imposes no limit, then this value should be the recommended maximum value. 7 L_tmpnam This macro is an integer, which represents the longest length of a char array suitable for holding the longest possible temporary filename created by the tmpnam function. 8 SEEK_CUR, SEEK_END, and SEEK_SET These macros are used in the fseek function to locate different positions in a file. 9 TMP_MAX This macro is the maximum number of unique filenames that the function tmpnam can generate. 10 stderr, stdin, and stdout These macros are pointers to FILE types which correspond to the standard error, standard input, and standard output streams. Library Functions Following are the functions defined in the header stdio.h − Sr.No. Function & Description 1 int fclose(FILE *stream) Closes the stream. All buffers are flushed. 2 void clearerr(FILE *stream) Clears the end-of-file and error indicators for the given stream. 3 int feof(FILE *stream) Tests the end-of-file indicator for the given stream. 4 int ferror(FILE *stream) Tests the error indicator for the given stream. 5 int fflush(FILE *stream) Flushes the output buffer of a stream. 6 int fgetpos(FILE *stream, fpos_t *pos) Gets the current file position of the stream and writes it to pos. 7 FILE *fopen(const char *filename, const char *mode) Opens the filename pointed to by filename using the given mode. 8 size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream) Reads data from the given stream into the array pointed to by ptr. 9 FILE *freopen(const char *filename, const char *mode, FILE *stream) Associates a new filename with the given open stream and same time closing the old file in stream. 10 int fseek(FILE *stream, long int offset, int whence) Sets the file position of the stream to the given offset. The argument offset signifies the number of bytes to seek from the given whence position. 11 int fsetpos(FILE *stream, const fpos_t *pos) Sets the file position of the given stream to the given position. The argument pos is a position given by the function fgetpos. 12 long int ftell(FILE *stream) Returns the current file position of the given stream. 13 size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream) Writes data from the array pointed to by ptr to the given stream. 14 int remove(const char *filename) Deletes the given filename so that it is no longer accessible. 15 int rename(const char *old_filename, const char *new_filename) Causes the filename referred to, by old_filename to be changed to new_filename. 16 void rewind(FILE *stream) Sets the file position to the beginning of the file of the given stream. 17 void setbuf(FILE *stream, char *buffer) Defines how a stream should be buffered. 18 int setvbuf(FILE *stream, char *buffer, int mode, size_t size) Another function to define how a stream should be buffered. 19 FILE *tmpfile(void) Creates a temporary file in binary update mode (wb+). 20 char *tmpnam(char *str) Generates and returns a valid temporary filename which does not exist. 21 int fprintf(FILE *stream, const char *format, …) Sends formatted output to a stream. 22 int printf(const char *format, …) Sends formatted output to stdout. 23 int sprintf(char *str, const char *format, …) Sends formatted output to a string. 24 int vfprintf(FILE *stream, const char *format, va_list arg) Sends formatted output to a stream using an argument list. 25 int vprintf(const char *format, va_list arg) Sends formatted output to stdout using an argument list. 26 int vsprintf(char *str, const char *format, va_list arg) Sends formatted output to a string using an argument list. 27 int fscanf(FILE *stream, const char *format, …) Reads formatted input from a stream. 28 int scanf(const char *format, …) Reads formatted input from stdin. 29 int sscanf(const char *str, const char *format, …) Reads formatted input from a string. 30 int fgetc(FILE *stream) Gets the next character (an unsigned char) from the specified stream and advances the position indicator for the stream. 31 char *fgets(char *str, int n, FILE *stream) Reads a line from the specified stream and stores it into the string pointed to by str. It stops when either (n-1) characters are read, the newline character is read, or the end-of-file is reached, whichever comes first. 32 int fputc(int char, FILE *stream) Writes a character (an unsigned char) specified by the argument char to the specified stream and advances the position indicator for the stream. 33 int fputs(const char *str, FILE *stream) Writes a string to the specified stream up to but not including the null character. 34 int getc(FILE *stream) Gets the next character (an unsigned char) from the specified stream and advances the position indicator for the stream. 35 int getchar(void) Gets a character (an
C Programming – Tutorial
C Tutorial Table of content C Tutorial Why to Learn C Programming? Facts about C Hello World using C Programming Applications of C Programming C Audiences C Prerequisites FAQs on C Programming PDF Version Quick Guide Resources Job Search Discussion C Tutorial C programming is a general-purpose, procedural, imperative computer programming language developed in 1972 by Dennis M. Ritchie at the Bell Telephone Laboratories to develop the UNIX operating system. C is the most widely used computer language. It keeps fluctuating at number one scale of popularity along with Java programming language, which is also equally popular and most widely used among modern software programmers. Why to Learn C Programming? C programming language is a MUST for students and working professionals to become a great Software Engineer specially when they are working in Software Development Domain. Here are some of the important reasons why you should learn C Programming − It is a structured programming language and you can use the skills learned in C to master other programming languages. You can use C program to write efficient codes and develop robust projects. C is a low-level language and you can use it to interact more directly with the computer”s hardware and memory. Facts about C C is the most widely used and popular System Programming Language. Most of the state-of-the-art software have been implemented using C. Here are some facts about the C language: C was invented to write an operating system called UNIX. The UNIX OS was totally written in C. C is a successor of B language which was introduced around the early 1970s. The language was formalized in 1988 by the American National Standard Institute (ANSI). Hello World using C Programming Just to give you a little excitement about C programming, I”m going to give you a small conventional C Programming Hello World program. You can run it here using the “Edit and Run” button. #include <stdio.h> int main() { /* my first program in C */ printf(“Hello, World! n”); return 0; } Applications of C Programming C was initially used for system development work, particularly the programs that make-up the operating system. C was adopted as a system development language because it produces code that runs nearly as fast as the code written in assembly language. Some examples of the use of C are – Operating Systems Language Compilers Assemblers Text Editors Print Spoolers Network Drivers Modern Programs Databases Language Interpreters Utilities Audience This tutorial is designed for software programmers with a need to understand the C programming language starting from scratch. This C tutorial will give you enough understanding on C programming language from where you can take yourself to higher level of expertise. Prerequisites Before proceeding with this tutorial, you should have a basic understanding of Computer Programming terminologies. A basic understanding of any of the programming languages will help you in understanding the C programming concepts and move fast on the learning track. FAQs on C Programming There are some very Frequently Asked Questions(FAQ) about C, this section tries to answer them briefly. Is C Programming still relevant today? C programming came into being in 1972. After more than 5 decades, C is still one of the most popular languages that ranks consistently in the top three. Since C can directly interact with the hardware, it is primarily used in low-level applications such as building operating systems, device drivers, embedded systems, networking etc. Therefore, C programming skills are very much in demand, in this age also. One’s career prospects will definitely brighter if he has a good proficiency in C programming. What are the applications of C Programming? C is a general-purpose programming language; therefore, it can be used to develop any type of applications. However, its ability to interact with the hardware makes it more suitable for developing system utilities, compilers and device drivers. C is predominantly used in building embedded systems and networking applications. C is significantly faster as compared to languages like Java or Python because it is directly compiled to the machine code. Hence, it is used in development of gaming applications. C is a versatile programming language that can be useful in development of a variety of software applications. Is C Programming difficult to learn for beginners? C is considered to be one of the easiest programming languages to learn for beginners. You can learn programming in C with the help of many online resources, such as the C tutorial provided by TutorialsPoint cprogramming. C does have a slightly steeper learning curve when you go towards advanced concepts. For attaining a high level of proficiency in C, you need to be able to master the features such as pointers, structures etc. Learning C allows you to build sound foundation with which you can easily learn other programming technologies. What are the advantages of learning C Programming? Here are some of the main advantages of learning C programming: C is a compiled language. It is translated directly into the machine language. That’s why the code execution is faster. Thus C offers better efficiency as compared to Java, Python. This fature is advantageous in applications like system utilities, embedded systems game development etc. C is a general-purpose language. Hence, it can be used to develop a variety of applications. C code easily portable. C compilers are available on all the operating system platforms. Hence, you can build an executable on relevant
C Library –
C Library – <errno.h> ”; Previous Next The errno.h header file of the C Standard Library defines the integer variable errno, which is set by system calls and some library functions in the event of an error to indicate what went wrong. This macro expands to a modifiable lvalue of type int, therefore it can be both read and modified by a program. The errno is set to zero at program startup. Certain functions of the standard C library modify its value to other than zero to signal some types of error. You can also modify its value or reset to zero at your convenience. The errno.h header file also defines a list of macros indicating different error codes, which will expand to integer constant expressions with type int. Library Macros Following are the macros defined in the header errno.h − Sr.No. Macro & Description 1 extern int errno This is the macro set by system calls and some library functions in the event of an error to indicate what went wrong. 2 EDOM Domain Error This macro represents a domain error, which occurs if an input argument is outside the domain, over which the mathematical function is defined and errno is set to EDOM. 3 ERANGE Range Error This macro represents a range error, which occurs if an input argument is outside the range, over which the mathematical function is defined and errno is set to ERANGE. Print Page Previous Next Advertisements ”;