C Programming Tutorial

Total Page:16

File Type:pdf, Size:1020Kb

C Programming Tutorial C Programming Tutorial C PROGRAMMING TUTORIAL Simply Easy Learning by tutorialspoint.com tutorialspoint.com i COPYRIGHT & DISCLAIMER NOTICE All the content and graphics on this tutorial are the property of tutorialspoint.com. Any content from tutorialspoint.com or this tutorial may not be redistributed or reproduced in any way, shape, or form without the written permission of tutorialspoint.com. Failure to do so is a violation of copyright laws. This tutorial may contain inaccuracies or errors and tutorialspoint provides no guarantee regarding the accuracy of the site or its contents including this tutorial. If you discover that the tutorialspoint.com site or this tutorial content contains some errors, please contact us at [email protected] ii Table of Contents C Language Overview .............................................................. 1 Facts about C ............................................................................................... 1 Why to use C ? ............................................................................................. 2 C Programs .................................................................................................. 2 C Environment Setup ............................................................... 3 Text Editor ................................................................................................... 3 The C Compiler ............................................................................................ 3 Installation on Unix/Linux ............................................................................. 4 Installation on Mac OS .................................................................................. 4 Installation on Windows ............................................................................... 4 C Program Structure ................................................................ 5 C Hello World Example ................................................................................. 5 Compile & Execute C Program ....................................................................... 6 C Basic Syntax ......................................................................... 7 Tokens in C .................................................................................................. 7 Semicolons ; ................................................................................................ 7 Comments ................................................................................................... 8 Identifiers .................................................................................................... 8 Keywords .................................................................................................... 8 Whitespace in C ........................................................................................... 9 C Data Types ......................................................................... 10 Integer Types ............................................................................................. 10 Floating-Point Types ................................................................................... 11 The void Type ............................................................................................ 12 C Variables ............................................................................ 13 Variable Declaration in C ................................ Error! Bookmark not defined. Variable Initialization in C ............................... Error! Bookmark not defined. Lvalues and Rvalues in C ............................................................................. 15 C Constants and Literals ........................................................ 17 Integer literals ............................................................................................ 17 Floating-point literals .................................................................................. 18 Character constants .................................................................................... 18 iii String literals .............................................................................................. 19 Defining Constants ..................................................................................... 19 The #define Preprocessor ...................................................................... 19 The const Keyword ................................................................................. 20 C Storage Classes ................................................................. 22 The auto Storage Class ................................................................................ 22 The register Storage Class ........................................................................... 22 The static Storage Class ............................................................................... 23 The extern Storage Class ............................................................................. 24 C Operators ........................................................................... 25 Arithmetic Operators .................................................................................. 25 Relational Operators................................................................................... 26 Logical Operators ....................................................................................... 28 Bitwise Operators ....................................................................................... 29 Assignment Operators ................................................................................ 31 Misc Operators ↦ sizeof & ternary .............................................................. 33 Operators Precedence in C .......................................................................... 33 Decision Making in C.............................................................. 35 if statement ............................................................................................... 36 Syntax ..................................................................................................... 36 Flow Diagram .......................................................................................... 36 Example .................................................................................................. 36 if...else statement ...................................................................................... 37 Syntax ..................................................................................................... 37 Flow Diagram .......................................................................................... 38 Example .................................................................................................. 38 The if...else if...else Statement ..................................................................... 39 Syntax ..................................................................................................... 39 Example .................................................................................................. 39 Nested if statements .................................................................................. 40 Syntax ..................................................................................................... 40 Example .................................................................................................. 40 switch statement ....................................................................................... 41 Syntax ..................................................................................................... 41 Flow Diagram .......................................................................................... 42 Example .................................................................................................. 42 Nested switch statements ........................................................................... 43 Syntax ..................................................................................................... 43 Example .................................................................................................. 43 iii The ? : Operator ......................................................................................... 44 C Loops .................................................................................. 45 while loop in C ........................................................................................... 46 Syntax ..................................................................................................... 46 Flow Diagram .......................................................................................... 46 Example .................................................................................................. 47 for loop in C ............................................................................................... 47 Syntax ..................................................................................................... 47 Flow Diagram .......................................................................................... 48 Example .................................................................................................. 48 do...while loop in C ..................................................................................... 49 Syntax ..................................................................................................... 49 Flow Diagram .........................................................................................
Recommended publications
  • PRE PROCESSOR DIRECTIVES in –C LANGUAGE. 2401 – Course
    PRE PROCESSOR DIRECTIVES IN –C LANGUAGE. 2401 – Course Notes. 1 The C preprocessor is a macro processor that is used automatically by the C compiler to transform programmer defined programs before actual compilation takes place. It is called a macro processor because it allows the user to define macros, which are short abbreviations for longer constructs. This functionality allows for some very useful and practical tools to design our programs. To include header files(Header files are files with pre defined function definitions, user defined data types, declarations that can be included.) To include macro expansions. We can take random fragments of C code and abbreviate them into smaller definitions namely macros, and once they are defined and included via header files or directly onto the program itself it(the user defined definition) can be used everywhere in the program. In other words it works like a blank tile in the game scrabble, where if one player possesses a blank tile and uses it to state a particular letter for a given word he chose to play then that blank piece is used as that letter throughout the game. It is pretty similar to that rule. Special pre processing directives can be used to, include or exclude parts of the program according to various conditions. To sum up, preprocessing directives occurs before program compilation. So, it can be also be referred to as pre compiled fragments of code. Some possible actions are the inclusions of other files in the file being compiled, definitions of symbolic constants and macros and conditional compilation of program code and conditional execution of preprocessor directives.
    [Show full text]
  • KDE 2.0 Development, Which Is Directly Supported
    23 8911 CH18 10/16/00 1:44 PM Page 401 The KDevelop IDE: The CHAPTER Integrated Development Environment for KDE by Ralf Nolden 18 IN THIS CHAPTER • General Issues 402 • Creating KDE 2.0 Applications 409 • Getting Started with the KDE 2.0 API 413 • The Classbrowser and Your Project 416 • The File Viewers—The Windows to Your Project Files 419 • The KDevelop Debugger 421 • KDevelop 2.0—A Preview 425 23 8911 CH18 10/16/00 1:44 PM Page 402 Developer Tools and Support 402 PART IV Although developing applications under UNIX systems can be a lot of fun, until now the pro- grammer was lacking a comfortable environment that takes away the usual standard activities that have to be done over and over in the process of programming. The KDevelop IDE closes this gap and makes it a joy to work within a complete, integrated development environment, combining the use of the GNU standard development tools such as the g++ compiler and the gdb debugger with the advantages of a GUI-based environment that automates all standard actions and allows the developer to concentrate on the work of writing software instead of managing command-line tools. It also offers direct and quick access to source files and docu- mentation. KDevelop primarily aims to provide the best means to rapidly set up and write KDE software; it also supports extended features such as GUI designing and translation in con- junction with other tools available especially for KDE development. The KDevelop IDE itself is published under the GNU Public License (GPL), like KDE, and is therefore publicly avail- able at no cost—including its source code—and it may be used both for free and for commer- cial development.
    [Show full text]
  • The Origin of the Peculiarities of the Vietnamese Alphabet André-Georges Haudricourt
    The origin of the peculiarities of the Vietnamese alphabet André-Georges Haudricourt To cite this version: André-Georges Haudricourt. The origin of the peculiarities of the Vietnamese alphabet. Mon-Khmer Studies, 2010, 39, pp.89-104. halshs-00918824v2 HAL Id: halshs-00918824 https://halshs.archives-ouvertes.fr/halshs-00918824v2 Submitted on 17 Dec 2013 HAL is a multi-disciplinary open access L’archive ouverte pluridisciplinaire HAL, est archive for the deposit and dissemination of sci- destinée au dépôt et à la diffusion de documents entific research documents, whether they are pub- scientifiques de niveau recherche, publiés ou non, lished or not. The documents may come from émanant des établissements d’enseignement et de teaching and research institutions in France or recherche français ou étrangers, des laboratoires abroad, or from public or private research centers. publics ou privés. Published in Mon-Khmer Studies 39. 89–104 (2010). The origin of the peculiarities of the Vietnamese alphabet by André-Georges Haudricourt Translated by Alexis Michaud, LACITO-CNRS, France Originally published as: L’origine des particularités de l’alphabet vietnamien, Dân Việt Nam 3:61-68, 1949. Translator’s foreword André-Georges Haudricourt’s contribution to Southeast Asian studies is internationally acknowledged, witness the Haudricourt Festschrift (Suriya, Thomas and Suwilai 1985). However, many of Haudricourt’s works are not yet available to the English-reading public. A volume of the most important papers by André-Georges Haudricourt, translated by an international team of specialists, is currently in preparation. Its aim is to share with the English- speaking academic community Haudricourt’s seminal publications, many of which address issues in Southeast Asian languages, linguistics and social anthropology.
    [Show full text]
  • Glibc and System Calls Documentation Release 1.0
    Glibc and System Calls Documentation Release 1.0 Rishi Agrawal <[email protected]> Dec 28, 2017 Contents 1 Introduction 1 1.1 Acknowledgements...........................................1 2 Basics of a Linux System 3 2.1 Introduction...............................................3 2.2 Programs and Compilation........................................3 2.3 Libraries.................................................7 2.4 System Calls...............................................7 2.5 Kernel.................................................. 10 2.6 Conclusion................................................ 10 2.7 References................................................ 11 3 Working with glibc 13 3.1 Introduction............................................... 13 3.2 Why this chapter............................................. 13 3.3 What is glibc .............................................. 13 3.4 Download and extract glibc ...................................... 14 3.5 Walkthrough glibc ........................................... 14 3.6 Reading some functions of glibc ................................... 17 3.7 Compiling and installing glibc .................................... 18 3.8 Using new glibc ............................................ 21 3.9 Conclusion................................................ 23 4 System Calls On x86_64 from User Space 25 4.1 Setting Up Arguements......................................... 25 4.2 Calling the System Call......................................... 27 4.3 Retrieving the Return Value......................................
    [Show full text]
  • Preview Objective-C Tutorial (PDF Version)
    Objective-C Objective-C About the Tutorial Objective-C is a general-purpose, object-oriented programming language that adds Smalltalk-style messaging to the C programming language. This is the main programming language used by Apple for the OS X and iOS operating systems and their respective APIs, Cocoa and Cocoa Touch. This reference will take you through simple and practical approach while learning Objective-C Programming language. Audience This reference has been prepared for the beginners to help them understand basic to advanced concepts related to Objective-C Programming languages. Prerequisites Before you start doing practice with various types of examples given in this reference, I'm making an assumption that you are already aware about what is a computer program, and what is a computer programming language? Copyright & Disclaimer © Copyright 2015 by Tutorials Point (I) Pvt. Ltd. All the content and graphics published in this e-book are the property of Tutorials Point (I) Pvt. Ltd. The user of this e-book can retain a copy for future reference but commercial use of this data is not allowed. Distribution or republishing any content or a part of the content of this e-book in any manner is also not allowed without written consent of the publisher. We strive to update the contents of our website and tutorials as timely and as precisely as possible, however, the contents may contain inaccuracies or errors. Tutorials Point (I) Pvt. Ltd. provides no guarantee regarding the accuracy, timeliness or completeness of our website or its contents including this tutorial. If you discover any errors on our website or in this tutorial, please notify us at [email protected] ii Objective-C Table of Contents About the Tutorial ..................................................................................................................................
    [Show full text]
  • C and C++ Preprocessor Directives #Include #Define Macros Inline
    MODULE 10 PREPROCESSOR DIRECTIVES My Training Period: hours Abilities ▪ Able to understand and use #include. ▪ Able to understand and use #define. ▪ Able to understand and use macros and inline functions. ▪ Able to understand and use the conditional compilation – #if, #endif, #ifdef, #else, #ifndef and #undef. ▪ Able to understand and use #error, #pragma, # and ## operators and #line. ▪ Able to display error messages during conditional compilation. ▪ Able to understand and use assertions. 10.1 Introduction - For C/C++ preprocessor, preprocessing occurs before a program is compiled. A complete process involved during the preprocessing, compiling and linking can be read in Module W. - Some possible actions are: ▪ Inclusion of other files in the file being compiled. ▪ Definition of symbolic constants and macros. ▪ Conditional compilation of program code or code segment. ▪ Conditional execution of preprocessor directives. - All preprocessor directives begin with #, and only white space characters may appear before a preprocessor directive on a line. 10.2 The #include Preprocessor Directive - The #include directive causes copy of a specified file to be included in place of the directive. The two forms of the #include directive are: //searches for header files and replaces this directive //with the entire contents of the header file here #include <header_file> - Or #include "header_file" e.g. #include <stdio.h> #include "myheader.h" - If the file name is enclosed in double quotes, the preprocessor searches in the same directory (local) as the source file being compiled for the file to be included, if not found then looks in the subdirectory associated with standard header files as specified using angle bracket. - This method is normally used to include user or programmer-defined header files.
    [Show full text]
  • What Are the Problems with Embedded Linux?
    What Are the Problems with Embedded Linux? Every Operating System Has Benefits and Drawbacks Linux is ubiquitous. It runs most internet servers, is inside Android* smartphones, and is used on millions of embedded systems that, in the past, ran Real-Time Operating Systems (RTOSes). Linux can (and should) be used were possible for embedded projects, but while it gives you extreme choice, it also presents the risk of extreme complexity. What, then, are the trade-offs between embedded Linux and an RTOS? In this article, we cover some key considerations when evaluating Linux for a new development: ■ Design your system architecture first ■ What is Linux? ■ Linux vs. RTOSes ■ Free software is about liberty—not price ■ How much does Embedded Linux cost? ■ Why pay for Embedded Linux? ■ Should you buy Embedded Linux or roll-your-own (RYO)? ■ To fork or not to fork? ■ Software patching ■ Open source licensing ■ Making an informed decision The important thing is not whether Linux or an RTOS is “the best,” but whether either operating system—or both together—makes the most technical and financial sense for your project. We hope this article helps you make an informed decision. Design Your System Architecture First It is important to design your system architecture first—before choosing either Linux or an RTOS—because both choices can limit architectural freedom. You may discover that aspects of your design require neither Linux nor an RTOS, making your design a strong candidate for a heterogeneous approach that includes one or more bare-metal environments (with possibly a Linux and/or RTOS environment as well).
    [Show full text]
  • B-Prolog User's Manual
    B-Prolog User's Manual (Version 8.1) Prolog, Agent, and Constraint Programming Neng-Fa Zhou Afany Software & CUNY & Kyutech Copyright c Afany Software, 1994-2014. Last updated February 23, 2014 Preface Welcome to B-Prolog, a versatile and efficient constraint logic programming (CLP) system. B-Prolog is being brought to you by Afany Software. The birth of CLP is a milestone in the history of programming languages. CLP combines two declarative programming paradigms: logic programming and constraint solving. The declarative nature has proven appealing in numerous ap- plications, including computer-aided design and verification, databases, software engineering, optimization, configuration, graphical user interfaces, and language processing. It greatly enhances the productivity of software development and soft- ware maintainability. In addition, because of the availability of efficient constraint- solving, memory management, and compilation techniques, CLP programs can be more efficient than their counterparts that are written in procedural languages. B-Prolog is a Prolog system with extensions for programming concurrency, constraints, and interactive graphics. The system is based on a significantly refined WAM [1], called TOAM Jr. [19] (a successor of TOAM [16]), which facilitates software emulation. In addition to a TOAM emulator with a garbage collector that is written in C, the system consists of a compiler and an interpreter that are written in Prolog, and a library of built-in predicates that are written in C and in Prolog. B-Prolog does not only accept standard-form Prolog programs, but also accepts matching clauses, in which the determinacy and input/output unifications are explicitly denoted. Matching clauses are compiled into more compact and faster code than standard-form clauses.
    [Show full text]
  • Note on Using the CS+ Integrated Development Environment
    Tool News RENESAS TOOL NEWS on April 16, 2015: 150416/tn2 Note on Using the CS+ Integrated Development Environment When using the CS+ IDE, take note of the problem described in this note regarding the following point. Statements in source code which form a deeply-nested block 1. Products Concerned Products from the following list for which the version number of the CS+ common program is 3.00.00 to 3.01.00. - RX Family C/C++ Compiler Package (with IDE) - RL78 Family C Compiler Package (with IDE) - RH850 Family C Compiler Package (with IDE) - CS+ evaluation edition To check the version number of the product you have, refer to the following URL. https://www.renesas.com/cubesuite+_ver 2. Description CS+ might be terminated forcibly when a program is downloaded to a debugging tool or when an editor panel is opened after downloading a program. 3. Conditions Forced termination may occur when the source code for a project includes code that meets any of the following conditions. (a) { } blocks nested to a depth of 128 or more within a function. (b) 64 or more consecutive "else if" conditions are in sequence. (c) The total of double the number of consecutive "else if" conditions and the depth of the nesting of {} blocks at some point in the sequence of consecutive "else if" conditions is 128 or more. With conditions (b) and (c) above, the problem only arises when the C99 option is designated and the product is the RX family C/C++ compiler package (with IDE). 4. Workaround To avoid this problem, do any of the following.
    [Show full text]
  • Continuation-Passing Style
    Continuation-Passing Style CS 6520, Spring 2002 1 Continuations and Accumulators In our exploration of machines for ISWIM, we saw how to explicitly track the current continuation for a computation (Chapter 8 of the notes). Roughly, the continuation reflects the control stack in a real machine, including virtual machines such as the JVM. However, accurately reflecting the \memory" use of textual ISWIM requires an implementation different that from that of languages like Java. The ISWIM machine must extend the continuation when evaluating an argument sub-expression, instead of when calling a function. In particular, function calls in tail position require no extension of the continuation. One result is that we can write \loops" using λ and application, instead of a special for form. In considering the implications of tail calls, we saw that some non-loop expressions can be converted to loops. For example, the Scheme program (define (sum n) (if (zero? n) 0 (+ n (sum (sub1 n))))) (sum 1000) requires a control stack of depth 1000 to handle the build-up of additions surrounding the recursive call to sum, but (define (sum2 n r) (if (zero? n) r (sum2 (sub1 n) (+ r n)))) (sum2 1000 0) computes the same result with a control stack of depth 1, because the result of a recursive call to sum2 produces the final result for the enclosing call to sum2 . Is this magic, or is sum somehow special? The sum function is slightly special: sum2 can keep an accumulated total, instead of waiting for all numbers before starting to add them, because addition is commutative.
    [Show full text]
  • About ILE C/C++ Compiler Reference
    IBM i 7.3 Programming IBM Rational Development Studio for i ILE C/C++ Compiler Reference IBM SC09-4816-07 Note Before using this information and the product it supports, read the information in “Notices” on page 121. This edition applies to IBM® Rational® Development Studio for i (product number 5770-WDS) and to all subsequent releases and modifications until otherwise indicated in new editions. This version does not run on all reduced instruction set computer (RISC) models nor does it run on CISC models. This document may contain references to Licensed Internal Code. Licensed Internal Code is Machine Code and is licensed to you under the terms of the IBM License Agreement for Machine Code. © Copyright International Business Machines Corporation 1993, 2015. US Government Users Restricted Rights – Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp. Contents ILE C/C++ Compiler Reference............................................................................... 1 What is new for IBM i 7.3.............................................................................................................................3 PDF file for ILE C/C++ Compiler Reference.................................................................................................5 About ILE C/C++ Compiler Reference......................................................................................................... 7 Prerequisite and Related Information..................................................................................................
    [Show full text]
  • 1 Curl and Divergence
    Sections 15.5-15.8: Divergence, Curl, Surface Integrals, Stokes' and Divergence Theorems Reeve Garrett 1 Curl and Divergence Definition 1.1 Let F = hf; g; hi be a differentiable vector field defined on a region D of R3. Then, the divergence of F on D is @ @ @ div F := r · F = ; ; · hf; g; hi = f + g + h ; @x @y @z x y z @ @ @ where r = h @x ; @y ; @z i is the del operator. If div F = 0, we say that F is source free. Note that these definitions (divergence and source free) completely agrees with their 2D analogues in 15.4. Theorem 1.2 Suppose that F is a radial vector field, i.e. if r = hx; y; zi, then for some real number p, r hx;y;zi 3−p F = jrjp = (x2+y2+z2)p=2 , then div F = jrjp . Theorem 1.3 Let F = hf; g; hi be a differentiable vector field defined on a region D of R3. Then, the curl of F on D is curl F := r × F = hhy − gz; fz − hx; gx − fyi: If curl F = 0, then we say F is irrotational. Note that gx − fy is the 2D curl as defined in section 15.4. Therefore, if we fix a point (a; b; c), [gx − fy](a; b; c), measures the rotation of F at the point (a; b; c) in the plane z = c. Similarly, [hy − gz](a; b; c) measures the rotation of F in the plane x = a at (a; b; c), and [fz − hx](a; b; c) measures the rotation of F in the plane y = b at the point (a; b; c).
    [Show full text]