The C family of programming languages are rivaled only by Java in their popularity. In 2014, the C family (that is C, C++ and C#) had a combined market share of 27.3% to Java’s 19.3% Considering this, you will almost certainly learn one or the other in your college or university, and many software professionals have had successful careers specialising in the C family.
The oldest of the languages, C, was designed by Dennis Ritchie in 1972. It has been a mainstay of operating system and low level application programming for decades. It is what is known as an imperative language, which means a program is made up of functions that execute in a linear progression, with each one changing some state of the program. For example, a line of code may add a number to another in memory, display a certain value on screen, or write a value to a file on disk. Programs are structured with “if” statements which fork the execution path of a program into multiple directions, or loops, which repeat operations until a condition is met. Complex data structures can be implemented, but in a more crude form than in C++ or C#, and the key factor is that the functions always take and manipulate these data structures, rather than exist as a part of them.
C++ moves us away from this paradigm into the Object-Oriented Programming (OOP) world. In OOP, the data structures we once knew are objects capable of their own actions. Commonly encountered examples in university tutorials are things such as a car, which would have methods (the actions) such as driving or changing gears. Instead of having a central program that operates on data structures, the data structures themselves gain some level of independence through what’s known as ‘encapsulation’. This allows program objects to hide their internal workings and instead provide publically accessible interfaces – rather than setting the location on the aforementioned car, we must call the drive function to direct it where to go. This allows the object to keep a consistent internal state and only allow other objects in the system to have as much knowledge about each other as is needed to perform their function, and never enough to break the functionality of another object. Understanding this key aspect which differentiates C++ from C is crucial in becoming confident and competent with C++ and more modern programming languages as a whole.
C# is the last member of the traditional C family of languages (although Apple/iOS-focused Objective C has its place too) and is usually only considered in association with Microsoft’s .Net framework. Syntactically C# can be viewed as just a cleaner version of C++, and although it introduces a lot of modern programming language quirks, it is less of a departure from C++ than C++ was from C. The .Net framework, on the other hand, provides easy to use tools to build Windows applications of all types, including desktop applications, web applications, web services, Windows services and Microsoft Office plugins. This is essentially what drives the market share in C#. The language has been improved significantly to modernise C++ to rival Java, as well. Memory management is no longer the responsibility of the programmer, as the application is run within what’s known as managed execution, which resembles Java’s virtual machine. This attempts to handle one of the most common problems with C and C++ applications, which was improper memory usage leading to applications that become slower and more resource-heavy during their lifecycle.
This is but an overview of one of the largest and most feature-rich programming languages in the field of computer programming, but it touches on some of the most important fundamentals you will have to learn on your journey to becoming a software developer. Indeed, you will encounter most of these concepts again and again regardless of which language you learn, so even if you don’t plan to make a career out of C programming, knowing these core concepts will stand you in good stead.
Your first C# program
It is difficult for a developer to consider writing C# code to any real degree without dipping into the .NET Framework or Mono extensively. As a result, most tutorials and examples you encounter take this coupling for granted and do not really differentiate between the two, and this will be no exception. The following code is for use with .NET 4.6 (the latest version of the framework at the time of writing) but should work on Mono or older versions without too many problems.
First let’s turn our attention to the inevitable “Hello World” application. First off we can make a new solution (this code is targeted at Visual Studio, the dominant IDE for C#), selecting the Console Application template. This gives us a single code file, “Program.cs”, and an application config file called App.config, which we will disregard for now. By default, the following code is given to us:
Anyone familiar with writing C console applications will recognize what they are seeing here. The program class is designed to be the startup class for our application (from the project and solution properties) and the Main() method is our entry point. At execution time, code will come in here, execute to the end of Main() function, and then quit. If you run this application by hitting ‘start’, you will see a console window appear and disappear just as quickly, which means our empty application has executed without error. The first thing we can do is add the following line to our application so that when it has completed, the application waits for us to press a key before exiting. We will leave this line at the end of our application for the duration of this tutorial. Add it, and try running your application again.
Now add the following line above this one, to create our ‘Hello World’ application.
When you run this, you will now see your greeting as text. Let’s examine what’s happening here with both lines of code. Console is a static class provided to us from the System namespace, which we can see at the top of our file (“using System;”). Static classes do not require an instantiated object to execute their functions, and are instead executed solely from any code module that has access to the namespace of your static class. The staticmethods we are using here are ‘WriteLine’ and ‘ReadKey’, which are fairly self-explanatory. WriteLine is passed one argument, which is our text string, “Hello World!” When we run that line of code, the application prints our text to the console, followed by a new-line. The program then waits (also referred to as ‘blocking for input’) at the ‘Console.ReadKey()’ line until the user pushes a key and the program can continue to the end, exiting. As you can see, these two lines actually perform a large number of low-level operations without us ever having to think about them, which is the strength of high level languages from a programming perspective.
Let’s go ahead and customize our input by reading from the console. Above our WriteLine method, we can add another method – ReadLine. ReadLine is like ReadKey, but instead we wait for a user to type several characters and press return, at which point the text they have typed will be returned and we can store it in a variable. This is done with the equals (=) operator. In the following example, we combine variable declaration and assignment into one line after prompting for input. The output of the ReadLine method is subsequently stored in the userName variable which is of type string (as in a sequence of characters, or text). Now, our program looks like this:
However, this program is identical to the following:
This is because declaring a variable and later assigning it is the same as doing both actions on the same line. In fact, the second example is a bad practice, as you may forget to assign your variable to some value before you use it, which will cause your program to crash.
The other part of this example that has changed is the ‘WriteLine’ method, which now is outputting "Hello, " + username. In this instance, the + operation usually associated with adding numbers is combining two strings. As a result, when executed, the value inside ‘username’ is appended to the end of “Hello, “ to create a new string. Try running this program now and experimenting with the output.
One brief word on variable declarations before we move on: You may see other tutorials and subsequent ones in this series use ‘var’ instead of ‘string’ (or any other type like ‘int’). This is because modern versions of C# can save the programmer the job of declaring a variable type and instead let the compiler do that work. As a result, the compiler has access to information about the ‘Console.ReadLine()’ method and can correctly determine that it will return a string, which it substitutes for ‘var’ at compile time. As a result, this is equally valid, and an even better practice:
Having built this basic application you have gained a brief insight into input/output methods and the structure of an application. Once you understand such procedural code and basic variables, it is time to move on to understand object oriented programming in order to make the most of the power of a high level language like C#.
To fulfill our tutoring mission of online education, our college homework help and online tutoring centers are standing by 24/7, ready to assist college students who need homework help with all aspects of C-family programming. Our computer science tutors can help with all your projects, large or small, and we challenge you to find better online C-family programming tutoring anywhere.
Please let us know the date by which you need help from your tutor or the date and time you wish to have an online tutoring session.
Normal response time: Our most experienced, most successful tutors are provided for maximum expertise and reliability.
Fast response time: Used only for emergencies when speed is the single most important factor.
We require your email address so that we can send you an email alert when the tutor responds to your message.
You will get a negotiable price quote with no obligation.
The C Library Reference Guideby Eric Huss
© Copyright 1997 Eric Huss
Introduction 1. Language 1.1 Characters 1.1.1 Trigraph Characters 1.1.2 Escape Sequences 1.1.3 Comments 1.2 Identifiers 1.2.1 Keywords 1.2.2 Variables 1.2.3 Enumerated Tags 1.2.4 Arrays 1.2.5 Structures and Unions 1.2.6 Constants 1.2.7 Strings 1.2.8 sizeof Keyword 1.3 Functions 1.3.1 Definition 1.3.2 Program Startup 1.4 References 1.4.1 Pointers and the Address Operator 1.4.2 Typecasting 1.5 Operators 1.5.1 Postfix 1.5.2 Unary and Prefix 1.5.3 Normal 1.5.4 Boolean 1.5.5 Assignment 1.5.6 Precedence 1.6 Statements 1.6.1 if 1.6.2 switch 1.6.3 while 1.6.4 do 1.6.5 for 1.6.6 goto 1.6.7 continue 1.6.8 break 1.6.9 return 1.7 Preprocessing Directives 1.7.1 #if, #elif, #else, #endif 1.7.2 #define, #undef, #ifdef, #ifndef 1.7.3 #include 1.7.4 #line 1.7.5 #error 1.7.6 #pragma 1.7.7 Predefined Macros 2. Library 2.1 assert.h 2.1.1 assert 2.2 ctype.h 2.2.1 is... Functions 2.2.2 to... Functions 2.3 errno.h 2.3.1 EDOM 2.3.2 ERANGE 2.3.3 errno 2.4 float.h 2.4.1 Defined Values 2.5 limits.h 2.5.1 Defined Values 2.6 locale.h 2.6.1 Variables and Definitions 2.6.2 setlocale 2.6.3 localeconv 2.7 math.h 2.7.1 Error Conditions 2.7.2 Trigonometric Functions 188.8.131.52 acos 184.108.40.206 asin 220.127.116.11 atan 18.104.22.168 atan2 22.214.171.124 cos 126.96.36.199 cosh 188.8.131.52 sin 184.108.40.206 sinh 220.127.116.11 tan 18.104.22.168 tanh 2.7.3 Exponential, Logarithmic, and Power Functions 22.214.171.124 exp 126.96.36.199 frexp 188.8.131.52 ldexp 184.108.40.206 log 220.127.116.11 log10 18.104.22.168 modf 22.214.171.124 pow 126.96.36.199 sqrt 2.7.4 Other Math Functions 188.8.131.52 ceil 184.108.40.206 fabs 220.127.116.11 floor 18.104.22.168 fmod 2.8 setjmp.h 2.8.1 Variables and Definitions 2.8.2 setjmp 2.8.3 longjmp 2.9 signal.h 2.9.1 Variables and Definitions 2.9.2 signal 2.9.3 raise 2.10 stdarg.h 2.10.1 Variables and Definitions 2.10.2 va_start 2.10.3 va_arg 2.10.4 va_end 2.11 stddef.h 2.11.1 Variables and Definitions 2.12 stdio.h 2.12.1 Variables and Definitions 2.12.2 Streams and Files 2.12.3 File Functions 22.214.171.124 clearerr 126.96.36.199 fclose 188.8.131.52 feof 184.108.40.206 ferror 220.127.116.11 fflush 18.104.22.168 fgetpos 22.214.171.124 fopen 126.96.36.199 fread 188.8.131.52 freopen 184.108.40.206 fseek 220.127.116.11 fsetpos 18.104.22.168 ftell 22.214.171.124 fwrite 126.96.36.199 remove 188.8.131.52 rename 184.108.40.206 rewind 220.127.116.11 setbuf 18.104.22.168 setvbuf 22.214.171.124 tmpfile 126.96.36.199 tmpnam 2.12.4 Formatted I/O Functions 188.8.131.52 ...printf Functions 184.108.40.206 ...scanf Functions 2.12.5 Character I/O Functions 220.127.116.11 fgetc 18.104.22.168 fgets 22.214.171.124 fputc 126.96.36.199 fputs 188.8.131.52 getc 184.108.40.206 getchar 220.127.116.11 gets 18.104.22.168 putc 22.214.171.124 putchar 126.96.36.199 puts 188.8.131.52 ungetc 2.12.7 Error Functions 184.108.40.206 perror 2.13 stdlib.h 2.13.1 Variables and Definitions 2.13.2 String Functions 220.127.116.11 atof 18.104.22.168 atoi 22.214.171.124 atol 126.96.36.199 strtod 188.8.131.52 strtol 184.108.40.206 strtoul 2.13.3 Memory Functions 220.127.116.11 calloc 18.104.22.168 free 22.214.171.124 malloc 126.96.36.199 realloc 2.13.4 Environment Functions 188.8.131.52 abort 184.108.40.206 atexit 220.127.116.11 exit 18.104.22.168 getenv 22.214.171.124 system 2.13.5 Searching and Sorting Functions 126.96.36.199 bsearch 188.8.131.52 qsort 2.13.6 Math Functions 184.108.40.206 abs 220.127.116.11 div 18.104.22.168 labs 22.214.171.124 ldiv 126.96.36.199 rand 188.8.131.52 srand 2.13.7 Multibyte Functions 184.108.40.206 mblen 220.127.116.11 mbstowcs 18.104.22.168 mbtowc 22.214.171.124 wcstombs 126.96.36.199 wctomb 2.14 string.h 2.14.1 Variables and Definitions 2.14.2 memchr 2.14.3 memcmp 2.14.4 memcpy 2.14.5 memmove 2.14.6 memset 2.14.7 strcat 2.14.8 strncat 2.14.9 strchr 2.14.10 strcmp 2.14.11 strncmp 2.14.12 strcoll 2.14.13 strcpy 2.14.14 strncpy 2.14.15 strcspn 2.14.16 strerror 2.14.17 strlen 2.14.18 strpbrk 2.14.19 strrchr 2.14.20 strspn 2.14.21 strstr 2.14.22 strtok 2.14.23 strxfrm 2.15 time.h 2.15.1 Variables and Definitions 2.15.2 asctime 2.15.3 clock 2.15.4 ctime 2.15.5 difftime 2.15.6 gmtime 2.15.7 localtime 2.15.8 mktime 2.15.9 strftime 2.15.10 timeAppendix AASCII ChartIndexIndex